Spring中@Async注解实现“方法”的异步调用

雨点打透心脏的1/2处 2022-06-02 10:16 578阅读 0赞

简单介绍
Spring为任务调度与异步方法执行提供了注解支持。通过在方法上设置@Async注解,可使得方法被异步调用。也就是说调用者会在调用时立即返回,而被调用方法的实际执行是交给Spring的TaskExecutor来完成。

开启@Async注解:

  1. <task:annotation-driven executor="annotationExecutor" />
  2. <!-- 支持 @Async 注解 -->
  3. <task:executor id="annotationExecutor" pool-size="20"/>

同时加入扫描注解。

为了比较,先来一个同步调用

  1. @Component
  2. public class TestAsyncBean {
  3. public void sayHello4() throws InterruptedException {
  4. Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。
  5. System.out.println("我爱你啊!");
  6. }
  7. @RunWith(SpringJUnit4ClassRunner.class)
  8. @ContextConfiguration({
  9. "classpath:/applicationContext.xml"})
  10. public class TestAsync {
  11. @Test
  12. public void test_sayHello4() throws InterruptedException, ExecutionException {
  13. System.out.println("你不爱我了么?");
  14. testAsyncBean.sayHello4();
  15. System.out.println("回的这么慢, 你肯定不爱我了, 我们还是分手吧。。。");
  16. Thread.sleep(3 * 1000);// 不让主进程过早结束
  17. }
  18. }

输出结果:

  1. 你不爱我了么?
  2. 我爱你啊!
  3. 回的这么慢, 你肯定不爱我了, 我们还是分手吧。。。

同步调用会按代码顺序依次进行下去,如果哪里需要等待,那么就阻塞在那里,不再向下继续进行。

使用@Async的异步调用:

  1. @Component
  2. public class TestAsyncBean {
  3. @Async
  4. public void sayHello3() throws InterruptedException {
  5. Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。
  6. System.out.println("我爱你啊!");
  7. }
  8. }
  9. @RunWith(SpringJUnit4ClassRunner.class)
  10. @ContextConfiguration({"classpath:/applicationContext.xml"})
  11. public class TestAsync {
  12. @Autowired
  13. private TestAsyncBean testAsyncBean;
  14. @Test
  15. public void test_sayHello3() throws InterruptedException, ExecutionException {
  16. System.out.println("你不爱我了么?");
  17. testAsyncBean.sayHello3();
  18. System.out.println("你竟无话可说, 我们分手吧。。。");
  19. Thread.sleep(3 * 1000);// 不让主进程过早结束
  20. }
  21. }

输出结果:

  1. 你不爱我了么?
  2. 你竟无话可说, 我们分手吧。。。
  3. 我爱你啊!

异步调用,通过开启新的线程来执行调用的方法,不影响主线程。异步方法实际的执行交给了Spring的TaskExecutor来完成。

上面这种方式是没有返回值的,下面尝试有返回值的异步调用:

  1. @Component
  2. public class TestAsyncBean {
  3. @Async
  4. public String sayHello2() throws InterruptedException {
  5. Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。
  6. return "我爱你啊!";// 调用方调用后会立即返回,所以返回null
  7. }
  8. }
  9. @RunWith(SpringJUnit4ClassRunner.class)
  10. @ContextConfiguration({"classpath:/applicationContext.xml"})
  11. public class TestAsync {
  12. @Autowired
  13. private TestAsyncBean testAsyncBean;
  14. @Test
  15. public void test_sayHello2() throws InterruptedException, ExecutionException {
  16. System.out.println("你不爱我了么?");
  17. System.out.println(testAsyncBean.sayHello2());
  18. System.out.println("你说的啥? 我们还是分手吧。。。");
  19. Thread.sleep(3 * 1000);// 不让主进程过早结束
  20. }
  21. }

输出结果

  1. 输出结你不爱我了么?
  2. null
  3. 你说的啥? 我们还是分手吧。。。

接获取返回值得方式是不行的,这里就需要用到异步回调,异步方法返回值必须为Future<>,就像Callable与Future。

下面通过AsyncResult<>来获得异步调用的返回值:

  1. @Component
  2. public class TestAsyncBean {
  3. @Async
  4. public Future<String> sayHello1() throws InterruptedException {
  5. int thinking = 2;
  6. Thread.sleep(thinking * 1000);//网络连接中 。。。消息发送中。。。
  7. System.out.println("我爱你啊!");
  8. return new AsyncResult<String>("发送消息用了"+thinking+"秒");
  9. }
  10. }
  11. @RunWith(SpringJUnit4ClassRunner.class)
  12. @ContextConfiguration({"classpath:/applicationContext.xml"})
  13. public class TestAsync {
  14. @Autowired
  15. private TestAsyncBean testAsyncBean;
  16. @Test
  17. public void test_sayHello1() throws InterruptedException, ExecutionException {
  18. Future<String> future = null;
  19. System.out.println("你不爱我了么?");
  20. future = testAsyncBean.sayHello1();
  21. System.out.println("你竟无话可说, 我们分手吧。。。");
  22. Thread.sleep(3 * 1000);// 不让主进程过早结束
  23. System.out.println(future.get());
  24. }
  25. }

输出结果:

  1. 你不爱我了么?
  2. 你竟无话可说, 我们分手吧。。。
  3. 我爱你啊!
  4. 发送消息用了2
  5. 官方文档:
  6. 34.4.3 The @Async annotation

发表评论

表情:
评论列表 (有 0 条评论,578人围观)

还没有评论,来说两句吧...

相关阅读