Configuration options for persistent enterprise JavaBeans timers
Persistent EJB timers do not require any configuration beyond enabling the ejbPersistentTimer feature and setting up the default data source, which has an ID of DefaultDataSource, to point to a database. Optional configuration settings are available to control behaviors such as choosing a different data source, controlling when and how often the database is polled to look for persisted timer tasks, and whether and how often to retry failed or rolled back timer tasks.
EJB timer configuration is specified by an optional timerService configuration element. Configuration attributes for persistent EJB timers are further grouped under a persistentExecutor configuration. By default, the EJB timer service uses a persistent executor instance named defaultEJBPersistentTimerExecutor. It is possible to customize the EJB persistent timer configuration by configuring the timer service to use a different persistent executor instance. However, the best practice for customizing the EJB persistent timer configuration is to override the defaultEJBPersistentTimerExecutor instance so that you inherit default values from the defaultEJBPersistentTimerExecutor instance.
<persistentExecutor id="defaultEJBPersistentTimerExecutor" retryLimit="50"/>
- Customizing the database store for persistent timer tasks
- Persistent EJB timer tasks are persisted to a database. Configuration related to the database is grouped under the databaseStore configuration element. Unless configured otherwise, an instance of databaseStore named defaultDatabaseStore is used for persistent EJB timers as well as by other product features that require a database. An example of overriding the defaultDatabaseStore instance,
<databaseStore id="defaultDatabaseStore" dataSourceRef="DB2DataSource" tablePrefix="MYTIMERS"/>
An example of configuring the defaultEJBPersistentTimerExecutor instance to use a different databaseStore instance,<persistentExecutor id="defaultEJBPersistentTimerExecutor" taskStoreRef="MyDBStore"/> <databaseStore id="MyDBStore" dataSourceRef="DB2DataSource" tablePrefix="MYTIMERS"> <authData user="user1" password="password1"/> </databaseStore>
- Enabling and disabling persistent timer task execution
Persistent EJB timer tasks are enabled to run upon commit of the transaction from which they are scheduled. To prevent persistent timer tasks from running, you can configure the enableTaskExecution attribute with a value of false, in which case the EJB timer service still writes persistent timer tasks to the database but does not run them. If the value is switched to true, the EJB timer service will start running the timers that were previously scheduled as well as any new timers that are scheduled.
- Customizing polling of the persistent store for timer tasks
- A single initial poll of the persistent store is performed upon startup to locate any previously scheduled timer tasks. When tasks are scheduled, information about the scheduled next run time is persisted as well as kept in memory, making it unnecessary to do further polling of the persistent store. This behavior is ideal in many cases, but might not always be desirable.
- If the timer tasks have external dependencies on other services, the initial poll might occur too soon and timer tasks might fail due to the external services they require being unavailable. If this happens, an initial poll delay can be used to defer the initial poll by a fixed amount of time.
- If a large number of EJB persistent timers are scheduled, they might consume too much memory. In this case, you can configure a poll interval to periodically poll the persistent store for only the timer tasks that you want to run within the interval up until the next poll. The poll size further limits the number of timer tasks that can be read from the database each poll interval, which might cause some tasks to run late.
Example configuration:<persistentExecutor id="defaultEJBPersistentTimerExecutor" initialPollDelay="5m" pollInterval="10m" pollSize="200"/>
- Retries for failed and rolled back persistent timer tasks
When persistent EJB timer executions fail or are marked to roll back, they are retried one time immediately. If the immediate retry fails, they are retried at a fixed interval until successful. It is possible to limit the number of retry attempts by specifying a retry limit in the configuration. You can also control the interval between retries by specifying a retry interval in the configuration.
Example configuration:<persistentExecutor id="defaultEJBPersistentTimerExecutor" retryLimit="100" retryInterval="2m"/>