怎么在SpringBoot中实现异步调用@Async

  介绍

这期内容当中小编将会给大家带来有关怎么在SpringBoot中实现异步调用@Async,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

<强>一、@Async使用演示

@Async是弹簧内置注解,用来处理异步任务,在SpringBoot中同样适用,且在SpringBoot项目中,除了引导本身的起动器外,不需要额外引入依赖。

而要使用@Async,需要在启动类上加上@EnableAsync主动声明来开启异步方法。

@EnableAsync   @SpringBootApplication   public  class  SpringbootApplication  {      public 才能;static  void  main (String [], args), {   ,,,SpringApplication.run (SpringbootApplication.class, args);   ,,}   }

现假设有3个任务需要去处理,分别对应AsyncTask类的taskOne, taskTwo, taskThree方法,这里做了线程的睡眠来模拟实际运行。

@Slf4j   @ component   public  class  AsyncTask  {      private 才能Random  Random =, new 随机();   ,,   public 才能;void  taskOne (), throws  InterruptedException  {   ,,,long  start =, System.currentTimeMillis ();   ,,,thread . sleep (random.nextInt (10000);   ,,,long 最终获得=,System.currentTimeMillis ();   ,,,log.info(“任务一执行完成耗时{}秒“,,(最终获得成功,开始)/1000 f);   ,,}   ,,   public 才能;void  taskTwo (), throws  InterruptedException  {   ,,,long  start =, System.currentTimeMillis ();   ,,,thread . sleep (random.nextInt (10000);   ,,,long 最终获得=,System.currentTimeMillis ();   ,,,log.info(“任务二执行完成耗时{}秒“,,(最终获得成功,开始)/1000 f);   ,,}   ,,   public 才能;void  taskThree (), throws  InterruptedException  {   ,,,long  start =, System.currentTimeMillis ();   ,,,thread . sleep (random.nextInt (10000);   ,,,long 最终获得=,System.currentTimeMillis ();   ,,,log.info(“任务三执行完成耗时{}秒“,,(最终获得成功,开始)/1000 f);   ,,}   }

然后编写测试类,由于@Async注解需要再春容器启动后才能生效,所以这里讲测试类放到了SpringBoot的测试包下,使用了SpringBootTest。

@Slf4j   @RunWith (SpringRunner.class)   @SpringBootTest (classes =, SpringbootApplication.class)   public  class  AsyncTaskTest  {      @ autowired才能   private 才能;AsyncTask  asyncTask;      @Test才能   public 才能;void  doAsyncTasks () {   ,,,try  {   ,,,,,long  start =, System.currentTimeMillis ();   ,,,,,asyncTask.taskOne ();   ,,,,,asyncTask.taskTwo ();   ,,,,,asyncTask.taskThree ();   ,,,,,thread . sleep (5000);   ,,,,,long 最终获得=,System.currentTimeMillis ();   ,,,,,log.info(“主程序执行完成耗时{}秒“,,(最终获得成功,开始)/1000 f);   ,,,},catch  (InterruptedException  e), {   ,,,,,e.printStackTrace ();   ,,,}   ,,}      }

运行测试方法,可以在控制台看到任务一二三按顺序执行,最后主程序完成,这和我们的预期一样,因为我们没有任何额外的处理,他们就是普通的方法,按编码顺序依次执行。

怎么在SpringBoot中实现异步调用@Async

而如果要使任务并发执行,我们只需要在任务方法上使用@Async注解即可,需要注意的是@Async所修饰的方法不要定义为静态类型,这样异步调用不会生效。

@Slf4j   @ component   public  class  AsyncTask  {      private 才能Random  Random =, new 随机();//@Async才能所修饰的函数不要定义为静态类型,这样异步调用不会生效   @Async才能   public 才能;void  taskOne (), throws  InterruptedException  {   ,,,long  start =, System.currentTimeMillis ();   ,,,thread . sleep (random.nextInt (10000);   ,,,long 最终获得=,System.currentTimeMillis ();   ,,,log.info(“任务一执行完成耗时{}秒“,,(最终获得成功,开始)/1000 f);   ,,}      @Async才能   public 才能;void  taskTwo (), throws  InterruptedException  {   ,,,long  start =, System.currentTimeMillis ();   ,,,thread . sleep (random.nextInt (10000);   ,,,long 最终获得=,System.currentTimeMillis ();   ,,,log.info(“任务二执行完成耗时{}秒“,,(最终获得成功,开始)/1000 f);   ,,}      @Async才能   public 才能;void  taskThree (), throws  InterruptedException  {   ,,,long  start =, System.currentTimeMillis ();   ,,,thread . sleep (random.nextInt (10000);   ,,,long 最终获得=,System.currentTimeMillis ();   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在SpringBoot中实现异步调用@Async