前期准备
1、创建一个spring-web工程,在pom.xml中添加依赖
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-quartz
org.springframework.boot
spring-boot-starter-mail
net.bytebuddy
byte-buddy
2、项目结构
3、启动类中添加注解
异步任务调度
1、编写异步任务调度方法,包括有返回值和无返回值的方法(注意,类前面一定要记得写Service),本来是实现类的,这里为了简便,直接写了。
@Servicepublic class MyAsyncServiceImpl{
@Asyncpublic void sendSMS() throwsException{
System.out.println("调用短信验证码业务方法。。。");
Long startTime=System.currentTimeMillis();
Thread.sleep(5000);
Long endTime=System.currentTimeMillis();
System.out.println("短信业务执行完成,耗时:"+(endTime-startTime));}
@Asyncpublic Future processA() throwsException {
System.out.println("开始分析统计业务A的数据。。。");
Long startTime=System.currentTimeMillis();
Thread.sleep(4000);int count=12345;
Long endTime=System.currentTimeMillis();
System.out.println("业务A数据统计耗时:"+(endTime-startTime));return new AsyncResult<>(count);}
@Asyncpublic Future processB() throwsException {
System.out.println("开始分析统计业务B的数据。。。");
Long startTime=System.currentT