Monday February 20, 2006
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:
public void startTimer() {
TimerService timerServ = context.getTimerService();
// create interval timer
Timer timer = timerServ.createTimer(new Date(), 5000, "Timer");
}
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();
Posted by 207.203.254.110 on February 21, 2006 at 05:53 PM CET #
Posted by sankara on February 22, 2006 at 07:53 AM CET #
Posted by Petr on February 22, 2006 at 01:23 PM CET #
Posted by Petr on February 22, 2006 at 01:47 PM CET #