黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

spring 元素 實現(xiàn)任務(wù)調(diào)度和異步方法

系統(tǒng) 2071 0
??? 在我們使用spring框架的過程中,在很多時候我們會使用@async注解來異步執(zhí)行某一些方法,提高系統(tǒng)的執(zhí)行效率。今天我們來探討下 spring 是如何完成這個功能的。?

??? spring 在掃描bean的時候會掃描方法上是否包含@async的注解,如果包含的,spring會為這個bean動態(tài)的生成一個子類,我們稱之為代理類(?),代理類是繼承我們所寫的bean的,然后把代理類注入進來,那此時,在執(zhí)行此方法的時候,會到代理類中,代理類判斷了此方法需要異步執(zhí)行,就不會調(diào)用父類(我們原本寫的bean)的對應(yīng)方法。spring自己維護了一個隊列,他會把需要執(zhí)行的方法,放入隊列中,等待線程池去讀取這個隊列,完成方法的執(zhí)行,從而完成了異步的功能。我們可以關(guān)注到再配置task的時候,是有參數(shù)讓我們配置線程池的數(shù)量的。 因為這種實現(xiàn)方法,所以在同一個類中的方法調(diào)用,添加@async注解是失效的! ,原因是當(dāng)你在同一個類中的時候,方法調(diào)用是在類體內(nèi)執(zhí)行的,spring無法截獲這個方法調(diào)用。?

??? 那在深入一步,spring為我們提供了AOP,面向切面的功能。他的原理和異步注解的原理是類似的,spring在啟動容器的時候,會掃描切面所定義的類。在這些類被注入的時候,所注入的也是代理類,當(dāng)你調(diào)用這些方法的時候,本質(zhì)上是調(diào)用的代理類。通過代理類再去執(zhí)行父類相對應(yīng)的方法,那spring只需要在調(diào)用之前和之后執(zhí)行某段代碼就完成了AOP的實現(xiàn)了!?

?? 那最后我們還有一個問題,spring是如何動態(tài)的生成某一個類的子類的?代理類??

?? 生成代理類可以通過jdk 和 CGLIB 兩種方式生成.具體的可以?
?? 參考 1.代理類說明: http://wenku.baidu.com/link?url=YpU3CNXsyLivMCnpILQ1qQc8PcKuqRrqZd1X8hPNQa9QuBFmbpCugSdjkXlY2L_ey4rUxM7TlwHeAatL65e664h_W8n0IKgTP1vFU5wacrm
??????? 2.CGLIB和JDK代理類的區(qū)別: http://www.blogjava.net/hello-yun/archive/2011/11/09/363359.html ?
這里就不詳細說明了?
?
?
在spring 3中,@Async注解能讓某個方法快速變?yōu)楫惒綀?zhí)行,馬上來先DEMO上手下。?

假如在網(wǎng)站的用戶注冊后,需要發(fā)送郵件,然后用戶得到郵件確認(rèn)后才能繼續(xù)其他工作;?
假設(shè)發(fā)送是一個很耗費時間的過程,因此需要異步。?

1 namespace要注意,加上task?
??
Java代碼?? 收藏代碼
  1. <?xml?version=” 1.0 ″?encoding=”UTF- 8 ″?>??
  2. ??
  3. <beans?xmlns=”http: //www.springframework.org/schema/beans”?xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” ??
  4. xmlns:p=”http: //www.springframework.org/schema/p”?xmlns:context=”http://www.springframework.org/schema/context” ??
  5. xsi:schemaLocation=”??
  6. http: //www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ??
  7. http: //www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.0.xsd ??
  8. http: //www.springframework.org/schema/task?http://www.springframework.org/schema/task/spring-task-3.0.xsd”> ??
  9. ??
  10. <context:component-scan?base- package =”cs”/>??
  11. ??
  12. </beans>??


2 RegularService.java 注冊類?
??
Java代碼?? 收藏代碼
  1. ? import ?org.springframework.beans.factory.annotation.Autowired;??
  2. import ?org.springframework.stereotype.Service;??
  3. ??
  4. import ?cs.async.MailUtility;??
  5. ??
  6. @Service ??
  7. public ? class ?RegularService?{??
  8. ??
  9. @Autowired ??
  10. private ?MailUtility?mailUtility?;??
  11. ??
  12. public ? void ?registerUser(String?userName){??
  13. ??
  14. System.out.println(”?User?registration? for ??“+userName?+”?complete”);??
  15. ??
  16. mailUtility.sendMail(userName);??
  17. ??
  18. System.out.println(”?注冊完成,郵件稍后發(fā)送“);??
  19. }??
  20. ??
  21. }??
  22. ??
  23. 3 ?發(fā)送郵件的工具類??
  24. import ?org.springframework.scheduling.annotation.Async;??
  25. import ?org.springframework.stereotype.Component;??
  26. ??
  27. @Component ??
  28. public ? class ?MailUtility?{??
  29. ??
  30. @Async ??
  31. public ? void ?sendMail(String?name){??
  32. ??
  33. System.out.println(”?在做發(fā)送準(zhǔn)備工作中??“);??
  34. ??
  35. try ?{??
  36. Thread.sleep( 5000 );??
  37. ??
  38. }? catch ?(InterruptedException?e)?{??
  39. ??
  40. e.printStackTrace();??
  41. }??
  42. ??
  43. System.out.println(”?異步發(fā)送完畢“);??
  44. ??
  45. }??
  46. ??
  47. }??
  48. ?
  49. ?
  50. 4 ?最后在applicationContext.xml中加入:??
  51. <?xml?version=” 1.0 ″?encoding=”UTF- 8 ″?>??
  52. ??
  53. <beans?xmlns=”http: //www.springframework.org/schema/beans”?xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” ??
  54. xmlns:p=”http: //www.springframework.org/schema/p”?xmlns:context=”http://www.springframework.org/schema/context” ??
  55. xmlns:task=”http: //www.springframework.org/schema/task” ??
  56. xsi:schemaLocation=”??
  57. http: //www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ??
  58. http: //www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.0.xsd ??
  59. http: //www.springframework.org/schema/task?http://www.springframework.org/schema/task/spring-task-3.0.xsd“> ??
  60. ??
  61. <context:component-scan?base- package =”cs”/>??
  62. ??
  63. <task:annotation-driven/>??
  64. ??
  65. </beans>??
  66. ? ?就是<task:annotation-driven/>這個一定不能少喔。 ?
  67. ?
  68. 5 ?運行:??
  69. ? User?registration? for ??tom?complete??
  70. ? 注冊完成,郵件稍后發(fā)送??
  71. ? 在做發(fā)送準(zhǔn)備工作中??
  72. ? 異步發(fā)送完畢??
  73. ?
  74. ? 6 ?有的時候,要從異步中返回值,這個時候,spring會返回一個java.util.concurrent.Future對象,要調(diào)用其中的get方法,比如??
  75. @Async ??
  76. public ?Future<Balance>?findBalanceAsync( final ?Account?account)?{??
  77. ????Balance?balance?=?accountRepository.findBalance(account);??
  78. ???? return ? new ?AsyncResult<Balance>(balance);??
  79. }??
  80. ??
  81. Balance?balance?=?future.get();??
  82. ??
  83. Spring3中加強了注解的使用,其中計劃任務(wù)也得到了增強,現(xiàn)在創(chuàng)建一個計劃任務(wù)只需要 兩步 就完成了:

    1. 創(chuàng)建一個Java類,添加一個無參無返回值的方法,在方法上用@Scheduled注解修飾一下;
    2. 在Spring配置文件中添加三個<task:**** />節(jié)點;

    最后說明一下,第一步創(chuàng)建的Java類要成為Spring可管理的Bean,可以直接寫在XML里,也可以@Component一下

    ?

    示例如下

    計劃任務(wù)類:

    Java代碼?? 收藏代碼
    1. /** ?
    2. ?*?com.zywang.spring.task.SpringTaskDemo.java ?
    3. ?*?@author?ZYWANG?2011-3-9 ?
    4. ?*/ ??
    5. package ?com.zywang.spring.task;??
    6. ??
    7. import ?org.springframework.scheduling.annotation.Scheduled;??
    8. import ?org.springframework.stereotype.Component;??
    9. ??
    10. /** ?
    11. ?*?Spring3?@Scheduled?演示 ?
    12. ?*?@author?ZYWANG?2011-3-9 ?
    13. ?*/ ??
    14. @Component ??
    15. public ? class ?SpringTaskDemo?{??
    16. ??
    17. ???? @Scheduled (fixedDelay?=? 5000 )??
    18. ???? void ?doSomethingWithDelay(){??
    19. ????????System.out.println( "I'm?doing?with?delay?now!" );??
    20. ????}??
    21. ??????
    22. ???? @Scheduled (fixedRate?=? 5000 )??
    23. ???? void ?doSomethingWithRate(){??
    24. ????????System.out.println( "I'm?doing?with?rate?now!" );??
    25. ????}??
    26. ??????
    27. ???? @Scheduled (cron?=? "0/5?*?*?*?*?*" )??
    28. ???? void ?doSomethingWith(){??
    29. ????????System.out.println( "I'm?doing?with?cron?now!" );??
    30. ????}??
    31. }??

    Spring配置文件:

    Xml代碼?? 收藏代碼
    1. <? xml ? version = "1.0" ? encoding = "UTF-8" ?> ??
    2. < beans ? xmlns = "http://www.springframework.org/schema/beans" ??
    3. ???? xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" ? xmlns:task = "http://www.springframework.org/schema/task" ??
    4. ???? xsi:schemaLocation ="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsd??
    5. ????????http://www.springframework.org/schema/task?http://www.springframework.org/schema/task/spring-task-3.0.xsd" > ??
    6. ???? <!--?Enables?the?Spring?Task?@Scheduled?programming?model?--> ??
    7. ???? < task:executor ? id = "executor" ? pool-size = "5" ? /> ??
    8. ???? < task:scheduler ? id = "scheduler" ? pool-size = "10" ? /> ??
    9. ???? < task:annotation-driven ? executor = "executor" ? scheduler = "scheduler" ? /> ??
    10. </ beans > ??

spring 元素 實現(xiàn)任務(wù)調(diào)度和異步方法


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論