官網(wǎng):http://elasticjob.io/index_zh.html
-
引入架包
<!-- 引入elastic-job-lite核心模塊 -->
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>2.1.5</version>
</dependency>
<!-- 使用Spring配置啟動 -->
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>2.1.5</version>
</dependency>
-
簡單作業(yè)開發(fā)
<?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="10.23.24.33:2181,10.23.24.34:2181"
namespace="test-qa" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3"/>
<!--簡單定時任務-->
<job:simple id="userSyncJobDemo" class="com.example.demo.job.simple.UserSyncSimpleJob" registry-center-ref="regCenter"
sharding-total-count="4" cron="0 0/1 * * * ? " description="zjs-UserSyncSimpleJob" overwrite="true"/>
</beans>
public class UserSyncSimpleJob implements SimpleJob {
@Override
public void execute(ShardingContext shardingContext) {
int a=shardingContext.getShardingItem();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(Thread.currentThread().getId()+","+sf.format(new Date())+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+a);
}
}
-
Dataflow作業(yè)開發(fā)
<job:dataflow id="userSyncDataFlowJob" class="com.example.demo.job.simple.UserSyncDataFlowJob"
registry-center-ref="regCenter"
sharding-total-count="4" cron="0 0/1 * * * ?" description="zjs-dataFlowDemo" overwrite="true"/>
public class UserSyncDataFlowJob implements DataflowJob<String> {
@Override
public List<String> fetchData(ShardingContext shardingContext) {
//獲取用戶的數(shù)據(jù)列表号枕,返回給processData
return null;
}
@Override
public void processData(ShardingContext shardingContext, List<String> list) {
//對每條數(shù)據(jù)的處理邏輯
}
}