当前位置:   article > 正文

SpringBoot整合Elastic-Job 2.1.53版本任务调度,手动任务,动态添加任务演示

SpringBoot整合Elastic-Job 2.1.53版本任务调度,手动任务,动态添加任务演示

前提:zookeeper安装并成功运行

1 pom依赖

  1. <dependency>
  2. <groupId>com.github.kuhn-he</groupId>
  3. <artifactId>elastic-job-lite-spring-boot-starter</artifactId>
  4. <version>2.1.53</version>
  5. </dependency>

2 yml配置

  1. #演示静态任务
  2. elaticjob:
  3. zookeeper:
  4. server-lists: 127.0.0.1:2181
  5. namespace: synctask
  6. #演示动态定时任务案例
  7. dynamiczk: 127.0.0.1:2181
  8. dynamicnamespace: dynamictask


手动任务,分片任务演示

  1. import com.dangdang.ddframe.job.api.ShardingContext;
  2. import com.dangdang.ddframe.job.api.simple.SimpleJob;
  3. import com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob;
  4. import com.example.sdfsdfsdfsf_renwu.mapper.TbShopMapper;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8. /***
  9. * 1:执行周期
  10. * 2:分片
  11. * 3:指定Zookeeper中的命名空间
  12. */
  13. @ElasticSimpleJob(cron = "0/5 * * * * ?",
  14. jobName = "firstJob_1",
  15. shardingTotalCount = 3,
  16. jobParameter = "测试参数",
  17. shardingItemParameters = "0=zhangsan,1=lishi,2=wangwu")
  18. @Component
  19. @Slf4j
  20. public class StaticJobTask implements SimpleJob {
  21. @Autowired
  22. private TbShopMapper tbShopMapper;
  23. //执行的作业
  24. @Override
  25. public void execute(ShardingContext shardingContext) {
  26. log.info("\nThread ID: {}\n任务总片数: {}\n当前分片项: {}\n当前参数: {}\n当前任务名称: {}\n当前任务参数: {}\n当前任务的id: {}\n",
  27. //获取当前线程的id
  28. Thread.currentThread().getId(),
  29. //获取任务总片数
  30. shardingContext.getShardingTotalCount(),
  31. //获取当前分片项
  32. shardingContext.getShardingItem(),
  33. //获取当前的参数
  34. shardingContext.getShardingParameter(),
  35. //获取当前的任务名称
  36. shardingContext.getJobName(),
  37. //获取当前任务参数
  38. shardingContext.getJobParameter(),
  39. //获取任务的id
  40. shardingContext.getTaskId()
  41. );
  42. }
  43. }



动态添加执行任务演示


a zookeeper配置类

  1. import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
  2. import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class DynamicConfig {
  8. @Value("${dynamiczk}")
  9. private String dynamiczk;
  10. @Value("${dynamicnamespace}")
  11. private String dynamicnamespace;
  12. /****
  13. * 指定当前注册地址信息
  14. */
  15. @Bean
  16. public ZookeeperConfiguration zookeeperConfiguration() {
  17. return new ZookeeperConfiguration(dynamiczk,dynamicnamespace);
  18. }
  19. /****
  20. * 向Zookeeper服务注册
  21. */
  22. @Bean(initMethod = "init")
  23. public ZookeeperRegistryCenter zookeeperRegistryCenter(ZookeeperConfiguration zookeeperConfiguration){
  24. return new ZookeeperRegistryCenter(zookeeperConfiguration);
  25. }
  26. }

b 执行任务的逻辑

  1. import com.dangdang.ddframe.job.api.ShardingContext;
  2. import com.dangdang.ddframe.job.api.simple.SimpleJob;
  3. public class DynamicJob implements SimpleJob {
  4. //执行的作业
  5. @Override
  6. public void execute(ShardingContext shardingContext) {
  7. System.out.println("执行任务的逻辑");
  8. }
  9. }

c 任务作业创建

  1. import com.dangdang.ddframe.job.api.simple.SimpleJob;
  2. import com.dangdang.ddframe.job.config.JobCoreConfiguration;
  3. import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
  4. import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
  5. import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
  6. import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9. @Component
  10. public class DynamicTaskCreate {
  11. @Autowired
  12. private ZookeeperRegistryCenter zookeeperRegistryCenter;
  13. /***
  14. * 作业创建
  15. * @param jobName:作业名字
  16. * @param cron:表达式
  17. * @param shardingTotalCount:分片数量
  18. * @param instance:作业实例
  19. * @param parameters:额外参数
  20. */
  21. public void create(String jobName, String cron, int shardingTotalCount, SimpleJob instance,String parameters){
  22. //1.配置作业->Builder->构建:LiteJobConfiguration
  23. LiteJobConfiguration.Builder builder = LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(
  24. JobCoreConfiguration.newBuilder(
  25. jobName,
  26. cron,
  27. shardingTotalCount
  28. ).jobParameter(parameters).build(),
  29. instance.getClass().getName()
  30. )).overwrite(true);
  31. LiteJobConfiguration liteJobConfiguration = builder.build();
  32. //2.开启作业
  33. new SpringJobScheduler(instance,zookeeperRegistryCenter,liteJobConfiguration).init();
  34. }
  35. }

d 创建作业任务的入口

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * 动态添加任务
  6. */
  7. @RestController
  8. public class Controller_1 {
  9. @Autowired
  10. private DynamicTaskCreate dynamicTaskCreate;
  11. @RequestMapping("/addTask")
  12. public void addTask(SeckillActivity seckillActivity)
  13. {
  14. //创建任务调度,活动结束的时候执行
  15. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ss mm HH dd MM ? yyyy");
  16. String cron = simpleDateFormat.format(seckillActivity.getEndTime());
  17. System.out.println("cron:"+cron);
  18. //cron="3/3 * * * * ? *";
  19. dynamicTaskCreate.create("renwu_"+seckillActivity.getId(),
  20. cron,
  21. 1,
  22. new DynamicJob(),
  23. "xingming=zhangsan");
  24. }
  25. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/873535
推荐阅读
相关标签
  

闽ICP备14008679号