The principle add comment asynchronous calls on @async method: Spring-source learning

In the process we use spring frame, in many cases we will use annotations to @async asynchronous execution of a number of methods to improve the efficiency of the system. Today we explore how next spring to complete this function.

   spring on the scanning method scans when the bean contains annotations @async, if included, spring will be a sub-class for the bean dynamically generated, we call proxy class (?), we have written proxy class is inherited the bean, and then injected into the proxy class came in, that at this time, in the implementation of this approach will be to the proxy class, proxy class to determine if this method should execute asynchronously, they will not call the parent class (we originally wrote bean ) of the corresponding method. spring maintains a queue himself, the way he would need to perform, placed in the queue, waiting to read this thread pool queue, complete the implementation process, thus completing the asynchronous function. We can focus on the task of re-configuration when there is a parameter allows us to configure the number of thread pool. Because of this implementation, the method in the same class called to add @async comment is invalid! Because when you're in the same class, method invocation is performed in the class body, spring can not intercept the method call. In that one step further, spring provides us with AOP, aspect-oriented features. His principle and the principle of asynchronous annotations are similar, spring start when the container is scanned as defined class section. When these classes are injected, the injected agent class is, when you call these methods, the proxy class is called nature. The method of implementation of the parent class to go through the corresponding proxy class that spring just before and after the call to execute a piece of code to complete the implementation of the AOP!    That last we have a problem, spring is how to dynamically generate a subclass of a class? Acting classes?

   

 

 

 

basic introduction:

 

Spring is the task scheduling and asynchronous method execution provides annotation support. By providing @Async annotation on the method, such a method can be invoked asynchronously. That caller returns immediately when you call, but the actual implementation of the method is to be called the Spring TaskExecutor to complete.

 

Open @Async notes:

 

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

 

While adding <context: component-scan /> Scan annotations.

 

 

chestnut:

 

For comparison, first to a synchronous call:

 

@Component 
public class TestAsyncBean { 
    public void sayHello4 () throws InterruptedException { 
        the Thread.sleep (2 * 1000); // network connection. . . Message transmission. . . 
        System.out.println ( "I love you, ah!"); 
}
 

 

@RunWith (SpringJUnit4ClassRunner.class) 
@ContextConfiguration ({ "the CLASSPATH: /applicationContext.xml"}) 
public class TestAsync { 
    @Test 
    public void test_sayHello4 () throws InterruptedException, ExecutionException { 
        System.out.println ( "You do not love me yet ? "); 
        testAsyncBean.sayHello4 (); 
        System.out.println (" back so slow, you certainly do not love me, we break up ... "); 
        Thread.sleep (3 * 1000); // not to prematurely end the main process 
    } 
}
 

 

Output:

 

Do not you love me? 
I love you, ah! 
Back so slow, you certainly do not love me, we break up. . .

 

Synchronous call will be followed by the code sequence continues, and if where to wait, then blocked there, not down to continue.

 

The use of asynchronous calls @Async:

 

 
@Component 
public class TestAsyncBean { 
    @Async 
    public void sayHello3 () throws InterruptedException { 
        the Thread.sleep (2 * 1000); // network connection. . . Message transmission. . . 
        System.out.println ( "I love you, ah!"); 
    } 
}
 

 

@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 ( " Why do not you love me anymore ");? 
        testAsyncBean.sayHello3 (); 
        System.out.println (" you actually have nothing to say, we break up ... "); 
        Thread.sleep (3 * 1000); // not to prematurely end the main process 
    } 
}
 

 

Output:

 

Do not you love me? 
You actually have nothing to say, we break up. . . 
I love you, ah!

 

Asynchronous call, the calling method by opening a new thread, does not affect the main thread. The actual execution of asynchronous method to Spring TaskExecutor to complete.

 

In this way the above is no return value, try the following asynchronous calls that return values:

 

@Component 
public class TestAsyncBean { 
    @Async 
    public String sayHello2 () throws InterruptedException { 
        the Thread.sleep (2 * 1000); // network connection. . . Message transmission. . . 
        return "I love you, ah!"; // call will return immediately after the party called, so return null 
    } 
}
 

 

@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 ( " Why do not you love me anymore ");? 
        System.out.println (testAsyncBean.sayHello2 ()); 
        System.out.println ("? you say what we break up ... "); 
        Thread.sleep (3 * 1000); // not to prematurely end the main process 
    } 
}
 

 

Output:

 

Why do not you love me anymore? 
Null 
you say what? We break up. . .

 

Direct access by way of the return is worth it does not work, here you need to use asynchronous callbacks, asynchronous method returns a value must be Future <>, as Callable and Future.

 

By following AsyncResult <> asynchronous call to obtain a return value:

 

@Component 
public class TestAsyncBean { 
    @Async 
    public Future <String> sayHello1 () throws InterruptedException { 
        int Thinking = 2; 
        the Thread.sleep (Thinking * 1000); // network connection. . . Message transmission. . . 
        System.out.println ( "I love you, ah!"); 
        Return new new AsyncResult <String> ( "send a message with a" + thinking + "seconds"); 
    } 
}
 

 

@RunWith (SpringJUnit4ClassRunner.class) 
@ContextConfiguration ({ "CLASSPATH: /applicationContext.xml"}) 
public class TestAsync { 
    @Autowired 
    Private TestAsyncBean testAsyncBean; 
    @Test 
    public void test_sayHello1 () throws InterruptedException, ExecutionException { 
        Future <String> = null Future ; 
        System.out.println ( "you do not love me yet?"); 
        Future = testAsyncBean.sayHello1 (); 
        System.out.println ( "you actually have nothing to say, we break up ..."); 
        the Thread .sleep (3 * 1000); // let the primary process ends prematurely 
        System.out.println (Future.get ()); 
    } 
}
 

 

Output:

 

Do not you love me? 
You actually have nothing to say, we break up. . . 
I love you, ah! 
Send messages in 2 seconds

 

 

 

Transfer: https://www.cnblogs.com/shangxiaofei/p/6211367.html

Guess you like

Origin www.cnblogs.com/panchanggui/p/11550827.html