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.
- Public interface timedobject {
- Public void ejbtimeout (timer );
- }
The simplest timer service is as follows (implemented by a stateless sessionbean, the home and local interfaces are omitted)
Java code
- Public class demotimerservicebean implements javax. EJB. sessionbean, timedobject
- {
- Private javax. EJB. sessioncontext mysessionctx;
- /**
- * Getsessioncontext
- */
- Public javax. EJB. sessioncontext getsessioncontext (){
- Return mysessionctx;
- }
- /**
- * Setsessioncontext
- */
- Public void setsessioncontext (javax. EJB. sessioncontext CTX ){
- Mysessionctx = CTX;
- }
- /**
- * Ejbcreate
- */
- Public void ejbcreate () throws javax. EJB. createexception
- {
- }
- /**
- * Ejbactivate
- */
- Public void ejbactivate (){
- }
- /**
- * Ejbpassivate
- */
- Public void ejbpassivate (){
- }
- /**
- * Ejbremove
- */
- Public void ejbremove ()
- {
- }
- Public void starttimer ()
- {
- Timerservice Ts = This. getsessioncontext (). gettimerservice ();
- Timer timer = ts. createtimer (new date (), 1000, "timerinfo ");
- }
- /* (Non-javadoc)
- * @ See javax. EJB. timedobject # ejbtimeout (javax. EJB. Timer)
- */
- Public void ejbtimeout (timer arg0)
- {
- // Implements your business logic.
- System. Out. println ("process your business logic ");
- Taskhandler. Process ();
- }
- }
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
- Public void starttimer ()
- {
- Timerservice Ts = This. getsessioncontext (). gettimerservice ();
- Iterator timers = ts. gettimers (). iterator ();
- While (timers. hasnext ())
- {
- Timer timer = (timer) timers. Next ();
- String info = (string) timer. getinfo ();
- System. Out. println ("timer info =" + info );
- If (info. Equals ("timerinfo "))
- {
- System. Out. println ("there is already a timer = timerinfo, so return ");
- Return;
- }
- }
- TS. createtimer (new date (), 1000, "timerinfo ");
- }
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
- Public class demotimerservicebean implements javax. EJB. sessionbean, timedobject
- {
- Private Static final string congig_bean_name = "configbean ";
- Private javax. EJB. sessioncontext mysessionctx;
- /**
- * Getsessioncontext
- */
- Public javax. EJB. sessioncontext getsessioncontext (){
- Return mysessionctx;
- }
- /**
- * Setsessioncontext
- */
- Public void setsessioncontext (javax. EJB. sessioncontext CTX ){
- Mysessionctx = CTX;
- }
- /**
- * Ejbcreate
- */
- Public void ejbcreate () throws javax. EJB. createexception
- {
- }
- /**
- * Ejbactivate
- */
- Public void ejbactivate (){
- }
- /**
- * Ejbpassivate
- */
- Public void ejbpassivate (){
- }
- /**
- * Ejbremove
- */
- Public void ejbremove ()
- {
- }
- Public void stopalltimer ()
- {
- Timerservice Ts = This. getsessioncontext (). gettimerservice ();
- Iterator timers = ts. gettimers (). iterator ();
- While (timers. hasnext ())
- {
- Timer timer = (timer) timers. Next ();
- Timer. Cancel ();
- }
- }
- Public void stoptimer (string timername)
- {
- Timerservice Ts = This. getsessioncontext (). gettimerservice ();
- Iterator timers = ts. gettimers (). iterator ();
- While (timers. hasnext ())
- {
- Timer timer = (timer) timers. Next ();
- String info = (string) timer. getinfo ();
- System. Out. println ("timer info =" + info );
- If (info. Equals (timername ))
- {
- System. Out. println ("there is already a timer =" + timername + ", so we cancel it! ");
- Timer. Cancel ();
- }
- }
- }
- Public arraylist getalltimerinfo ()
- {
- Arraylist timerlist = new arraylist ();
- Timerservice Ts = This. getsessioncontext (). gettimerservice ();
- Iterator timers = ts. gettimers (). iterator ();
- While (timers. hasnext ())
- {
- Timer timer = (timer) timers. Next ();
- String info = (string) timer. getinfo ();
- Timerlist. Add (Info );
- }
- Return timerlist;
- }
- Public void starttimer ()
- {
- Object configobj = mybeanfactory. instance (). getbean (congig_bean_name );
- If (configobj = NULL)
- {
- System. Out. println ("can't get configbean from spring config file. Config = NULL ");
- Return;
- }
- Config = (config) configobj;
- Long interval = config. getinterval ();
- String timerinfo = config. gettimerinfo ();
- This. stoptimer (timerinfo );
- System. Out. println ("start a timer and info =" + timerinfo + ", interval =" + interval );
- Timerservice Ts = This. getsessioncontext (). gettimerservice ();
- Timer timer = ts. createtimer (new date (), interval, timerinfo );
- }
- /* (Non-javadoc)
- * @ See javax. EJB. timedobject # ejbtimeout (javax. EJB. Timer)
- */
- Public void ejbtimeout (timer arg0)
- {
- // Todo auto-generated method stub
- System. Out. println ("in ejbtimeout now .");
- Taskhandler. Process ();
- }
- }