官网: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>
-
简单作业开发
<?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">
<!--配置作业注册中心 -->
<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作业开发
<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) {
//获取用户的数据列表,返回给processData
return null;
}
@Override
public void processData(ShardingContext shardingContext, List<String> list) {
//对每条数据的处理逻辑
}
}