File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JBoss/WildFly and the fly likes EJB Timers in JBoss Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "EJB Timers in JBoss" Watch "EJB Timers in JBoss" New topic
Author

EJB Timers in JBoss

Sanjeev Verma
Ranch Hand

Joined: Aug 24, 2000
Posts: 87
Hi All:

I am observing some strange behavior in a MDB that I am deploying to a JBoss server (v5.1.0). This MDB also has a EJB3 timer component to it. My current strategy is like this:

1. I have a ServletContextListener impl as part of my application. This impl will execute as the application starts up, and as part of execution, it will send a message to the above timer MDB.
2. On receipt of this message, the MDB will configure the timer from inside the onMessage method, which in my case, is to execute the @Timeout method first time after 5 minutes, and then after every 30 minutes.

The behavior I see is actually quite strange. First of all, for some reason I am not able to send a message to the queue on which my MDB is listening, but that is a question I am pursuing in another thread here. However, using a standalone java client, when I do activate the MDB, I see that the MDB sets up the timer appropriately. So now when I shut down the server and start it up back again, my expectation is w/o the initialization, the Timer inside the MDB will not fire, but that is not the case! So the second time around if I again initialize the MDB with a message, now I will have two timers executing the @Timeout method! and if I shutdown the server and repeat this process again, I will have 3 timers now, and so on.

I did some research, and I think I know what might be going on. In JBoss, timers are persisted by default, so every time I create a timer, it is persisted across server restarts, I think. Inside my server install, I found this text inside the ejb2-timer-service.xml:



I think all I need to do is to uncomment the "A persistence policy that does not persist the timer" and comment out the "persistence policy that persists timers to a database". Is that correct? And, more importantly, is that the right thing to do in the case of my application, given the scenario that I have described above? What have others experienced and what is the best practice around this? Please let me know.

Thanks is advance for looking!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10046
    
163

Using persistent or non-persistent timers is something that your application has to decide. For example, what behaviour do you want to see if a timer is scheduled 30 minutes from now and the server is stopped before the timer is triggered? Would you want to trigger it when the server is up (let's say 5 hours later)?

I think all I need to do is to uncomment the "A persistence policy that does not persist the timer" and comment out the "persistence policy that persists timers to a database". Is that correct?


That's correct. But in addition you also have to use the uncommented noop policy in the timer service:



[My Blog] [JavaRanch Journal]
Sanjeev Verma
Ranch Hand

Joined: Aug 24, 2000
Posts: 87
Thanks for the response Jaikiran, very appreciated.

Jaikiran Pai wrote:Using persistent or non-persistent timers is something that your application has to decide. For example, what behaviour do you want to see if a timer is scheduled 30 minutes from now and the server is stopped before the timer is triggered? Would you want to trigger it when the server is up (let's say 5 hours later)?


As I explained above, I am designing the app such that on every start up, the timer is initialized. So I think I am ok with the timer not being persisted across server restarts. Thanks for clarifying! One more related question - what impact, if any, will using a non persistent timer have on the transactional behavior of the timer? If there is any, then I will have to rethink my design.

Thanks again for all your help!
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10046
    
163

Sanjeev Verma wrote: what impact, if any, will using a non persistent timer have on the transactional behavior of the timer? If there is any, then I will have to rethink my design.


A non-persistence timer would still be transactional. It just won't be able to maintain state across restarts.
Sanjeev Verma
Ranch Hand

Joined: Aug 24, 2000
Posts: 87
Jaikiran Pai wrote:A non-persistence timer would still be transactional. It just won't be able to maintain state across restarts.


That is exactly the answer I was hoping for, thanks! As I mentioned, app design takes care of timer initialization in my case, so i think I am good then.

Thanks much for you help, very appreciated!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB Timers in JBoss