@Async java 异步方法

时间:2023-03-08 20:14:44
@Async java 异步方法

在spring 3中,@Async注解能让某个方法快速变为异步执行,马上来先DEMO上手下。

假如在网站的用户注册后,需要发送邮件,然后用户得到邮件确认后才能继续其他工作; 
假设发送是一个很耗费时间的过程,因此需要异步。

1 namespace要注意,加上task

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  3. xmlns:p=”http://www.springframework.org/schema/p” xmlns:context=”http://www.springframework.org/schema/context”
  4. xsi:schemaLocation=”
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  7. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd”>
  8. <context:component-scan base-package=”cs”/>
  9. </beans>

2 RegularService.java 注册类

    1. import org.springframework.beans.factory.annotation.Autowired;
    2. import org.springframework.stereotype.Service;
    3. import cs.async.MailUtility;
    4. @Service
    5. public class RegularService {
    6. @Autowired
    7. private MailUtility mailUtility ;
    8. public void registerUser(String userName){
    9. System.out.println(” User registration for  “+userName +” complete”);
    10. mailUtility.sendMail(userName);
    11. System.out.println(” 注册完成,邮件稍后发送“);
    12. }
    13. }
    14. 3 发送邮件的工具类
    15. <pre name="code" class="java">import org.springframework.scheduling.annotation.Async;
    16. import org.springframework.stereotype.Component;
    17. @Component
    18. public class MailUtility {
    19. @Async
    20. public void sendMail(String name){
    21. System.out.println(” 在做发送准备工作中  “);
    22. try {
    23. Thread.sleep(5000);
    24. } catch (InterruptedException e) {
    25. e.printStackTrace();
    26. }
    27. System.out.println(” 异步发送完毕“);
    28. }
    29. }
    30. </pre>
    31. <br>
    32. <br>4 最后在applicationContext.xml中加入:
    33. <br>  <pre name="code" class="java"><?xml version=”1.0″ encoding=”UTF-8″?>
    34. <beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
    35. xmlns:p=”http://www.springframework.org/schema/p” xmlns:context=”http://www.springframework.org/schema/context”
    36. xmlns:task=”http://www.springframework.org/schema/task”
    37. xsi:schemaLocation=”
    38. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    39. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    40. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd“>
    41. <context:component-scan base-package=”cs”/>
    42. <task:annotation-driven/>
    43. </beans>
    44. </pre>
    45. <br>   就是<task:annotation-driven/>这个一定不能少喔。
    46. <br>
    47. <br>5 运行:
    48. <br>   User registration for  tom complete
    49. <br>  注册完成,邮件稍后发送
    50. <br>在做发送准备工作中
    51. <br>异步发送完毕
    52. <br>
    53. <br>6 有的时候,要从异步中返回值,这个时候,spring会返回一个java.util.concurrent.Future对象,要调用其中的get方法,比如
    54. <br>   <pre name="code" class="java">@Async
    55. public Future<Balance> findBalanceAsync(final Account account) {
    56. Balance balance = accountRepository.findBalance(account);
    57. return new AsyncResult<Balance>(balance);
    58. }
    59. Balance balance = future.get();
    60. </pre>
    61. <br>
    62. <br>
    63. 注意事项:必须解决循环依赖
    64. 原理: spring 在扫描bean的时候会扫描方法上是否包含@async的注解,如果包含的,spring会为这个bean动态的生成一个子类,我们称之为代理类(?),代理类是继承我们所写的bean的,然后把代理类注入进来,那此时,在执行此方法的时候,会到代理类中,代理类判断了此方法需要异步执行,就不会调用父类(我们原本写的bean)的对应方法。spring自己维护了一个队列,他会把需要执行的方法,放入队列中,等待线程池去读取这个队列,完成方法的执行,从而完成了异步的功能。
    65. 今天对项目工程(spring3.0.6+structs2.2.3)进行瘦身,业务层bean统一用@Service注解,set注入用@Autowired替换,从xml配置文件中将业务bean配置全部清掉。
      这时专门处理异步操作的bean报循环依赖(引用):
      Bean with name ‘*********’ has been injected into other beans [******, **********, **********, **********] in its raw version as part of a circular reference具体情况是beanA注入用于异步处理的beanB(含有@Async注解的方法),用于对某些操作进行异步处理,而beanB又注入beanA用于实现异步处理
      解决方案:beanA注入异步处理的beanB的代理服务beanC(不含@Async注解),再由beanC注入beanB进行处理