How to implement cron in Enterprise application

One my colleague asked me for implementing cron functionality in J2EE applicationthat is deployed in Sun application server. He needs to execute scheduled commandsin his EJB module. I suggested folowing solution that consists of Timer bean and lifecycle module that starts the timer when the server is started.
I know that better solution is used lifecycle module that is connected to EJB module only. For instance, where the module is deployed the lifecycle listener can invoke the bean. However, Sun Application server doesn't support this type lifecycle listener. The WebLogic server supports lifecycle listeneres on module application level, see here. Other solution is use servlet life cycle listener in web tier but this suggestion adds additional dependency for J2EE application. Since I didn't find better solution I used lifecycle module and Timer bean. Steps are below:
  • Create session bean that implements TimedObject interface. Then you should implement ejbTimout method that is invoked when the time expires.
  • Then create business method that creates interval timer:
         public void startTimer() {
            TimerService timerServ = context.getTimerService();
             // create interval timer
            Timer timer = timerServ.createTimer(new Date(), 5000, "Timer"); 
        }
      
  • Deploy EJB module and then we should create lifecycle listener that invokes startTimer method. Create new J2SE project and add appserv-rt.jar and j2ee.jar on project classpath
  • Create new class that implements com.sun.appserv.server.LifecycleListener interface.
  • Implements handleEvent method:
         public void handleEvent(LifecycleEvent lifecycleEvent) 
               throws ServerLifecycleException {
            // start timer when server is ready to service requests
            if(lifecycleEvent.getEventType() == LifecycleEvent.READY_EVENT){
                LifecycleEventContext lfcECtx = lifecycleEvent.getLifecycleEventContext();
                try {
                    lfcECtx.log("Try to lookup Timer's bean home: " + EJB_NAME);
                    InitialContext ctx = 
                              lifecycleEvent.getLifecycleEventContext().getInitialContext();
                    Object obj = ctx.lookup(EJB_NAME);
                    TimerSessionRemote timer =
                      ((TimerSessionRemoteHome)PortableRemoteObject.narrow(obj,
                                        TimerSessionRemoteHome.class)).create();
                    lfcECtx.log("Create Timer bean");
                    timer.createTimer();
  • Build project and copy jar file in lib directory that is in your domain. Don't forget to copy all libraries and EJB's stubs as well.
  • Go to web admin console and select node Applications - LifecycleModules. Click New button and create new Lifecycle module.
  • Restart application server and ejbTimeout method should be invoked every 5000 ms.
Comments:

There needs to be a mechanism in the EJB spec to support the EJB container lifecycle. The part I hate about this solution is the vendor specific lifecycle class. There are ServletContextListeners in the web tier, why can't we have something similar in the EJB tier that is standard?

Posted by guest on February 21, 2006 at 09:53 AM CET #

Self management framework in GlassFish has predefined "timer" event. This timer event has similar capabilities as javax.management.timer package. Timer event can be defined as one time event at specified time or a repetitive event with periodicity and number of occurrences specified. </br> So the basic infrastructure is available and no piece of code is required to be written as far as saying call this business logic at this point of time and at a interval of for this many number of times. </br> Now, the business logic can be written as an MBean which implements javax.management.NotificationListener. Once this is done, a management rule can be created with event being "timer" and action being the MBean with the business logic. </br> Some useful info:</br> Self Management Framework in GlassFish</br> GlassFish : Self Management Rules</br> GlassFish Project : Self Management home </br>

Posted by sankara on February 21, 2006 at 11:53 PM CET #

Thanks sankara for your comment, I will try to rewrite the sample. I didn't know about this solution. I agree that suggested solutions has a server specific dependency. However, I didn't find better before :-(

Posted by Petr on February 22, 2006 at 05:23 AM CET #

The solution described by sankara is avalible in Glassfish only but it's not avalaible in App server 8.1

Posted by Petr on February 22, 2006 at 05:47 AM CET #

Post a Comment:
Comments are closed for this entry.
About

pblaha

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks