Java并发编程:Spring中使用 @async 注解实现异步调用的原理

来源:互联网 发布:丢书大作战 知乎 编辑:程序博客网 时间:2024/05/21 11:31

在我们使用spring框架的过程中,在很多时候我们会使用@async注解来异步执行某一些方法,提高系统的执行效率。今天我们来探讨下 spring 是如何完成这个功能的。

    spring 在扫描bean的时候会扫描方法上是否包含@async的注解,如果包含的,spring会为这个bean动态的生成一个子类,我们称之为代理类(?), 代理类是继承我们所写的bean的,然后把代理类注入进来,那此时,在执行此方法的时候,会到代理类中,代理类判断了此方法需要异步执行,就不会调用父类 (我们原本写的bean)的对应方法。spring自己维护了一个队列,他会把需要执行的方法,放入队列中,等待线程池去读取这个队列,完成方法的执行, 从而完成了异步的功能。我们可以关注到再配置task的时候,是有参数让我们配置线程池的数量的。因为这种实现方法,所以在同一个类中的方法调用,添加@async注解是失效的!,原因是当你在同一个类中的时候,方法调用是在类体内执行的,spring无法截获这个方法调用。

    那在深入一步,spring为我们提供了AOP,面向切面的功能。他的原理和异步注解的原理是类似的,spring在启动容器的时候,会扫描切面所定义的 类。在这些类被注入的时候,所注入的也是代理类,当你调用这些方法的时候,本质上是调用的代理类。通过代理类再去执行父类相对应的方法,那spring只 需要在调用之前和之后执行某段代码就完成了AOP的实现了!

   那最后我们还有一个问题,spring是如何动态的生成某一个类的子类的?代理类?



简单介绍:

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

开启@Async注解:

 

+ View code
<task:annotation-driven executor="annotationExecutor" /><!-- 支持 @Async 注解 --><task:executor id="annotationExecutor" pool-size="20"/>

 

同时加入<context:component-scan />扫描注解。

 

栗子:

 

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

 

复制代码
+ View code
@Componentpublic class TestAsyncBean {    public void sayHello4() throws InterruptedException {        Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。        System.out.println("我爱你啊!");}
 
复制代码

 

复制代码
+ View code
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({"classpath:/applicationContext.xml"})public class TestAsync {    @Test    public void test_sayHello4() throws InterruptedException, ExecutionException {        System.out.println("你不爱我了么?");        testAsyncBean.sayHello4();        System.out.println("回的这么慢, 你肯定不爱我了, 我们还是分手吧。。。");        Thread.sleep(3 * 1000);// 不让主进程过早结束    }}
 
复制代码

 

输出结果:

 

+ View code
你不爱我了么?我爱你啊!回的这么慢, 你肯定不爱我了, 我们还是分手吧。。。

 

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

 

使用@Async的异步调用:

 

复制代码
+ View code
@Componentpublic class TestAsyncBean {    @Async    public void sayHello3() throws InterruptedException {        Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。        System.out.println("我爱你啊!");    }}
 
复制代码

 

复制代码
+ View code
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({"classpath:/applicationContext.xml"})public class TestAsync {    @Autowired    private TestAsyncBean testAsyncBean;    @Test    public void test_sayHello3() throws InterruptedException, ExecutionException {        System.out.println("你不爱我了么?");        testAsyncBean.sayHello3();        System.out.println("你竟无话可说, 我们分手吧。。。");        Thread.sleep(3 * 1000);// 不让主进程过早结束    }}
 
复制代码

 

输出结果:

 

+ View code
你不爱我了么?你竟无话可说, 我们分手吧。。。我爱你啊!

 

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

 

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

 

复制代码
+ View code
@Componentpublic class TestAsyncBean {    @Async    public String sayHello2() throws InterruptedException {        Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。        return "我爱你啊!";// 调用方调用后会立即返回,所以返回null    }}
 
复制代码

 

复制代码
+ View code
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({"classpath:/applicationContext.xml"})public class TestAsync {    @Autowired    private TestAsyncBean testAsyncBean;    @Test    public void test_sayHello2() throws InterruptedException, ExecutionException {        System.out.println("你不爱我了么?");        System.out.println(testAsyncBean.sayHello2());        System.out.println("你说的啥? 我们还是分手吧。。。");        Thread.sleep(3 * 1000);// 不让主进程过早结束    }}
 
复制代码

 

输出结果:

 

+ View code
你不爱我了么?null你说的啥? 我们还是分手吧。。。

 

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

 

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

 

复制代码
+ View code
@Componentpublic class TestAsyncBean {    @Async    public Future<String> sayHello1() throws InterruptedException {        int thinking = 2;        Thread.sleep(thinking * 1000);//网络连接中 。。。消息发送中。。。        System.out.println("我爱你啊!");        return new AsyncResult<String>("发送消息用了"+thinking+"秒");    }}
 
复制代码

 

复制代码
+ View code
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({"classpath:/applicationContext.xml"})public class TestAsync {    @Autowired    private TestAsyncBean testAsyncBean;    @Test    public void test_sayHello1() throws InterruptedException, ExecutionException {        Future<String> future = null;        System.out.println("你不爱我了么?");        future = testAsyncBean.sayHello1();        System.out.println("你竟无话可说, 我们分手吧。。。");        Thread.sleep(3 * 1000);// 不让主进程过早结束        System.out.println(future.get());    }}
 
复制代码

 

输出结果:

 

+ View code

 

你不爱我了么?你竟无话可说, 我们分手吧。。。我爱你啊!发送消息用了2秒
阅读全文
0 0