日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java 项目初始化一个定时任务_elastic-job 分布式定时任务框架 在 SpringBoot 中如何使用(一)初始化任务并定时执行...

發(fā)布時間:2025/6/15 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 项目初始化一个定时任务_elastic-job 分布式定时任务框架 在 SpringBoot 中如何使用(一)初始化任务并定时执行... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

第一篇需要實現(xiàn)一個最簡單的需求:某個任務定時執(zhí)行,多臺機子只讓其中一臺機子執(zhí)行任務

一、安裝?分布式應用程序協(xié)調(diào)服務 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 配置代碼

三、作業(yè)代碼(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());

}

}

}

作業(yè)任務代碼

四、配置作業(yè)(直接將官方的配置代碼拿進自己的項目中)

/** 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();

}

}

作業(yè)配置代碼

五、最簡單的整合步驟就完成了,啟動應用(分別啟動三個進程),看見任務開始執(zhí)行了

最簡單的任務需求得到了滿足,定時執(zhí)行,多臺機子只讓其中一臺機子執(zhí)行任務

關于分片的數(shù)量設置,實際操作了一把

假如:有3個進程實例

并且elastic-job 設置

分片 1 時:3個進程只有1一個進程執(zhí)行1次任務,共執(zhí)行1次任務

分片 2 時:3個進程中的2個進程一共執(zhí)行2次任務,共執(zhí)行2次任務

分片 3 時:3個進程每個執(zhí)行1次任務,共執(zhí)行3次任務

分片 4 時:3個進程同時一共執(zhí)行4次任務,共執(zhí)行4次任務

分片 5 時:3個進程同時一共執(zhí)行5次任務,共執(zhí)行5次任務

分片為X時,此任務同時在Y臺個進程里面執(zhí)行X次。

這樣就可以根據(jù)不同的使用場景配置不同的分片數(shù)量了

總結

以上是生活随笔為你收集整理的java 项目初始化一个定时任务_elastic-job 分布式定时任务框架 在 SpringBoot 中如何使用(一)初始化任务并定时执行...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。