ElasticJob分布式调度,使用注册中心zookeeper开启动态定时任务附源码(三)
问题背景
上一篇介绍了ElasticJob分布式固态定时任务,这个篇章介绍一下分布式动态定时任务 注意事项:
项目搭建
1 引入pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yg</groupId>
<artifactId>elastic-job</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>elastic-job</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- elastic-job -->
<!-- <dependency>-->
<!-- <groupId>org.apache.shardingsphere.elasticjob</groupId>-->
<!-- <artifactId>elasticjob-lite-core</artifactId>-->
<!-- <version>3.0.1</version>-->
<!-- </dependency>-->
<!-- elastic-job -->
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>2.1.5</version>
</dependency>
<!-- elastic-job end -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.0</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2 动态添加定时任务句柄
package com.yg.elasticjob.elasticjob;
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.stereotype.Component;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class ElasticJobHandler {
@Resource
private ZookeeperRegistryCenter registryCenter;
@Resource
private ElasticJobListener elasticJobListener;
private static LiteJobConfiguration.Builder simpleJobConfigBuilder(String jobName, Class<? extends SimpleJob> jobClass, int shardingTotalCount, String cron, String id) {
LiteJobConfiguration.Builder builder = LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(
JobCoreConfiguration.
newBuilder(jobName, cron, shardingTotalCount).
jobParameter(id).
build(),
jobClass.getCanonicalName()));
builder.overwrite(true);
return builder;
}
public void addPublishJob(String cron, String id, String jobName, SimpleJob instance) {
LiteJobConfiguration jobConfig = simpleJobConfigBuilder(
jobName,
instance.getClass(),
1,
cron,
id).overwrite(true).build();
new SpringJobScheduler(instance, registryCenter, jobConfig, elasticJobListener).init();
}
public static final String CRON_DATE_FORMAT = "ss mm HH dd MM ? yyyy";
public static String getCron(final Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(CRON_DATE_FORMAT);
return simpleDateFormat.format(date);
}
}
3 application配置文件
server.port=1995
#动态定时任务
#zookeeper地址
zkserver=10.10.195.193:2181
# 任务命名空间
zknamespace=zknamesp
4 zookeeper注册中心配置
package com.yg.elasticjob.config;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.yg.elasticjob.elasticjob.ElasticJobListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticJobConfig {
@Value(value = "${zkserver}")
private String serverlists;
@Value("${zknamespace}")
private String namespace;
@Bean
public ZookeeperConfiguration zkConfig() {
return new ZookeeperConfiguration(serverlists, namespace);
}
@Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter(@Value("${zkserver}") final String serverList, @Value("${zknamespace}") final String namespace) {
ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(serverList, namespace);
return new ZookeeperRegistryCenter(zookeeperConfiguration);
}
5 实现SimpleJob
package com.yg.elasticjob.elasticjob;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
public class DynamicTask implements SimpleJob {
@Override
public void execute(ShardingContext shardingContext) {
String id = shardingContext.getJobParameter();
try {
System.out.println("执行你的逻辑代码!param:" + id + " jobName: " + shardingContext.getJobName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
6 使用Getmapping动态添加定时任务
package com.yg.elasticjob.controller;
import com.yg.elasticjob.elasticjob.DynamicTask;
import com.yg.elasticjob.elasticjob.ElasticJobHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.util.Date;
@RestController
@RequestMapping(value = "/test")
public class TestController {
@Autowired
ElasticJobHandler elasticJobHandler;
@GetMapping
public String add(@RequestHeader Long times, @RequestHeader String jobname, @RequestHeader String param) {
Date date = new Date(System.currentTimeMillis() + times);
String cron = "0/2 * * * * ?";
elasticJobHandler.addPublishJob(cron, param, jobname, new DynamicTask());
return "添加任务成功!";
}
}
7 任务启动类
package com.yg.elasticjob;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ElasticJobApplication {
public static void main(String[] args) {
SpringApplication.run(ElasticJobApplication.class, args);
}
}
8 项目整体目录
代码测试
1 添加第一个任务 2 添加第二个任务
总结
作为程序员第 112 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …
Lyric: 你说给过我纵容
|