Spring's @Async annotation implements asynchronous invocation of methods

1. Introduction

Spring provides annotation support for task scheduling and asynchronous method execution.
By setting the @Async annotation on the method, asynchronous invocation of the method can be achieved. Simply put, the caller will be returned immediately after the call, but the actual method is not fully executed, but is handed over to spring's TaskExecutor for execution.

2. Enable support for @Async annotation

<!-- 启动对@Async注解,异步方法执行的支持 -->  
<task:annotation-driven executor="asyncExecutor" />
<task:executor id="asyncExecutor" pool-size="100-10000" queue-capacity="10"/>

3. Take a chestnut

First, the chestnut called synchronously:
@Component
public class TestAsyncBean {
    @Async
    public String sayHello2() throws InterruptedException {
        Thread.sleep(2 * 1000);//模拟网络请求,发送消息等消耗的时间
        System.out.println("你好,小黑");
    }
}
@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);// 不让主进程过早结束,保证异步方法的正确执行
    }
}

Synchronous call result:

小白,你好
小黑,你好
你叫我啥?
Asynchronous call using @Async:
@Component
public class TestAsyncBean {
    @Async
    public void sayHello3() throws InterruptedException {
        Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。
        System.out.println("小黑,你好");
    }
}
@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);// 不让主进程过早结束
    }
}

Asynchronous call result:

小白,你好
你叫我啥?
小黑,你好

4. Conclusion

From the above chestnuts, it can be seen that the asynchronous call is to hand over the task executed by the asynchronous method to spring's TaskExecutor for execution, and the caller will get the call return immediately without waiting for the calling method to be completely executed before it can be put back. value.
Take a chestnut in practical applications,
such as making an open interface in the project, this open interface is for other people to request to call, and the internal logic of the interface is complex, the program execution time is long, and the caller does not care about the final execution result. , you only need to know whether the exact call is successful, then you can use asynchronous calls to avoid long-term request waiting of the interface, and also avoid a series of risky operations caused by request disconnection.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326360649&siteId=291194637