1、构造一个线程池
private static ThreadPoolExecutor threadPoolExecutor;
static {
//构造一个线程池
// corePoolSize:线程池维护线程的最少数量
// maximumPoolSize:线程池维护线程的最大数量
// keepAliveTime: 线程池维护线程所允许的空闲时间
// unit: 线程池维护线程所允许的空闲时间的单位
// workQueue: 线程池所使用的缓冲队列
// handler: 线程池对拒绝任务的处理策略
threadPoolExecutor = new ThreadPoolExecutor(2, 10, 3,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(50),
new ThreadPoolExecutor.CallerRunsPolicy());
}
2、更新数据库之后线程池中发送短信
private Long saveOrUpdateTodo(WfProcessTodo todo, Boolean isSendMsg, String sendMsgContent) {
if (todo.save()) {
// 线程池中发送短信和短消息
threadPoolExecutor.execute(
new Thread() {
public void run() {
try{
//发送消息
// messageService.sendInnerMessage(sendMsgContent, "", todo.processOperatorId, -1, messageSourceService.getMessage("system"));
//发送短信
if (isSendMsg) {
messageService.sendSMS(todo.processOperatorId, sendMsgContent);
}
}catch(Exception e){
log.error("发送消息或短信失败", e)
}
}
}
);
return todo.id;
} else {
throw new LogicException(DomainErrorUtil.formatDomainErrors(todo.errors));
}
}