Managing automatically created EJB persistent timers

Applications or modules use the javax.ejb.Schedule annotation or the schedule element in the XML deployment descriptor to instruct the application server to automatically create EJB timers. An MBean is provided for managing the creation of the automatically created EJB persistent timers.

About this task

The Liberty server creates the EJB automatic persistent timers for an application the first time the application is started within the server process. The timers, and an indication that they were created, are stored in the database that is associated with the PersistentExecutor instance that is configured for the EJB timer service. Even if the EJB persistent timer configuration on multiple servers specifies to use the same database tables, the timers are created only once, not once per server. After the server automatically creates persistent timers, they are not created again until you clear from the database the indication that they were created. Even if all of the automatically created timers are removed, the Liberty server does not create any of them again until the indication that they were created is also removed.

You can clear the indication that the automatically created timers were created either by manually clearing the timer database or by using the EJB timer service MBean. The EJB timer service MBean can be used either programmatically or through a tool such as JConsole. The interface for the EJB timer service MBean is: com.ibm.websphere.ejbcontainer.mbean.EJBPersistentTimerServiceMXBean.

Procedure

  1. Configure the application server to include an EJB feature that supports persistent timers and the rest connector in the server.xml file.
    <featureManager>
         <feature>servlet-3.1</feature>
         <feature>ejbPersistentTimer-3.2</feature>
         <feature>jdbc-4.1</feature>
         <feature>restConnector-2.0</feature>
    </featureManager>
  2. Configure the application server to include a data source for the EJB timer service in the server.xml file.
    <dataSource id="DefaultDataSource" jdbcDriverRef="DerbyEmbedded">
         <properties.derby.embedded createDatabase="create" databaseName="${server.config.dir}/data/EJBTimerDB"/>
    </dataSource>
    <jdbcDriver id="DerbyEmbedded" libraryRef="DerbyLib"/>
    <library id="DerbyLib">
         <file name="${server.config.dir}/derby/derby.jar"/>
    </library>
  3. Obtain the MBean server connection.
  4. Obtain the EJB persistent timer service MBean.
    import javax.management.MBeanServerConnection;
    import javax.management.ObjectName;
    import com.ibm.websphere.ejbcontainer.mbean.EJBPersistentTimerServiceMXBean;
    ...
    MBeanServerConnection mbsc = <step #3>;
    ObjectName on = new
    ObjectName("WebSphere:feature=ejbPersistentTimer,type=EJBPersistentTimerService,name=EJBPersistentTimerService");
    EJBPersistentTimerServiceMXBean timerServiceMBean =
         JMX.newMXBeanProxy(mbsc, on, EJBPersistentTimerServiceMXBean.class);
  5. Remove the automatically created persistent timers for the application.
    timerServiceMBean.removeAutomaticTimers("<application name>");

    The removeAutomaticTimers() method on the MBean removes both the automatically created timers and the indication that they were created. The next time that the server starts the application, the automatically created persistent timers are created.