Ejb2.1 & ejb3.0: timer service Trilogy

Source: Internet
Author: User
For scheduling tasks in J2EE, before ejb2.1 was launched, it seems that only some very good ways can be used: for example, a new timer in servlet.
We urgently need the standard implementation of EJB.
We have recently met such a requirement. Fortunately, we upgraded Websphere to 6.0 and supported the ejb2.1 specification. In the ejb2.1 standard, a standard implementation of timer service is provided.
To implement the timer service, a stateless sessionbean is used to implement the timedobject interface.

  1. Public interface timedobject {
  2. Public void ejbtimeout (timer );
  3. }

The simplest timer service is as follows (implemented by a stateless sessionbean, the home and local interfaces are omitted)

Java code
  1. Public class demotimerservicebean implements javax. EJB. sessionbean, timedobject
  2. {
  3. Private javax. EJB. sessioncontext mysessionctx;
  4. /**
  5. * Getsessioncontext
  6. */
  7. Public javax. EJB. sessioncontext getsessioncontext (){
  8. Return mysessionctx;
  9. }
  10. /**
  11. * Setsessioncontext
  12. */
  13. Public void setsessioncontext (javax. EJB. sessioncontext CTX ){
  14. Mysessionctx = CTX;
  15. }
  16. /**
  17. * Ejbcreate
  18. */
  19. Public void ejbcreate () throws javax. EJB. createexception
  20. {
  21. }
  22. /**
  23. * Ejbactivate
  24. */
  25. Public void ejbactivate (){
  26. }
  27. /**
  28. * Ejbpassivate
  29. */
  30. Public void ejbpassivate (){
  31. }
  32. /**
  33. * Ejbremove
  34. */
  35. Public void ejbremove ()
  36. {
  37. }
  38. Public void starttimer ()
  39. {
  40. Timerservice Ts = This. getsessioncontext (). gettimerservice ();
  41. Timer timer = ts. createtimer (new date (), 1000, "timerinfo ");
  42. }
  43. /* (Non-javadoc)
  44. * @ See javax. EJB. timedobject # ejbtimeout (javax. EJB. Timer)
  45. */
  46. Public void ejbtimeout (timer arg0)
  47. {
  48. // Implements your business logic.
  49. System. Out. println ("process your business logic ");
  50. Taskhandler. Process ();
  51. }
  52. }

Starttimer () is used to start a scheduling task. When the timeout time of the scheduling task arrives, the EJB container calls back the ejbtimeout () method. The preceding example executes the ejbtimeout () method every second.
Note that the customer needs to call the starttimer () method to start the timer service, and the timer service has the persistence feature, that is, if a timer service has been started, if
After the server is restarted, the timer service will continue to be executed (you do not need to call the starttimer () method again ).
In fact, we can put the starttimer () method in ejbcreate () and start scheduling when EJB is instantiated. However, we need to modify the scheduling policy because the container will call sessionbean's
Create () method. If a sessionbean is instantiated, a new scheduling is generated. This is obviously not what we need.
Therefore, the modified policy is:
If a timer service with the same name already exists, give up. Otherwise, a new timer service is generated.

Java code
  1. Public void starttimer ()
  2. {
  3. Timerservice Ts = This. getsessioncontext (). gettimerservice ();
  4. Iterator timers = ts. gettimers (). iterator ();
  5. While (timers. hasnext ())
  6. {
  7. Timer timer = (timer) timers. Next ();
  8. String info = (string) timer. getinfo ();
  9. System. Out. println ("timer info =" + info );
  10. If (info. Equals ("timerinfo "))
  11. {
  12. System. Out. println ("there is already a timer = timerinfo, so return ");
  13. Return;
  14. }
  15. }
  16. TS. createtimer (new date (), 1000, "timerinfo ");
  17. }

Paste a complete timer service implementation for your reference, including starting and stopping the timer Service (implemented by a stateless sessionbean, omitted by the home and local interfaces)

Java code
  1. Public class demotimerservicebean implements javax. EJB. sessionbean, timedobject
  2. {
  3. Private Static final string congig_bean_name = "configbean ";
  4. Private javax. EJB. sessioncontext mysessionctx;
  5. /**
  6. * Getsessioncontext
  7. */
  8. Public javax. EJB. sessioncontext getsessioncontext (){
  9. Return mysessionctx;
  10. }
  11. /**
  12. * Setsessioncontext
  13. */
  14. Public void setsessioncontext (javax. EJB. sessioncontext CTX ){
  15. Mysessionctx = CTX;
  16. }
  17. /**
  18. * Ejbcreate
  19. */
  20. Public void ejbcreate () throws javax. EJB. createexception
  21. {
  22. }
  23. /**
  24. * Ejbactivate
  25. */
  26. Public void ejbactivate (){
  27. }
  28. /**
  29. * Ejbpassivate
  30. */
  31. Public void ejbpassivate (){
  32. }
  33. /**
  34. * Ejbremove
  35. */
  36. Public void ejbremove ()
  37. {
  38. }
  39. Public void stopalltimer ()
  40. {
  41. Timerservice Ts = This. getsessioncontext (). gettimerservice ();
  42. Iterator timers = ts. gettimers (). iterator ();
  43. While (timers. hasnext ())
  44. {
  45. Timer timer = (timer) timers. Next ();
  46. Timer. Cancel ();
  47. }
  48. }
  49. Public void stoptimer (string timername)
  50. {
  51. Timerservice Ts = This. getsessioncontext (). gettimerservice ();
  52. Iterator timers = ts. gettimers (). iterator ();
  53. While (timers. hasnext ())
  54. {
  55. Timer timer = (timer) timers. Next ();
  56. String info = (string) timer. getinfo ();
  57. System. Out. println ("timer info =" + info );
  58. If (info. Equals (timername ))
  59. {
  60. System. Out. println ("there is already a timer =" + timername + ", so we cancel it! ");
  61. Timer. Cancel ();
  62. }
  63. }
  64. }
  65. Public arraylist getalltimerinfo ()
  66. {
  67. Arraylist timerlist = new arraylist ();
  68. Timerservice Ts = This. getsessioncontext (). gettimerservice ();
  69. Iterator timers = ts. gettimers (). iterator ();
  70. While (timers. hasnext ())
  71. {
  72. Timer timer = (timer) timers. Next ();
  73. String info = (string) timer. getinfo ();
  74. Timerlist. Add (Info );
  75. }
  76. Return timerlist;
  77. }
  78. Public void starttimer ()
  79. {
  80. Object configobj = mybeanfactory. instance (). getbean (congig_bean_name );
  81. If (configobj = NULL)
  82. {
  83. System. Out. println ("can't get configbean from spring config file. Config = NULL ");
  84. Return;
  85. }
  86. Config = (config) configobj;
  87. Long interval = config. getinterval ();
  88. String timerinfo = config. gettimerinfo ();
  89. This. stoptimer (timerinfo );
  90. System. Out. println ("start a timer and info =" + timerinfo + ", interval =" + interval );
  91. Timerservice Ts = This. getsessioncontext (). gettimerservice ();
  92. Timer timer = ts. createtimer (new date (), interval, timerinfo );
  93. }
  94. /* (Non-javadoc)
  95. * @ See javax. EJB. timedobject # ejbtimeout (javax. EJB. Timer)
  96. */
  97. Public void ejbtimeout (timer arg0)
  98. {
  99. // Todo auto-generated method stub
  100. System. Out. println ("in ejbtimeout now .");
  101. Taskhandler. Process ();
  102. }
  103. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.