spring-boot整合elastic-job笔记
1. 添加依赖【在此只列出额外需要添加的elastic-job依赖的jar】
<dependency>
<groupId>com.github.kuhn-he</groupId>
<artifactId>elastic-job-lite-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
2. 添加相应配置项
elaticjob.zookeeper.server-lists=10.140.6.18:2181,10.140.6.19:2181
elaticjob.zookeeper.namespace=my-project
3. 创建定时任务
import com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob;
import org.springframework.stereotype.Component;
import com.dangdang.ddframe.job.api.ShardingContext;
@ElasticSimpleJob(cron="* * * * * ?",jobName="test123",shardingTotalCount=2,jobParameter="测试参数",shardingItemParameters="0=A,1=B")
@Component
public class MyJob implements com.dangdang.ddframe.job.api.simple.SimpleJob {
@Override
public void execute(ShardingContext content) {
//do something
System.out.println("JobName:"+content.getJobName());
System.out.println("JobParameter:"+content.getJobParameter());
System.out.println("ShardingItem:"+content.getShardingItem());
System.out.println("ShardingParameter:"+content.getShardingParameter());
System.out.println("ShardingTotalCount:"+content.getShardingTotalCount());
System.out.println("TaskId:"+content.getTaskId());
System.out.println("---------------------------------------");
}
}
4. 执行结果
从测试结果看:jobName设置无效,jobParameter拿不到,这两个问题暂时没有解决还需研究
---------------------------------------
JobName:com.lenovo.job.MyJob
JobParameter:
ShardingItem:1
ShardingParameter:B
ShardingTotalCount:2
TaskId:com.lenovo.job.MyJob@-@0,1@-@READY@-@10.250.129.181@-@6024
---------------------------------------
JobName:com.lenovo.job.MyJob
JobParameter:
ShardingItem:0
ShardingParameter:A
ShardingTotalCount:2
TaskId:com.lenovo.job.MyJob@-@0,1@-@READY@-@10.250.129.181@-@6024
---------------------------------------
动态添加elastic-job任务
当前暂未解决的问题: 动态添加的任务只能在添加的机器上运行,平行部署的其他机器上不会运行该任务
在上边配置的基础上添加以下配置:
1. 添加zookeeper配置类
package com.lenovo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
@Configuration
public class ElasticJobConfig {
@Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter(@Value("${elaticjob.zookeeper.server-lists}") final String serverList, @Value("${elaticjob.zookeeper.namespace}") final String namespace) {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
}
}
2. 动态添加任务逻辑
package com.lenovo.controller;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.lenovo.job.MyJob;
@RestController
public class TestController {
@Autowired
private ZookeeperRegistryCenter zookeeperRegistryCenter;
@RequestMapping("/test")
public void test() {
int shardingTotalCount = 2;
String jobName = UUID.randomUUID().toString() + "-test123";
JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder(jobName, "* * * * * ?", shardingTotalCount).shardingItemParameters("0=A,1=B").build();
SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, MyJob.class.getCanonicalName());
JobScheduler jobScheduler = new JobScheduler(zookeeperRegistryCenter, LiteJobConfiguration.newBuilder(simpleJobConfiguration).build());
try {
jobScheduler.init();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("定时任务创建失败");
}
}
}