2
回答
Elastic-Job-lite 版本,10个分片,单机每5秒执行一次,为何10个分片是分开执行,而不是同时执行
科大讯飞通用文字识别100000次/天免费使用。立即申请   

spring 配置如下:

<job:simple  id="demoSimpleSpringJob5" class="com.coderxing.demo.MyElasticJob" 

registry-center-ref="regCenter" cron="0/5 * * * * ?" sharding-total-count="10" 

reconcile-interval-minutes="1" overwrite="true" />

代码如下: 

public class MyElasticJob implements SimpleJob {

	/**
	 * shardingContext 包含作业的分片信息。
	 */
	@Override
	public void execute(ShardingContext shardingContext) {

		System.out.println("========================="+new Date());
		System.out.println(Thread.currentThread().getName() + ", id:" + shardingContext.getShardingItem() + ", total:"
				+ shardingContext.getShardingTotalCount() + ", P:" + shardingContext.getShardingParameter());
	}

}

按照我的理解,一台机器上,应该是启动10个线程同时执行,但现象是每隔10秒,执行一个分片,执行结果如下:

=========================Wed Aug 30 13:45:47 CST 2017
inner-job-demoSimpleSpringJob5-1, id:0, total:10, P:null
=========================Wed Aug 30 13:45:52 CST 2017
inner-job-demoSimpleSpringJob5-2, id:1, total:10, P:null
=========================Wed Aug 30 13:46:07 CST 2017
inner-job-demoSimpleSpringJob5-3, id:2, total:10, P:null
=========================Wed Aug 30 13:46:12 CST 2017
inner-job-demoSimpleSpringJob5-4, id:3, total:10, P:null
=========================Wed Aug 30 13:46:27 CST 2017
inner-job-demoSimpleSpringJob5-5, id:4, total:10, P:null
=========================Wed Aug 30 13:46:32 CST 2017
inner-job-demoSimpleSpringJob5-6, id:5, total:10, P:null
=========================Wed Aug 30 13:46:47 CST 2017
inner-job-demoSimpleSpringJob5-7, id:6, total:10, P:null
=========================Wed Aug 30 13:46:52 CST 2017
inner-job-demoSimpleSpringJob5-8, id:7, total:10, P:null
=========================Wed Aug 30 13:47:07 CST 2017
inner-job-demoSimpleSpringJob5-9, id:8, total:10, P:null

 

举报
coderxing
发帖于10个月前 2回/650阅
顶部