If you want to execute scheduled tasks using EJB 2.1 Timer service, you might find this pattern usefull. It is inspired by EJB Command pattern and it fits very well with it. If somebody else already posted the same idea, I apologize!

As you might know, EJB 2.1 introduced Timer service into specification. If you want to schedule some job, your EJB should implement TimedObject interface which defines one method, ejbTimeout(), which is invoked after specified amount of time. For more information about this, you can check this article http://www2.theserverside.com/articles/article.tss?l=MonsonHaefel-Column4.

When EJB wants to schedule some process it creates Timer object. When creating Timer, it can store in it any Serializable attribute which can be used in ejbTimeout method. Now, if you already use EJB Command pattern, you know that commands are Serializable, so you can build generic Timer by passing your Commands to it. Later, ejbTimeout will get this Command and execute it. Here is the code:

public class SchedulerBean extends BaseSessionBean
TimedObject {

* @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)
public void ejbTimeout(Timer timer) {
log.debug("Timout expired!");
Command command = (Command) timer.getInfo();
try {
} catch (CommandException e) {
log.error("Error performing task " + command);


* Method which does scheduling
* @ejb.interface-method
public void schedule(Date startDate, long interval, Command task) {
TimerService timerService = this.getSessionContext().getTimerService();
log.debug("Created timer service");
log.info("Created timer service");
timerService.createTimer(startDate, interval, task);

public void executeCommand(Command command) throws CommandException {