Elastic Job入门示例
Elastic Job官网:http://elasticjob.io/index_zh.html
示例:Spring Boot + Elastic Job 实现一个simple任务类型
1.maven 导入相关的包
<!--Begin Elastic Job --><dependency><groupId>com.dangdang</groupId><artifactId>elastic-job-lite-core</artifactId><version>2.1.5</version><exclusions><exclusion><groupId>org.apache.curator</groupId><artifactId>curator-client</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.dangdang</groupId><artifactId>elastic-job-lite-spring</artifactId><version>2.1.5</version><exclusions><exclusion><groupId>org.apache.curator</groupId><artifactId>curator-client</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-client</artifactId><version>2.11.1</version></dependency><!--End Elastic Job -->
2.相关的配置说明
RegistryCenterConfig:连接注册中心相关的配置
JobEventConfig: Event持久到DB的相关配置
SimpleJobConfig:实现SimpleJob接口的相关配置
如下图:
3.配置代码
@Configuration
@ConditionalOnExpression("'${job.zookeeper.center.serverList}'.length() > 0")
public class RegistryCenterConfig {/**** @param serverList zookeeper 服务地址列表* @param namespace 业务的命名空间,全局唯一* @return*/@Bean(initMethod = "init")public ZookeeperRegistryCenter regCenter(@Value("${job.zookeeper.center.serverList}") final String serverList,@Value("${job.zookeeper.center.namespace}") final String namespace) {return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));}}
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;@Configuration
public class JobEventConfig {@Resourceprivate DataSource dataSource;@Beanpublic JobEventConfiguration jobEventConfiguration(){return new JobEventRdbConfiguration(dataSource);}}
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
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.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;/*** Simple类型作业* 需实现SimpleJob接口。该接口仅提供单一方法用于覆盖,此方法将定时执行。* 与Quartz原生接口相似,但提供了弹性扩缩容和分片等功能**/
public abstract class SimpleJobConfig implements SimpleJob {@Resourceprotected ZookeeperRegistryCenter regCenter;@Resourceprotected JobEventConfiguration jobEventConfiguration;/*** 作业启动时间的cron表达式** @return*/abstract protected String getCron();/*** 作业分片总数* 子类根据业务需要重写此方法** @return*/protected int getShardingTotalCount(){return 1;//default 1}/*** 设置分片序列号和个性化参数对照表.* 子类根据需要重写此方法** <p>* 分片序列号和参数用等号分隔, 多个键值对用逗号分隔. 类似map. 分片序列号从0开始, 不可大于或等于作业分片总数. 如: 0=a,1=b,2=c* </p>** @return*/protected String getShardingItemParameters(){return "";}/*** 获取执行任务实例的Class* @return* example:DemoTask.Class;*/abstract protected Class getJobClass();/**** @param jobClass* @param cron* @param shardingTotalCount* @param shardingItemParameters* @return*/protected LiteJobConfiguration getLiteJobConfiguration(final Class<? extends 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();}@PostConstructpublic void simpleJobScheduler() {new SpringJobScheduler(this,regCenter,getLiteJobConfiguration(this.getJobClass(), getCron(), getShardingTotalCount(), getShardingItemParameters()),jobEventConfiguration).init();}}
4. Task样例类
import com.alibaba.fastjson.JSON;
import com.dangdang.ddframe.job.api.ShardingContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;/*** 简单任务的示例*/
@Configuration("DemoTask")
public class DemoTask extends SimpleJobConfig {@Value("${DemoTaskJob.cron}")private String cronValue;@Overrideprotected String getCron() {return cronValue;
// return "0 0/1 * * * ?";}@Overrideprotected Class getJobClass() {return DemoTask.class;}@Overridepublic void execute(ShardingContext shardingContext) {String execTime=""+System.currentTimeMillis();System.out.println("#####Begin DemoTask executed:"+execTime+"###########################################"+JSON.toJSONString(shardingContext));try {Thread.sleep(1000*60*3);}catch (Exception ex){ex.printStackTrace();}System.out.println("#####End DemoTask executed:"+execTime+", finished time:"+System.currentTimeMillis()+"###########################################"+JSON.toJSONString(shardingContext));}}
5.配置文件说明
#zk config
job.zookeeper.center.serverList=127.0.0.1:2181
job.zookeeper.center.namespace=job-test#Demo task schedule time
DemoTaskJob.cron=0 0/1 * * * ?
6.启动类
@EnableEurekaClient
@SpringBootApplication
@MapperScan("com.xxx.service.job.mapper")
@EnableScheduling
@PropertySource(value = {"classpath:business.properties"},encoding="utf-8")
public class Application extends SpringBootServletInitializer {public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application){return application.sources(Application.class);}}
7.总结:
a.简单任务的实现,相关概念与参数的介绍后面在分析
b.查看zookeeper上的相关信息,可借ZooInspector工具
Elastic Job入门示例相关推荐
- Elastic search入门到集群实战操作详解(原生API操作、springboot整合操作)-step1
Elastic search入门到集群实战操作详解(原生API操作.springboot整合操作)-step2 https://blog.csdn.net/qq_45441466/article/de ...
- [WCF编程]1.WCF入门示例
一.WCF是什么? Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,整合了原有的windows通讯的 .net Remotin ...
- BizTalk 2006 简单入门示例程序(附源项目文件下载)
BizTalk 2006 简单入门示例程序(附源项目文件下载) 为初学BizTalk Server 2006的开发人员,提供一个简单入门的示例程序,包括一个Receive Port.Send Port ...
- Apache Camel框架入门示例
2019独角兽企业重金招聘Python工程师标准>>> Apache Camel是Apache基金会下的一个开源项目,它是一个基于规则路由和中介引擎,提供企业集成模式的Java对象的 ...
- OUYA游戏开发核心技术剖析OUYA游戏入门示例——StarterKit
第1章 OUYA游戏入门示例--StarterKit StarterKit是一个多场景的游戏示例,也是OUYA官方推荐给入门开发者分析的第一个完整游戏示例.本章会对StarterKit做详细介绍,包 ...
- python爬虫入门实例-终于领会python爬虫入门示例
随着人工智能 大数据的火热 Python成为了广大科学家和普通大众的学习语言.在学习Python的过程中 有很多人感到迷茫 不知道自己该从什么地方入手,今天我们就来说一些新手该如何学习Python编程 ...
- 【Unity 3D 游戏开发】Unity3D 入门 - 工作区域介绍 与 入门示例
一. 工作区域详解 1. Scence视图 (场景设计面板) scence视图简介 : 展示创建的游戏对象, 可以对所有的游戏对象进行 移动, 操作 和 放置; -- 示例 : 创建一个球体, 控制摄 ...
- Castle Active Record for .NET2.0快速入门示例
一.创建Web工程 创建一个Web站点或者Web应用程序,添加对Castle.ActiveRecord.dll的引用. 二.创建需要持久化的业务实体 在.NET2.0下,由于引入了泛型,创建业务实体比 ...
- Spring MVC 入门示例讲解
在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...
最新文章
- js的下拉框事件之onchange
- python ocr中文识别库 tesseract安装及问题处理
- 【天池赛事】零基础入门语义分割-地表建筑物识别 Task2:数据扩增方法
- 计算机仿真技术-基于matlab的电子信息类课程课后答案,计算机仿真技术:基于MATLAB的电子信息类课程(第4版)...
- 解决 springboot + JPA + MySQL 表名全大写 出现 “表不存在” 问题(Table ‘XXX.xxx‘ doesn‘t exist)
- win10系统用户:如何获得超级管理员权限(vue开发之Win10踩坑)
- 恐龙快跑小程序对接流量主源码+前端 v5.0.4 全开源微擎框架
- Spring学习笔记专题一
- APP 代码提交GitHub: 提交、合并与冲突解决 (终端操作语法)
- Hibernate - Query简易
- 硕士学位论文(2022年) Latex模板 模板修改记录 总结
- java编程中常见的拼写错误
- Pwn level题目
- 网络工程师笔记--广域网和接入网
- Zookeeper -选举流程
- 音频信号输入itc服务器,音频信号的两种传输方式,你知多少?
- Cortex-A8处理器编程(上)
- Centos系统修改为静态ip
- 深度分析:云控系统有什么功能,工作室用的话咋样?
- js继承的六种方式详解--认真看完你就会了