javascript
java 项目初始化一个定时任务_elastic-job 分布式定时任务框架 在 SpringBoot 中如何使用(一)初始化任务并定时执行...
第一篇需要實現一個最簡單的需求:某個任務定時執行,多臺機子只讓其中一臺機子執行任務
一、安裝?分布式應用程序協調服務 zookeeper,安裝步驟在鏈接里面
二、在springboot項目中引入?elastic-job 依賴,我這里用的 springboot 2.0.5 版本
整合代碼參考官方的springboot demo
對應的??elastic-job 版本
2.1.5
com.dangdang
elastic-job-lite-core
${elastic-job.version}
com.dangdang
elastic-job-lite-spring
${elastic-job.version}
依賴XML代碼
application.yaml 加入配置
regCenter:
serverList: localhost:2181
namespace: my-project
simpleJob:
cron: 0/15 * * * * ?
shardingTotalCount: 1
shardingItemParameters: 0=a
dataflowJob:
cron: 0/5 * * * * ?
shardingTotalCount: 3
shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou
application.yaml 配置代碼
三、作業代碼(simpleJob類型的任務),把官方的demo拿到自己的項目中來就行,注意添加一個@Component注解在類上
/** Copyright 1999-2015 dangdang.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*http://www.apache.org/licenses/LICENSE-2.0*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/packagecom.dianji.task_server.job.exec;importcom.dangdang.ddframe.job.api.ShardingContext;importcom.dangdang.ddframe.job.api.simple.SimpleJob;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Component;importjava.text.MessageFormat;importjava.text.SimpleDateFormat;importjava.util.Date;
@Slf4j
@Componentpublic class SpringSimpleJob implementsSimpleJob {
@Value("${flag}")privateString serverFlag;
@Overridepublic void execute(finalShardingContext shardingContext) {int shardingItem =shardingContext.getShardingItem();if (shardingItem == 0) {
log.info("這是來自『{}』的任務,SIMPLE「SpringSimpleJob_execute」", serverFlag + ":" +shardingContext.getShardingParameter());
String logStr= "Item: {0} | Parameter: {1} | : {2} | Time: {3} | Thread: {4} | {5}";
logStr=MessageFormat.format(logStr, shardingContext.getShardingItem(),
shardingContext.getShardingParameter(),new SimpleDateFormat("HH:mm:ss").format(newDate()),
Thread.currentThread().getId(),"SIMPLE「SpringSimpleJob_execute」");
log.info(logStr);
}else if (shardingItem == 1) {
log.info("這是來自『{}』的任務", serverFlag + ":" +shardingContext.getShardingParameter());
}else if (shardingItem == 2) {
log.info("這是來自『{}』的任務", shardingContext.getShardingParameter());
}
}
}
作業任務代碼
四、配置作業(直接將官方的配置代碼拿進自己的項目中)
/** Copyright 1999-2015 dangdang.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*http://www.apache.org/licenses/LICENSE-2.0*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/packagecom.dianji.task_server.job.config;importcom.dangdang.ddframe.job.api.simple.SimpleJob;importcom.dangdang.ddframe.job.config.JobCoreConfiguration;importcom.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;importcom.dangdang.ddframe.job.event.JobEventConfiguration;importcom.dangdang.ddframe.job.lite.api.JobScheduler;importcom.dangdang.ddframe.job.lite.config.LiteJobConfiguration;importcom.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;importcom.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;importcom.dianji.task_server.job.exec.SpringSimpleJob;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjavax.annotation.Resource;
@Configurationpublic classSimpleJobConfig {
@ResourceprivateZookeeperRegistryCenter regCenter;
@ResourceprivateJobEventConfiguration jobEventConfiguration;
@BeanpublicSimpleJob simpleJob() {return newSpringSimpleJob();
}
@Bean(initMethod= "init")public JobScheduler simpleJobScheduler(final SimpleJob simpleJob, @Value("${simpleJob.cron}") final String cron, @Value("${simpleJob.shardingTotalCount}") final intshardingTotalCount,
@Value("${simpleJob.shardingItemParameters}") finalString shardingItemParameters) {return newSpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters), jobEventConfiguration);
}private LiteJobConfiguration getLiteJobConfiguration(final Class extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, finalString shardingItemParameters) {return LiteJobConfiguration.newBuilder(newSimpleJobConfiguration(JobCoreConfiguration.newBuilder(
jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName())).overwrite(true).build();
}
}
作業配置代碼
五、最簡單的整合步驟就完成了,啟動應用(分別啟動三個進程),看見任務開始執行了
最簡單的任務需求得到了滿足,定時執行,多臺機子只讓其中一臺機子執行任務
關于分片的數量設置,實際操作了一把
假如:有3個進程實例
并且elastic-job 設置
分片 1 時:3個進程只有1一個進程執行1次任務,共執行1次任務
分片 2 時:3個進程中的2個進程一共執行2次任務,共執行2次任務
分片 3 時:3個進程每個執行1次任務,共執行3次任務
分片 4 時:3個進程同時一共執行4次任務,共執行4次任務
分片 5 時:3個進程同時一共執行5次任務,共執行5次任務
分片為X時,此任務同時在Y臺個進程里面執行X次。
這樣就可以根據不同的使用場景配置不同的分片數量了
總結
以上是生活随笔為你收集整理的java 项目初始化一个定时任务_elastic-job 分布式定时任务框架 在 SpringBoot 中如何使用(一)初始化任务并定时执行...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java程序的运行结果依赖操作系统吗_j
- 下一篇: java创建datetime_Java