Spring Boot异步调用@Async过程详解
在实际开发中,有时候为了及时处理请求和进行响应,我们可能会多任务同时执行,或者先处理主任务,也就是异步调用,异步调用的实现有很多,例如多线程、定时任务、消息队列等,
我们来讲讲@Async异步方法调用。
一、@Async使用演示
@Async是Spring内置注解,用来处理异步任务,在SpringBoot中同样适用,且在SpringBoot项目中,除了boot本身的starter外,不需要额外引入依赖。
而要使用@Async,需要在启动类上加上@EnableAsync主动声明来开启异步方法。
@EnableAsync @SpringBootApplication publicclassSpringbootApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(SpringbootApplication.class,args); } }
现假设有3个任务需要去处理,分别对应AsyncTask类的taskOne、taskTwo、taskThree方法,这里做了线程的sleep来模拟实际运行。
@Slf4j @Component publicclassAsyncTask{ privateRandomrandom=newRandom(); publicvoidtaskOne()throwsInterruptedException{ longstart=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); longend=System.currentTimeMillis(); log.info("任务一执行完成耗时{}秒",(end-start)/1000f); } publicvoidtaskTwo()throwsInterruptedException{ longstart=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); longend=System.currentTimeMillis(); log.info("任务二执行完成耗时{}秒",(end-start)/1000f); } publicvoidtaskThree()throwsInterruptedException{ longstart=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); longend=System.currentTimeMillis(); log.info("任务三执行完成耗时{}秒",(end-start)/1000f); } }
然后编写测试类,由于@Async注解需要再Spring容器启动后才能生效,所以这里讲测试类放到了SpringBoot的test包下,使用了SpringBootTest。
@Slf4j @RunWith(SpringRunner.class) @SpringBootTest(classes=SpringbootApplication.class) publicclassAsyncTaskTest{ @Autowired privateAsyncTaskasyncTask; @Test publicvoiddoAsyncTasks(){ try{ longstart=System.currentTimeMillis(); asyncTask.taskOne(); asyncTask.taskTwo(); asyncTask.taskThree(); Thread.sleep(5000); longend=System.currentTimeMillis(); log.info("主程序执行完成耗时{}秒",(end-start)/1000f); }catch(InterruptedExceptione){ e.printStackTrace(); } } }
运行测试方法,可以在控制台看到任务一二三按顺序执行,最后主程序完成,这和我们的预期一样,因为我们没有任何额外的处理,他们就是普通的方法,按编码顺序依次执行。
而如果要使任务并发执行,我们只需要在任务方法上使用@Async注解即可,需要注意的是@Async所修饰的方法不要定义为static类型,这样异步调用不会生效。
@Slf4j @Component publicclassAsyncTask{ privateRandomrandom=newRandom(); //@Async所修饰的函数不要定义为static类型,这样异步调用不会生效 @Async publicvoidtaskOne()throwsInterruptedException{ longstart=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); longend=System.currentTimeMillis(); log.info("任务一执行完成耗时{}秒",(end-start)/1000f); } @Async publicvoidtaskTwo()throwsInterruptedException{ longstart=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); longend=System.currentTimeMillis(); log.info("任务二执行完成耗时{}秒",(end-start)/1000f); } @Async publicvoidtaskThree()throwsInterruptedException{ longstart=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); longend=System.currentTimeMillis(); log.info("任务三执行完成耗时{}秒",(end-start)/1000f); } }
然后我们在运行测试类,这个时候输出可能就五花八门了,任意任务都可能先执行完成,也有可能有的方法因为主程序关闭而没有输出。
二、Future获取异步执行结果
上面演示了@Async,但是有时候除了需要任务并发调度外,我们还需要获取任务的返回值,且在多任务都执行完成后再结束主任务,这个时候又该怎么处理呢?
在多线程里通过Callable和Future可以获取返回值,这里也是类似的,我们使用Future返回方法的执行结果,AsyncResult是Future的一个实现类。
@Slf4j @Component publicclassFutureTask{ privateRandomrandom=newRandom(); //@Async所修饰的函数不要定义为static类型,这样异步调用不会生效 @Async publicFuturetaskOne()throwsInterruptedException{ longstart=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); longend=System.currentTimeMillis(); log.info("任务一执行完成耗时{}秒",(end-start)/1000f); returnnewAsyncResult<>("任务一Ok"); } @Async publicFuture taskTwo()throwsInterruptedException{ longstart=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); longend=System.currentTimeMillis(); log.info("任务二执行完成耗时{}秒",(end-start)/1000f); returnnewAsyncResult<>("任务二OK"); } @Async publicFuture taskThree()throwsInterruptedException{ longstart=System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); longend=System.currentTimeMillis(); log.info("任务三执行完成耗时{}秒",(end-start)/1000f); returnnewAsyncResult<>("任务三Ok"); } }
在AsyncResult中:
- isDone()方法可以用于判断异步方法是否执行完成,若任务完成,则返回true
- get()方法可用于获取任务执行后返回的结果
- cancel(booleanmayInterruptIfRunning)可用于取消任务,参数mayInterruptIfRunning表示是否允许取消正在执行却没有执行完毕的任务,如果设置true,则表示可以取消正在执行过程中的任务
- isCancelled()方法表示任务是否被取消成功,如果在任务正常完成前被取消成功,则返回true
- get(longtimeout,TimeUnitunit)用来获取执行结果,如果在指定时间内,还没获取到结果,就直接返回null
@Slf4j @RunWith(SpringRunner.class) @SpringBootTest(classes=SpringbootApplication.class) publicclassAsyncTaskTest{ @Autowired privateFutureTaskfutureTask; @Test publicvoiddoFutureTasks(){ try{ longstart=System.currentTimeMillis(); Futurefuture1=futureTask.taskOne(); Future future2=futureTask.taskTwo(); Future future3=futureTask.taskThree(); //3个任务执行完成之后再执行主程序 do{ Thread.sleep(100); }while(future1.isDone()&&future2.isDone()&&future3.isDone()); log.info("获取异步方法的返回值:{}",future1.get()); Thread.sleep(5000); longend=System.currentTimeMillis(); log.info("主程序执行完成耗时{}秒",(end-start)/1000f); }catch(InterruptedExceptione){ e.printStackTrace(); }catch(ExecutionExceptione){ e.printStackTrace(); } } }
运行测试类,我们可以看到任务一二三异步执行了,主任务最后执行完成,而且可以获取到任务的返回信息。
源码地址:https://github.com/imyanger/springboot-project/tree/master/p23-springboot-async
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。