- 异步任务
- 定时任务
- 邮件发送(常用,springboot官方)
一、异步任务
1、创建一个springboot项目,勾选web启动器依赖
2、新建一个业务
但是数据处理会线程休眠3秒,休眠期间页面会无显示
@Service//由spring托管的业务
public class AsyncService {
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理...");
}
}
3、编写controller调用这个业务
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello(){
asyncService.hello();//停止三秒,转圈
return "ok";
}
}
4、转换成异步任务
在业务代码块中加上@Async异步任务注解