本篇文章会对Elastic-job与spring-·boot进行整合,同时搭建任务监控平台通过elastic-job-console以便对任务进行管理,至于涉及到的 分片sharding
及配置项等核心概念可以参考官网文档,本文主要是快速搭建工程,让定时任务先跑起来,文末附源码地址。
Elastic-job官网中文 elasticjob.io/index_zh.ht…
github地址 github.com/elasticjob/…
<dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-core</artifactId> <version>2.1.5</version> </dependency> 复制代码
<dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-spring</artifactId> <version>2.1.5</version> </dependency> 复制代码
package com.morning.morningshiro.jobs; import com.dangdang.ddframe.job.api.ShardingContext; import com.dangdang.ddframe.job.api.simple.SimpleJob; import com.morning.morningshiro.dao.entity.UserEntity; import javax.annotation.Resource; import java.util.List; public class MyElasticJob implements SimpleJob { @Override public void execute(ShardingContext context) { // 根据分片项执行任务 System.out.println(context.toString()); switch (context.getShardingItem()) { case 0: break; case 1: // do something by sharding item 1 break; case 2: // do something by sharding item 2 break; // case n: ... } } } 复制代码
package com.morning.morningshiro.jobs; import com.dangdang.ddframe.job.executor.ShardingContexts; import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener; public class JobListener implements ElasticJobListener { @Override public void beforeJobExecuted(ShardingContexts shardingContexts) { System.out.println(shardingContexts.toString()); } @Override public void afterJobExecuted(ShardingContexts shardingContexts) { System.out.println(shardingContexts.toString()); } } 复制代码
resources
文件下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:reg="http://www.dangdang.com/schema/ddframe/reg" xmlns:job="http://www.dangdang.com/schema/ddframe/job" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.dangdang.com/schema/ddframe/reg http://www.dangdang.com/schema/ddframe/reg/reg.xsd http://www.dangdang.com/schema/ddframe/job http://www.dangdang.com/schema/ddframe/job/job.xsd "> <!--配置作业注册中心 --> <!--namespace为注册到zk的名称--> <reg:zookeeper id="regCenter" server-lists="127.0.0.1:2181" namespace="dd-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3"/> <bean id="myElasticJob" class="com.morning.morningshiro.jobs.MyElasticJob"/> <!-- 配置作业--> <!--overwrite 修改后覆盖原有定时任务--> <!--id 任务ID--> <job:simple id="job1" job-ref="myElasticJob" overwrite="true" registry-center-ref="regCenter" cron="0/30 * * * * ?" sharding-total-count="4" sharding-item-parameters="0=USER,1=TWO" description="test"> <job:listener class="com.morning.morningshiro.jobs.JobListener"/> </job:simple> </beans> 复制代码
@SpringBootApplication @ImportResource(locations = {"classpath:spring-elastic-job.xml"}) public class MorningShiroApplication { public static void main(String[] args) { SpringApplication.run(MorningShiroApplication.class, args); } } 复制代码
通过elastic-job-console工程来进行监控任务执行, github.com/elasticjob/…
下载工程,导入Idea启动elastic-job-lite-console工程,启动之前修改父pom文件中的spring框架版本为 5.1.9.RELEASE
,然后启动,浏览器输入 localhost:8899
即可进入控制台。
4.1 控制台界面,配置zk服务地址
4.2 查看任务类表
4.3 查看服务列表
4.4 修改任务
关于zk集群搭建查看: juejin.im/post/5cee0d…
源码地址: github.com/alwyngo/mor…