JBoss.org Community Documentation

11.5. EJB Timer Configuration

The J2EE timer service allows for any EJB object to register for a timer callback either at a designated time in the future. Timer events can be used for auditing, reporting or other cleanup tasks that need to need to happen at some given time in the future. Timer events are intended to be persistent and should be executed even in the event of a server failure. Coding to EJB timers is a standard part of the J2EE specification, so we won't explore the programming model. We will, instead, look at the configuration of the timer service in JBoss so that you can understand how to make timers work best in your environment

The EJB timer service is configure by several related MBeans in the ejb-deployer.xml file. The primary MBean is the EJBTimerService MBean.

<mbean code="org.jboss.ejb.txtimer.EJBTimerServiceImpl" name="jboss.ejb:service=EJBTimerService">
			<attribute name="RetryPolicy">jboss.ejb:service=EJBTimerService,retryPolicy=fixedDelay</attribute>
			<attribute name="PersistencePolicy">jboss.ejb:service=EJBTimerService,persistencePolicy=database</attribute>
			<attribute name="TimerIdGeneratorClassName">org.jboss.ejb.txtimer.BigIntegerTimerIdGenerator</attribute>
			<attribute name="TimedObjectInvokerClassName">org.jboss.ejb.txtimer.TimedObjectInvokerImpl</attribute>
			</mbean> 
		

The EJBTimerService has the following configurable attributes:

  • RetryPolicy : This is name of the MBean that implements the retry policy. The MBean must support the org.jboss.ejb.txtimer.RetryPolicy interface. JBoss provides one implementation, FixedDelayRetryPolicy, which will be described later.

  • PersistencePolicy : This is the name of the MBean that implements the the persistence strategy for saving timer events. The MBean must support the org.jboss.ejb.txtimer.PersistencePolicy interface. JBoss provides two implementations, NoopPersistencePolicy and DatabasePersistencePolicy, which will be described later.

  • TimerIdGeneratorClassName : This is the name of a class that provides the timer ID generator strategy. This class must implement the org.jboss.ejb.txtimer.TimerIdGenerator interface. JBoss provides the org.jboss.ejb.txtimer.BigIntegerTimerIdGenerator implementation.

  • TimedObjectInvokerClassname : This is the name of a class that provides the timer method invocation strategy. This class must implement the org.jboss.ejb.txtimer.TimedObjectInvoker interface. JBoss provides the org.jboss.ejb.txtimer.TimedObjectInvokerImpl implementation.

The retry policy MBean definition used is shown here:

<mbean code="org.jboss.ejb.txtimer.FixedDelayRetryPolicy"
			name="jboss.ejb:service=EJBTimerService,retryPolicy=fixedDelay">
			<attribute name="Delay">100</attribute>
			</mbean>
		

The retry policy takes one configuration value:

  • Delay : This is the delay (ms) before retrying a failed timer execution. The default delay is 100ms.

If EJB timers do not need to be persisted, the NoopPersistence policy can be used. This MBean is commented out by default, but when enabled will look like this:

<mbean code="org.jboss.ejb.txtimer.NoopPersistencePolicy" 
			name="jboss.ejb:service=EJBTimerService,persistencePolicy=noop"/>
		

Most applications that use timers will want timers to be persisted. For that the DatabasePersitencePolicy MBean should be used.

<mbean code="org.jboss.ejb.txtimer.DatabasePersistencePolicy" 
			name="jboss.ejb:service=EJBTimerService,persistencePolicy=database">
			<!-- DataSource JNDI name -->
			<depends optional-attribute-name="DataSource">jboss.jca:service=DataSourceBinding,name=DefaultDS</depends>
			<!-- The plugin that handles database persistence -->
			<attribute name="DatabasePersistencePlugin">org.jboss.ejb.txtimer.GeneralPurposeDatabasePersistencePlugin</attribute>
			</mbean>
		
  • DataSource : This is the MBean for the DataSource that timer data will be written to.

  • DatabasePersistencePlugin : This is the name of the class the implements the persistence strategy. This should be org.jboss.ejb.txtimer.GeneralPurposeDatabasePersistencePlugin.