@ Async java Asynchronous Method, @ async asynchronous
In spring 3, the @ Async annotation enables a method to be quickly converted to asynchronous execution, and immediately comes to the DEMO.
If you have registered a Website user, you need to send an email before the user can continue to work after receiving confirmation from the email;
Assume that sending is a very time-consuming process, so asynchronous transmission is required.
1 namespace Note: add the task
Java code
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Beans xmlns = "http://www.springframework.org/schema/beans” xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance"
- Xmlns: p = "http://www.springframework.org/schema/p” xmlns: context =" http://www.springframework.org/schema/context"
- Xsi: schemaLocation ="
- Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
- <Context: component-scan base-package = "cs"/>
- </Beans>
2 RegularService. java registration class
Java code
- Import org. springframework. beans. factory. annotation. Autowired;
- Import org. springframework. stereotype. Service;
- Import cs. async. MailUtility;
- @ Service
- Public class RegularService {
- @ Autowired
- Private MailUtility mailUtility;
- Public void registerUser (String userName ){
- System. out. println ("User registration for" + userName + "complete ");
- MailUtility. sendMail (userName );
- System. out. println ("registration is complete, mail is sent later");
- }
- }
- 3 tools for sending emails
- <Pre name = "code" class = "java"> import org. springframework. scheduling. annotation. Async;
- Import org. springframework. stereotype. Component;
- @ Component
- Public class MailUtility {
- @ Async
- Public void sendMail (String name ){
- System. out. println ("Preparing for sending");
- Try {
- Thread. sleep (5000 );
- } Catch (InterruptedException e ){
- E. printStackTrace ();
- }
- System. out. println ("Asynchronous Transmission completed");
- }
- }
- </Pre>
- <Br>
- <Br> 4 Add the following in applicationContext. xml:
- <Br> <pre name = "code" class = "java"> <? Xml version = "1.0" encoding = "UTF-8"?>
- <Beans xmlns = "http://www.springframework.org/schema/beans” xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance"
- Xmlns: p = "http://www.springframework.org/schema/p” xmlns: context =" http://www.springframework.org/schema/context"
- Xmlns: task = "http://www.springframework.org/schema/task"
- Xsi: schemaLocation ="
- Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
- <Context: component-scan base-package = "cs"/>
- <Task: annotation-driven/>
- </Beans>
- </Pre>
- <Br> it must be <task: annotation-driven/>.
- <Br>
- <Br> 5 run:
- <Br> User registration for tom complete
- <Br> the registration is complete and the email will be sent later.
- <Br> preparing for sending
- <Br> asynchronous transmission is complete.
- <Br>
- <Br> 6. Sometimes, return values from the asynchronous method. At this time, spring will return a java. util. concurrent. Future object and call the get method, for example
- <Br> <pre name = "code" class = "java"> @ Async
- Public Future <Balance> findBalanceAsync (final Account account ){
- Balance balance = accountRepository. findBalance (account );
- Return new AsyncResult <Balance> (balance );
- }
- Balance balance = future. get ();
- </Pre>
- <Br>
- <Br>
- Note: Circular dependency must be resolved.
- Principle: spring scans beans to check whether @ async annotations are included in the method. If any annotation is included, spring dynamically generates a subclass for the bean, we call it a proxy class (?), The proxy class inherits the bean we wrote and injects the proxy class into it. At this time, when this method is executed, it will be in the proxy class. The proxy class determines that this method needs to be executed asynchronously, it will not call the corresponding method of the parent class (the bean we originally wrote. Spring maintains a queue by itself. It puts the methods to be executed into the queue, waits for the thread pool to read the queue, completes method execution, and completes asynchronous functions.
- Solve the cycle dependency caused by spring @ Async
Today, the project (spring3.0.6 + structs2.2.3) is reduced. The business layer beans are all annotated with @ Service, and the set injection is replaced with @ Autowired. All the business bean configurations are cleared from the xml configuration file.
In this case, the bean reporting loop dependency (reference) that specifically handles asynchronous operations is as follows ):
Bean with name '********** 'has been injected into other beans [******,**********, **********, * ********] in its raw version as part of a circular reference beanA injection is used for asynchronous processing of beanB (Methods containing @ Async annotations ), beanB injects beanA to Implement Asynchronous processing.
Solution: beanC (excluding @ Async annotation) is the beanB proxy service for beanA injection asynchronous processing. beanC injects beanB for processing.