SpringBoot集成ElaticJob定時(shí)器的實(shí)現(xiàn)代碼
本文介紹了SpringBoot集成ElaticJob定時(shí)器的實(shí)現(xiàn)代碼,分享給大家,具體如下:
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demojob</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demojob</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--elastic-job--> <dependency> <artifactId>elastic-job-common-core</artifactId> <groupId>com.dangdang</groupId> <version>2.1.5</version> </dependency> <dependency> <artifactId>elastic-job-lite-core</artifactId> <groupId>com.dangdang</groupId> <version>2.1.5</version> </dependency> <dependency> <artifactId>elastic-job-lite-spring</artifactId> <groupId>com.dangdang</groupId> <version>2.1.5</version> </dependency> <dependency> <artifactId>elastic-job-cloud-executor</artifactId> <groupId>com.dangdang</groupId> <version>2.1.5</version> </dependency> <!--mariadb--> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>1.5.4</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency> <!--mybatis plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatisplus-spring-boot-starter</artifactId> <version>1.0.5</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.1.9</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
yaml文件配置(也可以用application.properties一樣的)
# 配置配置數(shù)據(jù)源 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.mariadb.jdbc.Driver name: elastic-job-event url: jdbc:mariadb://127.0.0.1:3306/elasticjob username: root password: 123456 druid: validationQuery: SELECT 1 initialSize: 10 minIdle: 10 maxActive: 200 minEvictableIdleTimeMillis: 180000 testOnBorrow: false testWhileIdle: true removeAbandoned: true removeAbandonedTimeout: 1800 logAbandoned: true poolPreparedStatements: true maxOpenPreparedStatements: 100 # 配置Zookeeper regCenter: serverList: localhost:2181 namespace: hulk_order_task # 配置定時(shí)器規(guī)則 simpleJob: cron: 0/5 * * * * ? shardingTotalCount: 1 shardingItemParameters: 0=1
開(kāi)始寫(xiě)代碼
RegistryCenterConfig
package com.example.demojob.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; /** * 注冊(cè)中心配置 * 用于注冊(cè)和協(xié)調(diào)作業(yè)分布式行為的組件,目前僅支持Zookeeper。 * @author shudalei */ @Configuration @ConditionalOnExpression("'${regCenter.serverList}'.length() > 0") public class RegistryCenterConfig { @Bean(initMethod = "init") public ZookeeperRegistryCenter regCenter(@Value("${regCenter.serverList}") final String serverList, @Value("${regCenter.namespace}") final String namespace) { return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace)); } }
JobEventConfig
package com.example.demojob.config; import com.dangdang.ddframe.job.event.JobEventConfiguration; import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.annotation.Resource; import javax.sql.DataSource; /** * 如果想把作業(yè)運(yùn)行的內(nèi)容寫(xiě)到DB中,我們需要用到另一個(gè)構(gòu)造器, * 同時(shí)定義自己的JobEventConfiguration, * 目前來(lái)說(shuō)實(shí)現(xiàn)這個(gè)接口的只有一個(gè)類(lèi)JobEventRdbConfiguration, * 通過(guò)這個(gè)可以將作業(yè)運(yùn)行的痕跡進(jìn)行持久化到DB的操作。 * @author shudalei */ @Configuration public class JobEventConfig { @Resource private DataSource dataSource; @Bean public JobEventConfiguration jobEventConfiguration() { return new JobEventRdbConfiguration(dataSource); } }
SimpleJobConfig
package com.example.demojob.config; import com.dangdang.ddframe.job.config.JobCoreConfiguration; import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration; import com.dangdang.ddframe.job.event.JobEventConfiguration; import com.dangdang.ddframe.job.lite.api.JobScheduler; 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 com.example.demojob.job.TestSimpleJob; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.annotation.Resource; @Configuration public class SimpleJobConfig { /** * 注冊(cè)中心 */ @Resource private ZookeeperRegistryCenter regCenter; /** * job事件配置 */ @Resource private JobEventConfiguration jobEventConfiguration; /** * 微信access token獲取任務(wù)對(duì)象 * */ @Resource private TestSimpleJob simpleJob; /** * * @param cron 定時(shí)任務(wù)cron配置 * @param shardingTotalCount 任務(wù)分片數(shù) * @param shardingItemParameters 任務(wù)分片參數(shù) * @return JobScheduler 任務(wù)調(diào)度器 */ @Bean(initMethod = "init") public JobScheduler simpleJobScheduler(@Value("${simpleJob.cron}") final String cron, @Value("${simpleJob.shardingTotalCount}") final int shardingTotalCount, @Value("${simpleJob.shardingItemParameters}") final String shardingItemParameters) { return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters), jobEventConfiguration); } /** * * @param jobClass 任務(wù)調(diào)度類(lèi) * @param cron 定時(shí)任務(wù)cron配置 * @param shardingTotalCount 任務(wù)分片數(shù) * @param shardingItemParameters 任務(wù)分片參數(shù) * @return LiteJobConfiguration 任務(wù)配置 */ private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends com.dangdang.ddframe.job.api.simple.SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) { return LiteJobConfiguration .newBuilder( new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount) .shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName())) .overwrite(true).build(); } }
TestSimpleJob,定時(shí)器任務(wù)本身
package com.example.demojob.job; import com.dangdang.ddframe.job.api.ShardingContext; import com.dangdang.ddframe.job.api.simple.SimpleJob; import org.springframework.stereotype.Component; @Component public class TestSimpleJob implements SimpleJob { private int count; //任務(wù)就是每5秒執(zhí)行一次控制臺(tái)輸出1,2,3…… @Override public void execute(ShardingContext shardingContext) { count++; System.out.println("task " + count); } }
最后在Docker下安裝 Zookeeper
安裝腳本compose文件如下
version: '2' services: zookeeper01: image: zookeeper restart: always hostname: zookeeper01 ports: - 2181:2181 environment: ZOO_MY_ID: 1 ZOO_SERVERS: server.1=0.0.0.0:2888:3888 server.2=zookeeper02:2888:3888 server.3=zookeeper03:2888:3888 zookeeper02: image: zookeeper restart: always hostname: zookeeper02 ports: - 2182:2181 environment: ZOO_MY_ID: 2 ZOO_SERVERS: server.1=zookeeper01:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zookeeper03:2888:3888 zookeeper03: image: zookeeper restart: always hostname: zookeeper03 ports: - 2183:2181 environment: ZOO_MY_ID: 3 ZOO_SERVERS: server.1=zookeeper01:2888:3888 server.2=zookeeper02:2888:3888 server.3=0.0.0.0:2888:3888
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot+@EnableScheduling使用定時(shí)器的常見(jiàn)案例
- SpringBoot簡(jiǎn)單實(shí)現(xiàn)定時(shí)器過(guò)程
- Springboot webscoket自定義定時(shí)器
- springboot使用定時(shí)器@Scheduled不管用的解決
- 使用springboot時(shí),解決@Scheduled定時(shí)器遇到的問(wèn)題
- SpringBoot開(kāi)發(fā)實(shí)戰(zhàn)系列之定時(shí)器
- Springboot集成定時(shí)器和多線程異步處理操作
- SpringBoot 動(dòng)態(tài)定時(shí)器的使用方法
- SpringBoot項(xiàng)目中定時(shí)器的實(shí)現(xiàn)示例
相關(guān)文章
SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn)
MyBatisFlex是一款優(yōu)秀的持久層框架,本文主要介紹了SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06java開(kāi)發(fā)的工廠方法模式及抽象工廠驗(yàn)證示例
這篇文章主要為大家介紹了java開(kāi)發(fā)中的工廠方法模式以及抽象工廠的驗(yàn)證示例,有需要的朋友可以借鑒參考下希望能夠有所幫助祝大家多多進(jìn)步2021-10-10java分頁(yè)攔截類(lèi)實(shí)現(xiàn)sql自動(dòng)分頁(yè)
這篇文章主要為大家詳細(xì)介紹了java分頁(yè)攔截類(lèi)可以實(shí)現(xiàn)sql自動(dòng)分頁(yè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Spring源碼解析之循環(huán)依賴(lài)的實(shí)現(xiàn)流程
這篇文章主要介紹了Spring源碼解析之循環(huán)依賴(lài)的實(shí)現(xiàn)流程,文章基于Java的相關(guān)內(nèi)容展開(kāi)循環(huán)依賴(lài)的實(shí)現(xiàn)流程,需要的小伙伴可以參考一下2022-07-07springboot整合JavaCV實(shí)現(xiàn)視頻截取第N幀并保存圖片
這篇文章主要為大家詳細(xì)介紹了springboot如何整合JavaCV實(shí)現(xiàn)視頻截取第N幀并保存為圖片,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-08-08如何利用Spring?MVC實(shí)現(xiàn)RESTful風(fēng)格
這篇文章主要介紹了如何利用Spring?MVC實(shí)現(xiàn)RESTful風(fēng)格,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02