1. 获取任务
public ExecutorService executorService = Executors.newCachedThreadPool();
@Scheduled(cron = "0 0/1 * * * ? ")
public void callBack() {
List<SyncInfoObject> syncInfoList = null;
if(null != syncInfoList && syncInfoList.size() > 0) {
try {
CountDownLatch latch = new CountDownLatch(syncInfoList.size());
for(SyncInfoObject syncInfoObject:syncInfoList){
executorService.submit(new CallBakTask(syncInfoObject,latch));
}
//等待所有任务都执行完, 最多等待600秒(10分钟)
latch.await(600, TimeUnit.SECONDS);
}catch(Exception e){
logger.error("---------CallBackService.callBack任务执行异常,"+e.getMessage(),e);
}finally {
//批量更新
asynInfoMapper.batchUpdateCallBackCount(syncInfoList);
}
}
}
2. 任务线程
public class CallBakTask implements Callable<JSONObject>{
private SyncInfoObject syncInfoObject;
CountDownLatch latch;
public CallBakTask(SyncInfoObject syncInfoObjec
,CountDownLatch latch){
this.syncInfoObject = syncInfoObject;
this.latch = latch;
}
public JSONObject call() throws Exception {
try {
//执行处理
}catch(Exception e){
logger.error("---------callBack子任务执行异常" +e.getMessage(),e);
throw new Exception(e);
}finally {
latch.countDown();//单次任务结束,计数器减一
}
return output;
}
}
3. 任务状态
<!-- 批量更新回调次数 -->
<update id="batchUpdateCount" parameterType="java.util.List">
UPDATE t_table
SET exec_count = exec_count+ 1
WHERE key_id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.keyId,jdbcType=VARCHAR}
</foreach>
</update>