springboot调用php,SpringBoot开启异步调用方法

异步调用无需等待,方法相当于子线程,后台执行,主线程执行完成,子线程开始执行。

SpringBoot 开启异步执行仅需两步:

方法上加 @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

原因是返回为null 基本类型要用包装类型。

总结

1 使用了@Async的方法,会被当成是一个子线程,所有整个sendSms方法,会在主线程执行完了之后执行

2 同一个类中,一个方法调用另外一个有@Async的方法,注解是不会生效的!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

异步调用无需等待,方法相当于子线程,后台执行,主线程执行完成,子线程开始执行。 SpringBoot 开启异步执行仅需两步: 方法上加 @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 原因是返回为null 基本类型要用包装类型。 总结 1 使用了@Async的方法,会被当成是一个子线程,所有整个sendSms方法,会在主线程执行完了之后执行 2 同一个类中,一个方法调用另外一个有@Async的方法,注解是不会生效的! 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
经验分享 程序员 微信小程序 职场和发展