elasticJob
项目结构如下:
pom.xml依赖,因为elastic-job-lite-spring中有curator依赖,所以不用引curator jar包,
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>2.1.5</version>
</dependency>
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:reg="http://www.dangdang.com/schema/ddframe/reg" xmlns:job="http://www.dangdang.com/schema/ddframe/job"
xmlns:jod="http://www.dangdang.com/schema/ddframe/job"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.dangdang.com/schema/ddframe/reg http://www.dangdang.com/schema/ddframe/reg/reg.xsd
http://www.dangdang.com/schema/ddframe/job http://www.dangdang.com/schema/ddframe/job/job.xsd">
<!--配置作业注册中心 -->
<reg:zookeeper id="regCenter" server-lists="zookeeper注册中心地址" namespace="名字"
base-sleep-time-milliseconds="基本的sleepTime"
max-sleep-time-milliseconds="最大的sleepTime"
max-retries="最大尝试次数"/>
<!-- 同步example-->
<job:simple id="exampleJob"
class="spring.boot.job.ExampleJob" registry-center-ref="regCenter"
sharding-total-count="10" cron="0 0/1 * * * ? " description="example"
overwrite="true"/>
</beans>
exampleJob代码如下:
package spring.boot.job;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ExampleJob implements SimpleJob {
private static final Logger LOGGER = LoggerFactory.getLogger(ExampleJob.class);
@Override
public void execute(ShardingContext shardingContext) {
int item = shardingContext.getShardingItem();
int itemTotal = shardingContext.getShardingTotalCount();
LOGGER.info("===item:{},itemTotal:{}",item,itemTotal);
}
}
启动类如下:
package spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
//@ImportResource("classpath:spring/dubbo.xml")
@ImportResource("classpath:spring/applicationContext-elasticjoblite.xml")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动项目测试,结果如下:
2019-10-29 16:24:01.699 INFO 6216 --- [ob-exampleJob-1] spring.boot.job.ExampleJob : ===item:0,itemTotal:10
2019-10-29 16:24:01.699 INFO 6216 --- [ob-exampleJob-2] spring.boot.job.ExampleJob : ===item:1,itemTotal:10
2019-10-29 16:24:01.699 INFO 6216 --- [ob-exampleJob-3] spring.boot.job.ExampleJob : ===item:2,itemTotal:10
2019-10-29 16:24:01.699 INFO 6216 --- [ob-exampleJob-4] spring.boot.job.ExampleJob : ===item:3,itemTotal:10
2019-10-29 16:24:01.699 INFO 6216 --- [ob-exampleJob-5] spring.boot.job.ExampleJob : ===item:4,itemTotal:10
2019-10-29 16:24:01.699 INFO 6216 --- [ob-exampleJob-6] spring.boot.job.ExampleJob : ===item:5,itemTotal:10
2019-10-29 16:24:01.699 INFO 6216 --- [ob-exampleJob-1] spring.boot.job.ExampleJob : ===item:8,itemTotal:10
2019-10-29 16:24:01.699 INFO 6216 --- [ob-exampleJob-2] spring.boot.job.ExampleJob : ===item:9,itemTotal:10
2019-10-29 16:24:01.699 INFO 6216 --- [ob-exampleJob-7] spring.boot.job.ExampleJob : ===item:6,itemTotal:10
2019-10-29 16:24:01.699 INFO 6216 --- [ob-exampleJob-8] spring.boot.job.ExampleJob : ===item:7,itemTotal:10
多台服务器同时执行定时任务,如下:
将任务分为10片(sharding-total-count="10")
可以通过id/10来做分片管理;
int item = shardingContext.getShardingItem();
int itemTotal = shardingContext.getShardingTotalCount();
if (item == Math.floorMod(Dto.getId(), itemTotal)) {
//doSomething
}