1 启动类加上异步任务注解
@SpringBootApplication @EnableScheduling @EnableAsync//开启异步任务 public class NpApplication{ public static void main(String[] args) { SpringApplication.run( NpApplication.class, args); } @Bean(name="taskExecutor") public TaskExecutor workExecutor() {//线程池配置 ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); threadPoolTaskExecutor.setThreadNamePrefix("Async-"); threadPoolTaskExecutor.setCorePoolSize(5); threadPoolTaskExecutor.setMaxPoolSize(20); threadPoolTaskExecutor.setQueueCapacity(20000); threadPoolTaskExecutor.afterPropertiesSet(); return threadPoolTaskExecutor; } }
2 在需要的方法加上@Async注解(方法内的代码为业务代码,无须纠结)
@Async public Future<BmloanInfoQueryResponseRoot> sendLoanQuery(BmloanInfoQueryRequestBody requestBody) throws Exception {//需要响应结果,用future包装 BmloanInfoQueryRequestRoot root = new BmloanInfoQueryRequestRoot(); root.setBody(requestBody); BmInitRequestHead.initRequestHeaderXml(root.getRequestHead(), bmChannelid, bmChanbankid, bmChanuserid, bmKhTranid); String sendXml = XMLUtil.convertToXml(root); // log.info("贷款申请信息批量查询接口请求报文:{}", sendXml); ESBClient esbclient = new ESBClientImpl(); esbclient.setTimeOut(600000); String res = esbclient.sendSync(sendXml, bmOpenandapplyUrl); // log.info("贷款申请信息批量查询接口返回报文:{}", res); BmloanInfoQueryResponseRoot queryResponse = (BmloanInfoQueryResponseRoot) XMLUtil.convertXmlStrToObject(BmloanInfoQueryResponseRoot.class, res); return new AsyncResult<BmloanInfoQueryResponseRoot>(queryResponse);//包装响应结果 }
3 调用异步方法(方法内的代码为业务代码,无须纠结)
public void syncInfo() { List<String> businos = loanInfoDao.listNotSynch(100); if (businos.size() == 0) { return; } int size = businos.size(); for (int i = 0; i < businos.size(); i++) { String busino = businos.get(i); BmloanInfoQueryRequestBody body = new BmloanInfoQueryRequestBody(); body.setBusino(busino); try { Future<BmloanInfoQueryResponseRoot> queryResFuture = bmService.sendLoanQuery(body);//这里调用的异步方法 futures.add(queryResFuture);//将异步结果加入list中,注意不能在这里直接获取异步的结果,否则线程将阻塞,异步无效 //futrues的定义===>>> List<Future<BmloanInfoQueryResponseRoot>> futures = new ArrayList<Future<BmloanInfoQueryResponseRoot>>(); businoList.add(busino); } catch (Exception e) { log.error("贷款申请信息批量查询异常=>流水号:{},错误信息:{}", busino, e.getMessage()); } } dataHandle(); syncInfo();//再调用 }
4 注意事项
4.1 第三步调用异步的方法 和 第二步的异步方法不能在同一个类中。
4.2 异步方法得定义为public。
4.3 在@Async标注的方法,并有@Transactional注解的;在其调用数据库操作之时,将无法产生事务管理的控制,原因就在于其是基于异步处理的操作。
可以将需要事务管理操作的方法放置到异步方法内部,在内部被调用的方法上添加@Transactional.