SpringBoot集成ElaticJob定时器的实现代码
本文介绍了SpringBoot集成ElaticJob定时器的实现代码,分享给大家,具体如下:
POM文件配置
4.0.0 com.example demojob 0.0.1-SNAPSHOT jar demojob DemoprojectforSpringBoot org.springframework.boot spring-boot-starter-parent 2.0.2.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test elastic-job-common-core com.dangdang 2.1.5 elastic-job-lite-core com.dangdang 2.1.5 elastic-job-lite-spring com.dangdang 2.1.5 elastic-job-cloud-executor com.dangdang 2.1.5 org.mariadb.jdbc mariadb-java-client 1.5.4 com.alibaba druid-spring-boot-starter 1.1.9 com.baomidou mybatisplus-spring-boot-starter 1.0.5 com.baomidou mybatis-plus 2.1.9 org.springframework.boot spring-boot-maven-plugin
yaml文件配置(也可以用application.properties一样的)
#配置配置数据源 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:SELECT1 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 #配置定时器规则 simpleJob: cron:0/5****? shardingTotalCount:1 shardingItemParameters:0=1
开始写代码
RegistryCenterConfig
packagecom.example.demojob.config; importcom.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration; importcom.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.boot.autoconfigure.condition.ConditionalOnExpression; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; /** *注册中心配置 *用于注册和协调作业分布式行为的组件,目前仅支持Zookeeper。 *@authorshudalei */ @Configuration @ConditionalOnExpression("'${regCenter.serverList}'.length()>0") publicclassRegistryCenterConfig{ @Bean(initMethod="init") publicZookeeperRegistryCenterregCenter(@Value("${regCenter.serverList}")finalStringserverList, @Value("${regCenter.namespace}")finalStringnamespace){ returnnewZookeeperRegistryCenter(newZookeeperConfiguration(serverList,namespace)); } }
JobEventConfig
packagecom.example.demojob.config; importcom.dangdang.ddframe.job.event.JobEventConfiguration; importcom.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importjavax.annotation.Resource; importjavax.sql.DataSource; /** *如果想把作业运行的内容写到DB中,我们需要用到另一个构造器, *同时定义自己的JobEventConfiguration, *目前来说实现这个接口的只有一个类JobEventRdbConfiguration, *通过这个可以将作业运行的痕迹进行持久化到DB的操作。 *@authorshudalei */ @Configuration publicclassJobEventConfig{ @Resource privateDataSourcedataSource; @Bean publicJobEventConfigurationjobEventConfiguration(){ returnnewJobEventRdbConfiguration(dataSource); } }
SimpleJobConfig
packagecom.example.demojob.config; 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.example.demojob.job.TestSimpleJob; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importjavax.annotation.Resource; @Configuration publicclassSimpleJobConfig{ /** *注册中心 */ @Resource privateZookeeperRegistryCenterregCenter; /** *job事件配置 */ @Resource privateJobEventConfigurationjobEventConfiguration; /** *微信accesstoken获取任务对象 * */ @Resource privateTestSimpleJobsimpleJob; /** * *@paramcron定时任务cron配置 *@paramshardingTotalCount任务分片数 *@paramshardingItemParameters任务分片参数 *@returnJobScheduler任务调度器 */ @Bean(initMethod="init") publicJobSchedulersimpleJobScheduler(@Value("${simpleJob.cron}")finalStringcron, @Value("${simpleJob.shardingTotalCount}")finalintshardingTotalCount, @Value("${simpleJob.shardingItemParameters}")finalStringshardingItemParameters){ returnnewSpringJobScheduler(simpleJob,regCenter, getLiteJobConfiguration(simpleJob.getClass(),cron,shardingTotalCount,shardingItemParameters), jobEventConfiguration); } /** * *@paramjobClass任务调度类 *@paramcron定时任务cron配置 *@paramshardingTotalCount任务分片数 *@paramshardingItemParameters任务分片参数 *@returnLiteJobConfiguration任务配置 */ privateLiteJobConfigurationgetLiteJobConfiguration(finalClassjobClass,finalStringcron, finalintshardingTotalCount,finalStringshardingItemParameters){ returnLiteJobConfiguration .newBuilder( newSimpleJobConfiguration(JobCoreConfiguration.newBuilder(jobClass.getName(),cron,shardingTotalCount) .shardingItemParameters(shardingItemParameters).build(),jobClass.getCanonicalName())) .overwrite(true).build(); } }
TestSimpleJob,定时器任务本身
packagecom.example.demojob.job; importcom.dangdang.ddframe.job.api.ShardingContext; importcom.dangdang.ddframe.job.api.simple.SimpleJob; importorg.springframework.stereotype.Component; @Component publicclassTestSimpleJobimplementsSimpleJob{ privateintcount; //任务就是每5秒执行一次控制台输出1,2,3…… @Override publicvoidexecute(ShardingContextshardingContext){ 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:3888server.2=zookeeper02:2888:3888server.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:3888server.2=0.0.0.0:2888:3888server.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:3888server.2=zookeeper02:2888:3888server.3=0.0.0.0:2888:3888
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。