Monday, August 31, 2009

Daemon Applications in J2EE


Prior to ejb 3.0 spec there was no such concept of container provided timer services. But now an ejb (stateless + mdb) can schedule itself. Moreover container provided timer services are reliable (can survive container crash) and serializable (we can save timer handle for later reference e.g. for cancellation).
But still we cannot right daemon ejbs! It could have been possible if timer services were not transaction aware so we could create timer in PostConstruct callback. But since timer services are transaction aware and PostConstruct callback is not, so we cannot do this. How to get away with it? Startup servlet is the solution, it will call our dameon ejb on first instance and from there we can schedule for regular intervals.
Other point that must be kept in mind while using ejb timer services is that they are not real time as per specs i.e. we cannot expect container to expire timer in millis. Programming model does not restrict it as interval unit is in millis but container does not provide any guarantees for real time. So, for this you are bound to use schedulers e.g. Quartz. I have seen two ways, to integrate quartz in j2ee
1-      Startup servlet approach as mentioned above. http://www.theserverside.com/news/thread.tss?thread_id=24896
2-       A resource adapter with Message beans as endpoints see JBoss for an implementation. This is really cool way of achieving this. http://www.jboss.org/ejb3/docs/tutorial/jca/inflow/quartz/quartz.html

1 comment:

Tahir Rauf said...

Nice effort bro !!! Keep it up..