赞
踩
- <dependency>
- <groupId>com.github.kuhn-he</groupId>
- <artifactId>elastic-job-lite-spring-boot-starter</artifactId>
- <version>2.1.53</version>
- </dependency>
- #演示静态任务
- elaticjob:
- zookeeper:
- server-lists: 127.0.0.1:2181
- namespace: synctask
-
- #演示动态定时任务案例
- dynamiczk: 127.0.0.1:2181
- dynamicnamespace: dynamictask
- import com.dangdang.ddframe.job.api.ShardingContext;
- import com.dangdang.ddframe.job.api.simple.SimpleJob;
- import com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob;
- import com.example.sdfsdfsdfsf_renwu.mapper.TbShopMapper;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
-
- /***
- * 1:执行周期
- * 2:分片
- * 3:指定Zookeeper中的命名空间
- */
- @ElasticSimpleJob(cron = "0/5 * * * * ?",
- jobName = "firstJob_1",
- shardingTotalCount = 3,
- jobParameter = "测试参数",
- shardingItemParameters = "0=zhangsan,1=lishi,2=wangwu")
- @Component
- @Slf4j
- public class StaticJobTask implements SimpleJob {
-
- @Autowired
- private TbShopMapper tbShopMapper;
-
- //执行的作业
- @Override
- public void execute(ShardingContext shardingContext) {
-
- log.info("\nThread ID: {}\n任务总片数: {}\n当前分片项: {}\n当前参数: {}\n当前任务名称: {}\n当前任务参数: {}\n当前任务的id: {}\n",
- //获取当前线程的id
- Thread.currentThread().getId(),
- //获取任务总片数
- shardingContext.getShardingTotalCount(),
- //获取当前分片项
- shardingContext.getShardingItem(),
- //获取当前的参数
- shardingContext.getShardingParameter(),
- //获取当前的任务名称
- shardingContext.getJobName(),
- //获取当前任务参数
- shardingContext.getJobParameter(),
- //获取任务的id
- shardingContext.getTaskId()
- );
-
- }
- }
-
- import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
- import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- public class DynamicConfig {
-
- @Value("${dynamiczk}")
- private String dynamiczk;
- @Value("${dynamicnamespace}")
- private String dynamicnamespace;
-
- /****
- * 指定当前注册地址信息
- */
- @Bean
- public ZookeeperConfiguration zookeeperConfiguration() {
- return new ZookeeperConfiguration(dynamiczk,dynamicnamespace);
- }
-
- /****
- * 向Zookeeper服务注册
- */
- @Bean(initMethod = "init")
- public ZookeeperRegistryCenter zookeeperRegistryCenter(ZookeeperConfiguration zookeeperConfiguration){
- return new ZookeeperRegistryCenter(zookeeperConfiguration);
- }
- }
- import com.dangdang.ddframe.job.api.ShardingContext;
- import com.dangdang.ddframe.job.api.simple.SimpleJob;
-
- public class DynamicJob implements SimpleJob {
-
- //执行的作业
- @Override
- public void execute(ShardingContext shardingContext) {
- System.out.println("执行任务的逻辑");
- }
- }
-
- import com.dangdang.ddframe.job.api.simple.SimpleJob;
- import com.dangdang.ddframe.job.config.JobCoreConfiguration;
- import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
- import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
- import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
- import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
-
- @Component
- public class DynamicTaskCreate {
-
- @Autowired
- private ZookeeperRegistryCenter zookeeperRegistryCenter;
-
- /***
- * 作业创建
- * @param jobName:作业名字
- * @param cron:表达式
- * @param shardingTotalCount:分片数量
- * @param instance:作业实例
- * @param parameters:额外参数
- */
- public void create(String jobName, String cron, int shardingTotalCount, SimpleJob instance,String parameters){
-
- //1.配置作业->Builder->构建:LiteJobConfiguration
- LiteJobConfiguration.Builder builder = LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(
- JobCoreConfiguration.newBuilder(
- jobName,
- cron,
- shardingTotalCount
- ).jobParameter(parameters).build(),
- instance.getClass().getName()
- )).overwrite(true);
- LiteJobConfiguration liteJobConfiguration = builder.build();
-
- //2.开启作业
- new SpringJobScheduler(instance,zookeeperRegistryCenter,liteJobConfiguration).init();
- }
- }
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * 动态添加任务
- */
- @RestController
- public class Controller_1 {
-
- @Autowired
- private DynamicTaskCreate dynamicTaskCreate;
-
- @RequestMapping("/addTask")
- public void addTask(SeckillActivity seckillActivity)
- {
- //创建任务调度,活动结束的时候执行
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ss mm HH dd MM ? yyyy");
- String cron = simpleDateFormat.format(seckillActivity.getEndTime());
- System.out.println("cron:"+cron);
-
- //cron="3/3 * * * * ? *";
-
- dynamicTaskCreate.create("renwu_"+seckillActivity.getId(),
- cron,
- 1,
- new DynamicJob(),
- "xingming=zhangsan");
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。