2.1 任务监听器:
- 定义监听器:
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.shardingsphere.elasticjob.infra.listener.ElasticJobListener;
import org.apache.shardingsphere.elasticjob.infra.listener.ShardingContexts;
import java.util.Date;
@Slf4j
public class MyElasticJobListener implements ElasticJobListener {
private long beginTime = 0;
@Override
public void beforeJobExecuted(ShardingContexts shardingContexts) {
beginTime = System.currentTimeMillis();
log.info("===>{} MyElasticJobListener BEGIN TIME: {} <===",shardingContexts.getJobName(), DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
}
@Override
public void afterJobExecuted(ShardingContexts shardingContexts) {
long endTime = System.currentTimeMillis();
log.info("===>{} MyElasticJobListener END TIME: {},TOTAL CAST: {} <===",shardingContexts.getJobName(), DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"), endTime - beginTime);
}
@Override
public String getType() {
return "myElasticJobListener";
}
}
2) 在项目resources 新建文件夹: META-INF\services
3)新建文件,名称为:org.apache.shardingsphere.elasticjob.infra.listener.ElasticJobListener
文集内容:
# 监听器实现类的 类全路径
com.example.springelasticjob.config.MyElasticJobListener
4)job 配置增加监听器:
// 创建作业配置
JobConfiguration jobConfiguration = JobConfiguration.newBuilder("myjob-param", 1).cron("0/5 * * * * ?")
.overwrite(true).shardingItemParameters("0=Beijing,1=Shanghai,2=Guangzhou").jobParameter("0=a,1=b,2=c")
.jobListenerTypes("myElasticJobListener")
.build();
jobListenerTypes(“myElasticJobListener”) 中 “myElasticJobListener” 要和 MyElasticJobListener getType() 返回的保持一致,否则启动无法找到 监听器:
2. 2 DataflowJob 流工作:
2.2.1 新建 DataflowJob:
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.dataflow.job.DataflowJob;
import java.util.ArrayList;
import java.util.List;
/**
* 流任务
*/
@Slf4j
public class MyDataFlowJob implements DataflowJob {
@Override
public List fetchData(ShardingContext shardingContext) {
// 抓取数据
// 分片参数 0=text,1=image,2=radio,3=vedio
String jobParameter = shardingContext.getJobParameter();
log.debug("job 执行 error,job名称:{},分片数量:{},分片:{},分片参数:{}", shardingContext.getJobName(), shardingContext.getShardingTotalCount(), shardingContext.getShardingItem(), jobParameter);
List list = new ArrayList(1);
list.add("lgx");
return list;
}
@Override
public void processData(ShardingContext shardingContext, List list) {
// 数据处理
System.out.println("list.toString() = " + list.toString());
}
}
2.2.2 streaming.process 属性配置:
private static JobConfiguration createJobConfiguration() {
JobConfiguration jobConfiguration = JobConfiguration.newBuilder("myjob-dataflow-param", 1).cron("0/30 * * * * ?")
.overwrite(true).shardingItemParameters("0=Beijing,1=Shanghai,2=Guangzhou").jobParameter("0=a,1=b,2=c")
// streaming.process 流处理设置为true
.setProperty("streaming.process","true")
.build();
return jobConfiguration;
}
2.2.3 执行效果:
虽然任务是每隔30s 执行一次,但是因为 fetchData 可以一直获取到数据,使的 processData 方法可以一直被调用: