BlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>(config.getTaskQueueSize());
ExecutorService service = new ThreadPoolExecutor(
config.getWorkingThreadCount(),
config.getWorkingThreadCount(), 0L, TimeUnit.MILLISECONDS, taskQueue);
for (int i = 0; i < entities.size(); i++) {
// 如果等待队列满了,线程睡眠
while (taskQueue.size() == config.getTaskQueueSize()) {
try {
Thread.sleep(config.getWaitingTimeMillis());
} catch (InterruptedException e) {
log.error("进入睡眠等待队列空闲失败,取消更新风险客户任务");
return;
}
}
CmbCorporateCustomerEntity entity = entities.get(i);
service.submit(() -> {
try {
riskCustomerService.executeRiskCustomerMatchingTask(entity);
} catch (Exception e) {
log.error("执行风险客户扫描失败,customerNumber:{},name:{},{}"
, entity.getCustomerNumber(), entity.getCustomerChineseName(), e.getMessage());
}
});
// 当前list遍历完时,判断是否还有数据未进行匹配任务,有则从头继续
if (i == entities.size() - 1) {
log.info("【更新风险客户】第【{}】批任务提交完毕,总共提交了【{}】条,开始尝试获取下一批"
, totalCount / config.getBatchSize(), totalCount);
entities = cmbCorporateCustomerService.getNextCustomersBatch();
if (CollectionUtils.isNotEmpty(entities)) {
totalCount += entities.size();
i = -1;
} else {
break;
}
}
}
// 任务提交完毕,关闭线程池
service.shutdown();
while (!service.isTerminated()) {
try {
Thread.sleep(config.getWaitingTimeMillis());
} catch (InterruptedException e) {
log.error("进入睡眠等待跑批任务执行完成失败,停止等待执行完毕");
return;
}
}