引入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的更多相关文章

  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. MySQL分区管理

    以下是我看MySQL官方文档的时候整理的笔记,仅作参考保留. RANGE,LIST分区管理 1:为未分区表创建分区 ; 2:删除某个分区的数据 ALTER TABLE tr DROP PARTITIO ...

  2. [代码]Delphi实现窗体内嵌其他应用程序窗体

    实现原理是启动一个应用程序,通过ProcessID得到窗体句柄,然后对其设定父窗体句柄为本程序某控件句柄(本例是窗体内一个Panel的句柄),这样就达成了内嵌的效果. 本文实现的是内嵌一个记事本程序, ...

  3. Java之数组遍历

    package basic; //数组遍历方法 public class ForEach { public static void main(String[] args) { // 原始数组 Stri ...

  4. zookeeper 四字命令

    zookeeper四字命令   ZooKeeper3.4.6支持某些特定的四字命令字母与其的交互.它们大多是查询命令,用来获取 ZooKeeper 服务的当前状态及相关信息.用户在客户端可以通过 te ...

  5. UVALive5874 - Social Holidaying-二分图匹配/匈牙利算法

    有n个家庭,m个房间,一个房间只能两个家庭住.求最大匹配. 比较标准的二分图问题.先初始化把可能的家庭建边,然后跑一边匈牙利算法. 最后的答案是最大匹配数/2,因为建图时有重复. #include & ...

  6. Jquery 行选中背景色

    直接上代码: 懒得以后网上在查了,拷贝直接可用 Style: .tbSelectCss { background-color:#d5f4fe; } Html: <table name=" ...

  7. jdbc,mybatis,hibernate各自有优缺点以及区别

    JDBC: 我们平时使用jdbc进行编程,大致需要下面几个步骤: 1,使用jdbc编程需要连接数据库,注册驱动和数据库信息 2,操作Connection,打开Statement对象 3,通过State ...

  8. IDEA如何刷新pom文件

    被新手问到了“IDEA如何刷新pom文件?”这个问题,想来这是一个不好意思问的常犯的错误吧. 在IDEA中,修改了pom.xml文件,添加了依赖以后,一般会弹出以下这个警告来. 点击[Import C ...

  9. Python的二叉树实现

    二叉树需要实现的功能及思路 找到最小值 没什么好说的就是二叉树最左下的顶点 找到最大值 没什么好说的就是二叉树最右下的顶点 插入 分情况,如果二叉树为空那么直接直接设置成根节点就好了.否则就逐层向下, ...

  10. 自学Linux Shell3.1-帮助命令man

    点击返回 自学Linux命令行与Shell脚本之路 3.1-帮助命令man 1.man命令概述 默认bash shell提示符是美元符号($),这个符号表明shell在等待用户输入. Linux ma ...