Elastic Job入门(3) - 集成Springboot
引入pom文件
- <dependency>
- <groupId>com.dangdang</groupId>
- <artifactId>elastic-job-lite-core</artifactId>
- </dependency>
- <dependency>
- <groupId>com.dangdang</groupId>
- <artifactId>elastic-job-lite-spring</artifactId>
- </dependency>
yml文件配置
- regCenter:
- serverList: localhost:6181
- namespace: elastic-job-lite-springboot
- simpleJob:
- cron: 0/5 * * * * ?
- shardingTotalCount: 3
- shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou
- dataflowJob:
- cron: 0/5 * * * * ?
- shardingTotalCount: 3
- shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou
编写Job
- public class SpringSimpleJob implements SimpleJob {
- @Resource
- private FooRepository fooRepository;
- @Override
- public void execute(final ShardingContext shardingContext) {
- System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
- shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "SIMPLE"));
- List<Foo> data = fooRepository.findTodoData(shardingContext.getShardingParameter(), 10);
- for (Foo each : data) {
- fooRepository.setCompleted(each.getId());
- }
- }
- }
- public class SpringDataflowJob implements DataflowJob<Foo> {
- @Resource
- private FooRepository fooRepository;
- @Override
- public List<Foo> fetchData(final ShardingContext shardingContext) {
- System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
- shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW FETCH"));
- return fooRepository.findTodoData(shardingContext.getShardingParameter(), 10);
- }
- @Override
- public void processData(final ShardingContext shardingContext, final List<Foo> data) {
- System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
- shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW PROCESS"));
- for (Foo each : data) {
- fooRepository.setCompleted(each.getId());
- }
- }
- }
配置ZooKeeper
- @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));
- }
- }
配置作业状态监听
- @Configuration
- public class JobEventConfig {
- @Resource
- private DataSource dataSource;
- @Bean
- public JobEventConfiguration jobEventConfiguration() {
- return new JobEventRdbConfiguration(dataSource);
- }
- }
配置Job
- @Configuration
- public class SimpleJobConfig {
- @Resource
- private ZookeeperRegistryCenter regCenter;
- @Resource
- private JobEventConfiguration jobEventConfiguration;
- @Bean
- public SimpleJob simpleJob() {
- return new SpringSimpleJob();
- }
- @Bean(initMethod = "init")
- public JobScheduler simpleJobScheduler(final SimpleJob simpleJob, @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);
- }
- private 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();
- }
- }
- @Configuration
- public class DataflowJobConfig {
- @Resource
- private ZookeeperRegistryCenter regCenter;
- @Resource
- private JobEventConfiguration jobEventConfiguration;
- @Bean
- public DataflowJob dataflowJob() {
- return new SpringDataflowJob();
- }
- @Bean(initMethod = "init")
- public JobScheduler dataflowJobScheduler(final DataflowJob dataflowJob, @Value("${dataflowJob.cron}") final String cron, @Value("${dataflowJob.shardingTotalCount}") final int shardingTotalCount,
- @Value("${dataflowJob.shardingItemParameters}") final String shardingItemParameters) {
- return new SpringJobScheduler(dataflowJob, regCenter, getLiteJobConfiguration(dataflowJob.getClass(), cron, shardingTotalCount, shardingItemParameters), jobEventConfiguration);
- }
- private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends DataflowJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
- return LiteJobConfiguration.newBuilder(new DataflowJobConfiguration(JobCoreConfiguration.newBuilder(
- jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName(), true)).overwrite(true).build();
- }
- }
更多使用方法,可以参照官方包里边的example案例。
Elastic Job入门(3) - 集成Springboot的更多相关文章
- 实战:docker搭建FastDFS文件系统并集成SpringBoot
实战:docker搭建FastDFS文件系统并集成SpringBoot 前言 15年的时候,那时候云存储还远远没有现在使用的这么广泛,归根结底就是成本和安全问题,记得那时候我待的公司是做建站开发的,前 ...
- Spring Maven项目集成Springboot
Maven管理的Spring项目,准备集成Springboot做接口 1.Springboot对Spring有版本要求 我用的Springboot版本:1.4.5.RELEASE,对应Spring的版 ...
- ELK入门使用-与springboot集成
前言 ELK官方的中文文档写的已经挺好了,为啥还要记录本文?因为我发现,我如果不写下来,过几天就忘记了,而再次捡起来必然还要经历资料查找筛选测试的过程.虽然这个过程很有意义,但并不总是有那么多时间去做 ...
- SpringBoot入门之集成Druid
Druid:为监控而生的数据库连接池.这篇先了解下它的简单使用,下篇尝试用它做多数据源配置.主要参考:https://github.com/alibaba/druid/wiki/常见问题 https: ...
- SpringBoot入门之集成JSP
原本打算这篇继续写thymeleaf方面的内容,一看内容还挺多的,可能一周也写不完,而且从Controller获取值等内容也都能从网上百度,所以就写了springboot集成jsp.不管thymele ...
- netty集成springboot
一 前言 springboot 如何集成netty实现mapper调用不为null的问题让好多读者都头疼过,知识追寻者发了一点时间做了个基本入门集成应用给读者们指明条正确的集成方式,我相信,只要你有n ...
- 入门到熟练-SpringBoot
Spring Boot概述 1.1. Spring Boot是什么 Spring Boot是一套基于Spring框架的微服务框架. 1.2. Spring Boot框架出现的背景 由于Spring是一 ...
- 集成Springboot+MyBatis+JPA
1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Spri ...
- 5.1 入门整合案例(SpringBoot+Spring-data-elasticsearch) ---- good
本节讲解SpringBoot与Spring-data-elasticsearch整合的入门案例. 一.环境搭建 新建maven项目,名字随意 pom.xml <parent> <gr ...
随机推荐
- async中await是干啥的,用不用有什么区别?
最近在研究异步编程,用的async await task啥的,但是都这几个概念很模糊,还有不太清楚await是干啥的,task又是干啥的,用不用await有什么区别,他们三个之间的联系是什么? tas ...
- iOS 页面之间的转场动画控制器间的转换
CATransition类实现层的转场动画.你可以从一组预定义的转换或者通过提供定制的CIFilter实例来指定转场效果. 例如:控制器之间的跳转 LoginViewController *myVC ...
- nginx-匹配规则
location 指令的作用是根据用户请求的URI来执行不同的应用. locationn使用的语法为 location [=|~|~*|^~] uri { .... } location 语法说明表 ...
- ubuntu 16.04 samba服务搭建
一:安装 1. sudo apt-get install samba 有询问Yes的地方Yes就行. 无法安装samba 执行 sudo apt-get update 2.等待安装完成,进入配置文件目 ...
- LOJ2540 [PKUWC2018] 随机算法 【状压DP】
题目分析: 听说这题考场上能被$ O(4^n) $的暴力水过,难不成出题人是毕姥爷? 首先思考一个显而易见的$ O(n^2*2^n) $的暴力DP.一般的DP都是考虑最近的加入了哪个点,然后删除后递归 ...
- Catenyms POJ - 2337(单词+字典序输出路径)
题意: 就是给出几个单词 看能否组成欧拉回路或路径 当然还是让输出组成的最小字典序的路 解析: 还是把首尾字母看成点 把单词看成边 记录边就好了 这题让我对fleury输出最小字典序又加深了一些 ...
- poj1753 【枚举】
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
- 一个死循环导致的栈溢出实例:StackOverFlowError
有一个功能,要用复选框组做成单选框效果,如果有三个复选框 CheckBox ,并且保证每次只能选中一个.刚开始添加了以下的值改变后的监听方法 addValueChangeListener ,却导致了栈 ...
- c# Point不能输入小数
换成用 PointF PointF p = new PointF(116.305671f, 39.966051f); //6位小数后面要加f 表示转float,否则报错
- HGOI 20190217 题解
/* for me,开训第一天 /beacuse 文化课太差被抓去补文化课了... 看一眼题 : AK局? 但是,Wa on test #10 in problem C 290! (就差那么一咪咪) ...