“Before you speak, listen. Before you write, think. Before you spend, earn. Before you invest, investigate. Before you criticize, wait. Before you pray, forgive. Before you quit, try. Before you retire, save. Before you die, give.”

Tuesday, October 13, 2009

Scheduling solutions Timer Service in EJB3.0 vs. Quartz

As a developer you have to choose the best tool for the task at hand (no religious decisions based on vendors should be taken).
When I started to work with Spring Framework in 2006 I learned to implement jobs running at a certain interval by using quartz.As at the time I had only learned EJB 2.0 it seemed a huge feature for me.
The Spring Framework docs shows how you can do this. You can use cron expressions to set a time for execution , a repeat interval and whatever cron under Linux allows you to do.
The problem with this is that it is a bit more complicated and non-standard.

The EJB3 solution is to use the TimerService and call a method on a session bean(or message driven bean) whenever the timer expires. You use the @Timeout annotation for that method.
Here is an example for this. Although it is very easy to set some triggers without much pre-configuration (that's why you have a EJB3 container) it lacks some of the flexibility of Quartz.
I found a copy of EJB3 in Action at work and the author suggests using Quartz for more complicated tasks instead of the EJB3 scheduling support.
I agree on this issue.

Which one would I choose for a new system ? The answer is (consultant fashion): it depends.
Of course if you only have a servlet container (say Tomcat) you will need to go with the Spring/Quartz support (as non standard as it might be) but for a fully blown EJB3 container you can start with the TimerService and replace it as required as the customer requirements change(as non-standard or unprofessional as it might seem to do it).

No comments:

Post a Comment