引入pom文件

  1.      <dependency>
  2. <groupId>com.dangdang</groupId>
  3. <artifactId>elastic-job-lite-core</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.dangdang</groupId>
  7. <artifactId>elastic-job-lite-spring</artifactId>
  8. </dependency>

yml文件配置

  1. regCenter:
  2. serverList: localhost:6181
  3. namespace: elastic-job-lite-springboot
  4.  
  5. simpleJob:
  6. cron: 0/5 * * * * ?
  7. shardingTotalCount: 3
  8. shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou
  9.  
  10. dataflowJob:
  11. cron: 0/5 * * * * ?
  12. shardingTotalCount: 3
  13. shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou

编写Job

  1. public class SpringSimpleJob implements SimpleJob {
  2.  
  3. @Resource
  4. private FooRepository fooRepository;
  5.  
  6. @Override
  7. public void execute(final ShardingContext shardingContext) {
  8. System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
  9. shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "SIMPLE"));
  10. List<Foo> data = fooRepository.findTodoData(shardingContext.getShardingParameter(), 10);
  11. for (Foo each : data) {
  12. fooRepository.setCompleted(each.getId());
  13. }
  14. }
  15. }
  1. public class SpringDataflowJob implements DataflowJob<Foo> {
  2.  
  3. @Resource
  4. private FooRepository fooRepository;
  5.  
  6. @Override
  7. public List<Foo> fetchData(final ShardingContext shardingContext) {
  8. System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
  9. shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW FETCH"));
  10. return fooRepository.findTodoData(shardingContext.getShardingParameter(), 10);
  11. }
  12.  
  13. @Override
  14. public void processData(final ShardingContext shardingContext, final List<Foo> data) {
  15. System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
  16. shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW PROCESS"));
  17. for (Foo each : data) {
  18. fooRepository.setCompleted(each.getId());
  19. }
  20. }
  21. }

配置ZooKeeper

  1. @Configuration
  2. @ConditionalOnExpression("'${regCenter.serverList}'.length() > 0")
  3. public class RegistryCenterConfig {
  4.  
  5. @Bean(initMethod = "init")
  6. public ZookeeperRegistryCenter regCenter(@Value("${regCenter.serverList}") final String serverList, @Value("${regCenter.namespace}") final String namespace) {
  7. return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
  8. }
  9. }

配置作业状态监听

  1. @Configuration
  2. public class JobEventConfig {
  3.  
  4. @Resource
  5. private DataSource dataSource;
  6.  
  7. @Bean
  8. public JobEventConfiguration jobEventConfiguration() {
  9. return new JobEventRdbConfiguration(dataSource);
  10. }
  11. }

配置Job

  1. @Configuration
  2. public class SimpleJobConfig {
  3.  
  4. @Resource
  5. private ZookeeperRegistryCenter regCenter;
  6.  
  7. @Resource
  8. private JobEventConfiguration jobEventConfiguration;
  9.  
  10. @Bean
  11. public SimpleJob simpleJob() {
  12. return new SpringSimpleJob();
  13. }
  14.  
  15. @Bean(initMethod = "init")
  16. public JobScheduler simpleJobScheduler(final SimpleJob simpleJob, @Value("${simpleJob.cron}") final String cron, @Value("${simpleJob.shardingTotalCount}") final int shardingTotalCount,
  17. @Value("${simpleJob.shardingItemParameters}") final String shardingItemParameters) {
  18. return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters), jobEventConfiguration);
  19. }
  20.  
  21. private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
  22. return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(
  23. jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName())).overwrite(true).build();
  24. }
  25. }
  1. @Configuration
  2. public class DataflowJobConfig {
  3.  
  4. @Resource
  5. private ZookeeperRegistryCenter regCenter;
  6.  
  7. @Resource
  8. private JobEventConfiguration jobEventConfiguration;
  9.  
  10. @Bean
  11. public DataflowJob dataflowJob() {
  12. return new SpringDataflowJob();
  13. }
  14.  
  15. @Bean(initMethod = "init")
  16. public JobScheduler dataflowJobScheduler(final DataflowJob dataflowJob, @Value("${dataflowJob.cron}") final String cron, @Value("${dataflowJob.shardingTotalCount}") final int shardingTotalCount,
  17. @Value("${dataflowJob.shardingItemParameters}") final String shardingItemParameters) {
  18. return new SpringJobScheduler(dataflowJob, regCenter, getLiteJobConfiguration(dataflowJob.getClass(), cron, shardingTotalCount, shardingItemParameters), jobEventConfiguration);
  19. }
  20.  
  21. private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends DataflowJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters) {
  22. return LiteJobConfiguration.newBuilder(new DataflowJobConfiguration(JobCoreConfiguration.newBuilder(
  23. jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName(), true)).overwrite(true).build();
  24. }
  25. }

 更多使用方法,可以参照官方包里边的example案例。

Elastic Job入门(3) - 集成Springboot的更多相关文章

  1. 实战:docker搭建FastDFS文件系统并集成SpringBoot

    实战:docker搭建FastDFS文件系统并集成SpringBoot 前言 15年的时候,那时候云存储还远远没有现在使用的这么广泛,归根结底就是成本和安全问题,记得那时候我待的公司是做建站开发的,前 ...

  2. Spring Maven项目集成Springboot

    Maven管理的Spring项目,准备集成Springboot做接口 1.Springboot对Spring有版本要求 我用的Springboot版本:1.4.5.RELEASE,对应Spring的版 ...

  3. ELK入门使用-与springboot集成

    前言 ELK官方的中文文档写的已经挺好了,为啥还要记录本文?因为我发现,我如果不写下来,过几天就忘记了,而再次捡起来必然还要经历资料查找筛选测试的过程.虽然这个过程很有意义,但并不总是有那么多时间去做 ...

  4. SpringBoot入门之集成Druid

    Druid:为监控而生的数据库连接池.这篇先了解下它的简单使用,下篇尝试用它做多数据源配置.主要参考:https://github.com/alibaba/druid/wiki/常见问题 https: ...

  5. SpringBoot入门之集成JSP

    原本打算这篇继续写thymeleaf方面的内容,一看内容还挺多的,可能一周也写不完,而且从Controller获取值等内容也都能从网上百度,所以就写了springboot集成jsp.不管thymele ...

  6. netty集成springboot

    一 前言 springboot 如何集成netty实现mapper调用不为null的问题让好多读者都头疼过,知识追寻者发了一点时间做了个基本入门集成应用给读者们指明条正确的集成方式,我相信,只要你有n ...

  7. 入门到熟练-SpringBoot

    Spring Boot概述 1.1. Spring Boot是什么 Spring Boot是一套基于Spring框架的微服务框架. 1.2. Spring Boot框架出现的背景 由于Spring是一 ...

  8. 集成Springboot+MyBatis+JPA

    1.前言 Springboot最近可谓是非常的火,本人也在项目中尝到了甜头.之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Spri ...

  9. 5.1 入门整合案例(SpringBoot+Spring-data-elasticsearch) ---- good

    本节讲解SpringBoot与Spring-data-elasticsearch整合的入门案例. 一.环境搭建 新建maven项目,名字随意 pom.xml <parent> <gr ...

随机推荐

  1. async中await是干啥的,用不用有什么区别?

    最近在研究异步编程,用的async await task啥的,但是都这几个概念很模糊,还有不太清楚await是干啥的,task又是干啥的,用不用await有什么区别,他们三个之间的联系是什么? tas ...

  2. iOS 页面之间的转场动画控制器间的转换

    CATransition类实现层的转场动画.你可以从一组预定义的转换或者通过提供定制的CIFilter实例来指定转场效果. 例如:控制器之间的跳转 LoginViewController *myVC ...

  3. nginx-匹配规则

    location 指令的作用是根据用户请求的URI来执行不同的应用. locationn使用的语法为 location [=|~|~*|^~] uri { .... } location 语法说明表 ...

  4. ubuntu 16.04 samba服务搭建

    一:安装 1. sudo apt-get install samba 有询问Yes的地方Yes就行. 无法安装samba 执行 sudo apt-get update 2.等待安装完成,进入配置文件目 ...

  5. LOJ2540 [PKUWC2018] 随机算法 【状压DP】

    题目分析: 听说这题考场上能被$ O(4^n) $的暴力水过,难不成出题人是毕姥爷? 首先思考一个显而易见的$ O(n^2*2^n) $的暴力DP.一般的DP都是考虑最近的加入了哪个点,然后删除后递归 ...

  6. Catenyms POJ - 2337(单词+字典序输出路径)

    题意: 就是给出几个单词 看能否组成欧拉回路或路径  当然还是让输出组成的最小字典序的路 解析: 还是把首尾字母看成点   把单词看成边 记录边就好了 这题让我对fleury输出最小字典序又加深了一些 ...

  7. poj1753 【枚举】

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...

  8. 一个死循环导致的栈溢出实例:StackOverFlowError

    有一个功能,要用复选框组做成单选框效果,如果有三个复选框 CheckBox ,并且保证每次只能选中一个.刚开始添加了以下的值改变后的监听方法 addValueChangeListener ,却导致了栈 ...

  9. c# Point不能输入小数

    换成用  PointF PointF p = new PointF(116.305671f, 39.966051f);  //6位小数后面要加f   表示转float,否则报错

  10. HGOI 20190217 题解

    /* for me,开训第一天 /beacuse 文化课太差被抓去补文化课了... 看一眼题 : AK局? 但是,Wa on test #10 in problem C 290! (就差那么一咪咪) ...