aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Duplicate Timers in JBOSS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Duplicate Timers in JBOSS" Watch "Duplicate Timers in JBOSS" New topic
Author

Duplicate Timers in JBOSS

Stu Quinn
Ranch Hand

Joined: Feb 27, 2009
Posts: 35
Good day,

I have used Quartz with no real problems in Tomcat. Now I am using EJB3 over JBOSS 4.2.2 and it looks straight-forward, however I have run into an issue. I seem to end up with multiple instances of the timer. After I have deployed / restarted several times I end up with my job executing repeatedly. Obviously I am not approaching this correctly as the JBOSS is persisting the timer (which is nice), but I am somehow generating duplicates.

First I am creating my timed job the first time my session bean is initialized. Below is my code:



So far, so good, or so I thought. After multiple redeploys and restarts when working on my code I found that the job was running repeatedly with multiple different timer IDs. Following is the log:


16:25:51,566 INFO [PromisPartSpecBean] scheduledJob(Timer timer=[id=1251501729393,target=[target=jboss.j2ee:service=EJB3,ear=OnPromisPartSpecEAR.ear,jar=OnPromisPartSpec.jar,name=PromisPartSpecBean],remaining=17546,periode=60000,canceled_in_tx]) - end
16:25:51,597 INFO [PromisPartSpecBean] updateFwPlans() - end
16:25:51,597 INFO [PromisPartSpecBean] scheduledJob(Timer timer=[id=1251501729637,target=[target=jboss.j2ee:service=EJB3,ear=OnPromisPartSpecEAR.ear,jar=OnPromisPartSpec.jar,name=PromisPartSpecBean],remaining=34179,periode=60000,canceled_in_tx]) - end
16:25:51,644 INFO [OnFwPartSpecBean] init() - start
16:25:51,644 INFO [OnFwPartSpecBean] init() - end
16:25:51,644 INFO [OnFwPartSpecBean] findFwPlans() - start
16:25:51,644 INFO [FwPartSpecDataDelegate] findFwPlans() - start
16:25:51,644 INFO [PromisPartSpecBean] updateFwPlans() - end
16:25:51,644 INFO [PromisPartSpecBean] scheduledJob(Timer timer=[id=1251501729709,target=[target=jboss.j2ee:service=EJB3,ear=OnPromisPartSpecEAR.ear,jar=OnPromisPartSpec.jar,name=PromisPartSpecBean],remaining=34382,periode=60000,canceled_in_tx]) - end
16:25:51,706 INFO [PromisPartSpecBean] updateFwPlans() - end
16:25:51,722 INFO [PromisPartSpecBean] scheduledJob(Timer timer=[id=1251501729407,target=[target=jboss.j2ee:service=EJB3,ear=OnPromisPartSpecEAR.ear,jar=OnPromisPartSpec.jar,name=PromisPartSpecBean],remaining=33210,periode=60000,canceled_in_tx]) - end
16:25:52,175 INFO [FwPartSpecDataDelegate] findFwPlans() - end
16:25:52,175 INFO [OnFwPartSpecBean] findFwPlans() - end
16:25:52,285 INFO [PromisPartSpecBean] updateFwPlans() - end
16:25:52,285 INFO [PromisPartSpecBean] scheduledJob(Timer timer=[id=1251501729560,target=[target=jboss.j2ee:service=EJB3,ear=OnPromisPartSpecEAR.ear,jar=OnPromisPartSpec.jar,name=PromisPartSpecBean],remaining=-1384392,periode=60000,canceled_in_tx]) - end

I am obviously missing some basic aspect about creating and managing timers. I would sure appreciate a little help here.

Thanks,
Stu
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

I think you are starting the timer each time your application starts. Obviously jBoss is persisting your timers (which you know). So basically do you want to turn of timer persistence or you can get a list of all timers, and then check if your timer is already running??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Stu Quinn
Ranch Hand

Joined: Feb 27, 2009
Posts: 35
Ankit,

I would like to retain persistence of the timers within the app serve, so yes what I am looking for is how to query the timer service to determine if my timer already exists. I would then check the persisted timer to see if the interval was the same. If it is the same then I will not create it again. If I had changed the interval I would delete the prior instance of the timer and create it again.

Thank you for taking time to help. Life is good at the ranch.
Stu
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

In your bean try using TimerService.getTimers(), that will return a list of timers, then you can check the next timeout of the timers using Timer.getNextTimeout(). I've never tried this but I had read it so should work I think...
Stu Quinn
Ranch Hand

Joined: Feb 27, 2009
Posts: 35
This was just what I needed. From the JavaDoc:


getTimers()
Get all the active timers associated with this bean.


I had expected the Timer to include the interval, but no such luck. However, when you create the timer you can pass an object so I just passed in the interval in for the 'info' parameter and now I am able check to see if the interval has changed. If so I clear the timer and re-create it, otherwise I take no action. Here is the code.



Lastly, the use of the TimerService is not supported in an @PostConstruct method. It might work some of the time, but may also throw some unexpected errors.

All is well.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Duplicate Timers in JBOSS