本篇博文將會自頂向下地介紹elastic-job,讓大家認識了解并且快速搭建起環(huán)境。
elastic-job在2.x之后,出了兩個產(chǎn)品線:Elastic-Job-Lite和Elastic-Job-Cloud。我們一般使用Elastic-Job-Lite就能夠滿足需求,本文也是以Elastic-Job-Lite為主。1.x系列對應(yīng)的就只有Elastic-Job-Lite,并且在2.x里修改了一些核心類名,差別雖大,原理類似,建議使用2.x系列。寫此博文,最新release版本為2.0.5。
- <!-- 引入elastic-job-lite核心模塊 -->
- <dependency>
- <groupId>com.dangdang</groupId>
- <artifactId>elastic-job-lite-core</artifactId>
- <version>2.0.5</version>
- </dependency>
- <!-- 使用springframework自定義命名空間時引入 -->
- <dependency>
- <groupId>com.dangdang</groupId>
- <artifactId>elastic-job-lite-spring</artifactId>
- <version>2.0.5</version>
- </dependency>
- package com.fanfan.sample001;
- import com.dangdang.ddframe.job.api.ShardingContext;
- import com.dangdang.ddframe.job.api.simple.SimpleJob;
- import java.util.Date;
- /**
- * Created by fanfan on 2016/12/20.
- */
- public class MySimpleJob implements SimpleJob {
- @Override
- public void execute(ShardingContext shardingContext) {
- System.out.println(String.format("------Thread ID: %s, 任務(wù)總片數(shù): %s, 當前分片項: %s",
- Thread.currentThread().getId(), shardingContext.getShardingTotalCount(), shardingContext.getShardingItem()));
- /**
- * 實際開發(fā)中,有了任務(wù)總片數(shù)和當前分片項,就可以對任務(wù)進行分片執(zhí)行了
- * 比如 SELECT * FROM user WHERE status = 0 AND MOD(id, shardingTotalCount) = shardingItem
- */
- }
- }
Step3. Spring配置
- <?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">
- <!--配置作業(yè)注冊中心 -->
- <reg:zookeeper id="regCenter" server-lists="192.168.241.1:2181" namespace="dd-job"
- base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />
- <!-- 配置作業(yè)-->
- <job:simple id="mySimpleJob" class="com.fanfan.sample001.MySimpleJob" registry-center-ref="regCenter"
- sharding-total-count="2" cron="0/2 * * * * ?" overwrite="true" />
- </beans>
- package com.fanfan.sample001;
- import com.dangdang.ddframe.job.api.ShardingContext;
- import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by fanfan on 2016/12/23.
- */
- public class MyDataFlowJob implements DataflowJob<User> {
- /*
- status
- 0:待處理
- 1:已處理
- */
- @Override
- public List<User> fetchData(ShardingContext shardingContext) {
- List<User> users = null;
- /**
- * users = SELECT * FROM user WHERE status = 0 AND MOD(id, shardingTotalCount) = shardingItem Limit 0, 30
- */
- return users;
- }
- @Override
- public void processData(ShardingContext shardingContext, List<User> data) {
- for (User user: data) {
- System.out.println(String.format("用戶 %s 開始計息", user.getUserId()));
- user.setStatus(1);
- /**
- * update user
- */
- }
- }
- }
- <job:dataflow id="myDataFlowJob" class="com.fanfan.sample001.MyDataFlowJob" registry-center-ref="regCenter"
- sharding-total-count="2" cron="0 0 02 * * ?" streaming-process="true" overwrite="true" />
聯(lián)系客服