1. 开启异步调用(EnableAsync)
@SpringBootApplication
@MapperScan("com.sun.mapper")
@EnableScheduling//支持任务调度
@EnableAsync //开启异步调用
public class SBAPP {
public static void main(String[] args) {
SpringApplication.run(SBAPP.class,args);
}
}
2、编写异步请求方法上加@Async
@Async
public void sedMsg() throws InterruptedException {
//假设网络暂停不同,需要重复向外发送,直到发送成功为止
while(true){
Thread.sleep(1000);
System.out.println("发送中......."+new SimpleDateFormat("HH:mm:ss").format(new Date()));
}
}
3、测试异步请求
@Autowired
private SyslJob syslJob;
@RequestMapping("/test")
@ResponseBody
public String m5() throws InterruptedException {
syslJob.sedMsg();
return "测试" ;
}
}