Elastic-Job是一个分布式任务执行框架,并不直接提供数据处理的功能,框架只会将分片项分配至各个运行中的作业服务器,开发者需要自行处理分片项与真实数据的对应关系elasticjob使用了 Quartz,任务执行是实现了 Quartz 的 Job 接口下面是一个工作中案例:
首先引入springboot中pom依赖:
<!-- elastic job -->
<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-lite-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-lite-spring-boot-starter</artifactId>
</dependency>
代码中实现SimpleJob接口:
public class JobLite implements SimpleJob {
public void execute(ShardingContext context) {
//获取执行任务的参数
String parameter = shardingContext.getShardingParameter();
int day = NumberUtils.toInt(parameter);
switch (context.getShardingItem()) {
case 0:
// do something by sharding item 0
break;
case 1:
// do something by sharding item 1
break;
case 2:
// do something by sharding item 2
break;
// case n: ...
}
}
propertyes配置文件中内容如下
#################elasticjob config ##################
//zk的地址
elasticjob.reg-center.server-lists=test.zookeeper@
//job的名称
elasticjob.reg-center.namespace=test/jobs
elasticjob.reg-center.base-sleep-time-milliseconds=1000
elasticjob.reg-center.max-sleep-time-milliseconds=3000
//失败重试数
elasticjob.reg-center.max-retries=5
elasticjob.reg-center.session-timeout-milliseconds=10000
elasticjob.reg-center.connection-timeout-milliseconds=10000
//任务实现类
elasticjob.jobs.testJob.elastic-job-class=com.test.service.schedule.testJob
elasticjob.jobs.testJob.cron=0 0/1 * * * ?
//执行任务的线程数
elasticjob.jobs.testJob.sharding-total-count=2
//线程中的参数
elasticjob.jobs.testJob.sharding-item-parameters=0=0,1=1
推荐阅读文章:
springboot集成elastic job分布式任务调度-Apache版本尝鲜_unable to find property 'jobclass' on class: org.a-CSDN博客
https://www.cnblogs.com/dalianpai/p/12069429.html