@ElasticSimpleJob(cron = “* * * * * ?”, jobName = “test123”, shardingTotalCount = 2, jobParameter = “测试参数”, shardingItemParameters = “0=A,1=B”)
@Component
public class MySimpleJob implements com.dangdang.ddframe.job.api.simple.SimpleJob {
@Override
public void execute(ShardingContext shardingContext) {
System.out.println(String.format("------Thread ID: %s, 任务总片数: %s, " +
“当前分片项: %s.当前参数: %s,” +
“当前任务名称: %s.当前任务参数: %s”
,
Thread.currentThread().getId(),
shardingContext.getShardingTotalCount(),
shardingContext.getShardingItem(),
shardingContext.getShardingParameter(),
shardingContext.getJobName(),
shardingContext.getJobParameter()
));
}
}
4. 启动2个不同端口,查看执行结果
执行: java -jar xxx.jar --server.port=8081
------Thread ID: 83, 任务总片数: 2, 当前分片项: 0.当前参数: A,当前任务名称: com.willow.elasticJob.MySimpleJob.当前任务参数:
------Thread ID: 84, 任务总片数: 2, 当前分片项: 1.当前参数: B,当前任务名称: com.willow.elasticJob.MySimpleJob.当前任务参数:
------Thread ID: 89, 任务总片数: 2, 当前分片项: 0.当前参数: A,当前任务名称: com.willow.elasticJob.MySimpleJob.当前任务参数:
------Thread ID: 90, 任务总片数: 2, 当前分片项: 1.当前参数: B,当前任务名称: com.willow.elasticJob.MySimpleJob.当前任务参数:
1.2动态添加elastic-job任务
当前暂未解决的问题: 动态添加的任务只能在添加的机器上运行,平行部署的其他机器上不会运行该任务
在上边配置的基础上添加以下配置:
1. 添加zookeeper配置类,和动态添加方法
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.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
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(“ e l a t i c j o b . z o o k e e p e r . s e r v e r − l i s t s " ) f i n a l S t r i n g s e r v e r L i s t , @ V a l u e ( " {elaticjob.zookeeper.server-lists}") final String serverList, @Value(" elaticjob.zookeeper.server−lists")finalStringserverList,@Value("{elaticjob.zookeeper.namespace}”) final String namespace) {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
}
@Autowired
private ZookeeperRegistryCenter regCenter;
/**
-
动态添加
-
@param jobClass
-
@param cron
-
@param shardingTotalCount
-
@param shardingItemParameters
*/
public void addSimpleJobScheduler(final Class<? extends SimpleJob> jobClass,
final String cron,
final int shardingTotalCount,
final String shardingItemParameters){
JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).jobParameter(“job参数”).build();
SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(coreConfig, jobClass.getCanonicalName());
JobScheduler jobScheduler = new JobScheduler(regCenter, LiteJobConfiguration.newBuilder(simpleJobConfig).build());
jobScheduler.init();
}
}
2. 动态添加任务逻辑
import com.willow.elasticJob.MySimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private ElasticJobConfig elasticJobConfig;
@RequestMapping(“/addJob”)
public void addJob() {
int shardingTotalCount = 2;
elasticJobConfig.addSimpleJobScheduler(new MySimpleJob().getClass(),“* * * * * ?”,shardingTotalCount,“0=A,1=B”);
}
}
二、Spring 集成 elastic-job
2.1添加POM依赖
com.dangdang
elastic-job-lite-core
2.1.5
com.dangdang
elastic-job-lite-spring
2.1.5
2.2添加配置
server.port=8766
spring.application.name=scheduler-service
zookeeper注册中心
spring.elasticjob.serverList = 192.168.7.108:2181
spring.elasticjob.namespace = elastic-job-lite-springboot
stockJob.cron = 0/5 * * * * ?
stockJob.shardingTotalCount = 2
stockJob.shardingItemParameters = 0=Chengdu0,1=Chengdu1
2.3 添加zookeeper注册中心
package com.willow.config;
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.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnExpression(“‘${spring.elasticjob.serverList}’.length() > 0”) //判断是否配置了zookeeper 地址
public class JobRegistryCenterConfig {
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
最后
作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料
1711154697372)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-SYmS9yWq-1711154697372)]
最后
作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料
[外链图片转存中…(img-nLZyTP0v-1711154697373)]
[外链图片转存中…(img-ZNrswt2h-1711154697373)]