SpringBoot开启异步调用方法
发布时间:2020-12-14 19:39:50 所属栏目:Java 来源:网络整理
导读:异步调用无需等待,方法相当于子线程,后台执行,主线程执行完成,子线程开始执行。 SpringBoot 开启异步执行仅需两步: 方法上加 @Async @Override @Async @Transactional(rollbackFor = Exception.class) public Integer init(DatePojo datePojo){ //xxxxx
异步调用无需等待,方法相当于子线程,后台执行,主线程执行完成,子线程开始执行。 方法上加 @Async @Override @Async @Transactional(rollbackFor = Exception.class) public Integer init(DatePojo datePojo){ //xxxxxxxxxxx 业务略 xxxxxxx log.info(" 起止日期为 : {},{} ",start,end); //xxxxxxxxxxxxx 业务略 xxxxxxx log.info(" ------------------ 【能源入库完成】------------------ {}",nyList); log.info(" ------------------ 【新能源初始化结束】------------------"); return 0; } main 方法 开启 @EnableAsync @SpringBootApplication @EnableAsync public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } } controller @PostMapping("/ny") public ReturnMessage ny( @RequestBody DatePojo datePojo,BindingResult result) { log.info(" 【 能源初始化接口调用开始 】"); //业务类 Integer data = xstjJdcNyService.init(datePojo); log.info(" 【 能源初始化接口调用结束 】"); return new ReturnMessage(CodeMsgEnum.OK.getCode(),CodeMsgEnum.OK.getMsg(),data); } 执行结果 可以看到 controller 先执行完成然后返回状态,接着 方法才开始执行。 错误 Spring之AOP奇葩报错:Null return value from advice does not match primitive return type for 总结 1 使用了@Async的方法,会被当成是一个子线程,所有整个sendSms方法,会在主线程执行完了之后执行 2 同一个类中,一个方法调用另外一个有@Async的方法,注解是不会生效的! 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |