This service implements a PersistencePolicy that persistes the timer to a
database.
| Method from org.jboss.ejb.txtimer.DatabasePersistencePolicy Detail: |
public void clearTimers() {
try
{
dbpPlugin.clearTimers();
}
catch (SQLException e)
{
log.warn("Unable to clear timers", e);
}
}
Delete all persisted timers |
public void deleteTimer(String timerId,
TimedObjectId timedObjectId) {
try
{
dbpPlugin.deleteTimer(timerId, timedObjectId);
}
catch (SQLException e)
{
log.warn("Unable to delete timer", e);
}
}
Removes the timer from persistent storage. |
public ObjectName getDataSource() {
return dataSource;
}
|
public String getDatabasePersistencePlugin() {
return dbpPluginClassName;
}
|
public String getTimersTable() {
return timersTable;
}
|
public void insertTimer(String timerId,
TimedObjectId timedObjectId,
Date firstEvent,
long intervalDuration,
Serializable info) {
try
{
dbpPlugin.insertTimer(timerId, timedObjectId, firstEvent, intervalDuration, info);
}
catch (SQLException e)
{
RuntimeException ex = new IllegalStateException("Unable to persist timer");
ex.initCause(e);
throw ex;
}
}
Creates the timer in persistent storage. |
public List listTimerHandles() {
List list = new ArrayList();
try
{
list.addAll(dbpPlugin.selectTimers(null));
}
catch (SQLException e)
{
log.warn("Unable to get timer handles", e);
}
return list;
}
Return a List of TimerHandle objects. |
public List listTimerHandles(ObjectName containerId,
ClassLoader loader) {
List list = new ArrayList();
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
try
{
if (loader != null)
{
Thread.currentThread().setContextClassLoader(loader);
}
list.addAll(dbpPlugin.selectTimers(containerId));
}
catch (SQLException e)
{
log.warn("Unable to get timer handles for containerId: " + containerId, e);
}
finally
{
// restore class loader
Thread.currentThread().setContextClassLoader(oldCl);
}
return list;
}
List the persisted timer handles for a particular container |
public void resetAndRestoreTimers() throws SQLException {
timersToRestore = dbpPlugin.selectTimers(null);
log.debug("Found " + timersToRestore.size() + " timer(s)");
if (timersToRestore.size() > 0)
{
// delete all timers
clearTimers();
}
restoreTimers();
}
Re-read the current persistent timers list, clear the db of timers,
and restore the timers. |
public void restoreTimers() {
if (timersToRestore != null && timersToRestore.size() > 0)
{
log.debug("Restoring " + timersToRestore.size() + " timer(s)");
// recreate the timers
for (int i = 0; i < timersToRestore.size(); i++)
{
TimerHandleImpl handle = (TimerHandleImpl)timersToRestore.get(i);
try
{
TimedObjectId targetId = handle.getTimedObjectId();
ObjectName containerName = targetId.getContainerId();
ContainerMBean container = (ContainerMBean)MBeanProxyExt.create(ContainerMBean.class, containerName, server);
TimerService timerService = container.getTimerService(targetId.getInstancePk());
timerService.createTimer(handle.getFirstTime(), handle.getPeriode(), handle.getInfo());
}
catch (Exception e)
{
log.warn("Unable to restore timer record: " + handle);
}
}
timersToRestore.clear();
}
}
Restore the persistent timers seen during service startup |
public void setDataSource(ObjectName dataSource) {
this.dataSource = dataSource;
}
|
public void setDatabasePersistencePlugin(String dbpPluginClass) {
this.dbpPluginClassName = dbpPluginClass;
}
|
public void setTimersTable(String timersTable) {
this.timersTable = timersTable;
}
|
public void startService() throws Exception {
// Get the persistence plugin
if (dbpPluginClassName != null)
{
Class dbpPolicyClass = Thread.currentThread().getContextClassLoader().loadClass(dbpPluginClassName);
dbpPlugin = (DatabasePersistencePlugin)dbpPolicyClass.newInstance();
}
else
{
dbpPlugin = new GeneralPurposeDatabasePersistencePlugin();
}
// init the plugin
if (dbpPlugin instanceof DatabasePersistencePluginExt)
{
// if using the extended plugin interface, initialize the timers table name
((DatabasePersistencePluginExt)dbpPlugin).init(server, dataSource, timersTable);
}
else
{
dbpPlugin.init(server, dataSource);
}
// warn if timers table cannot be set
if (dbpPlugin.getTableName().equals(timersTable) == false)
{
log.warn("Database persistence plugin '" + dbpPluginClassName +
"' uses hardcoded timers table name: '" + dbpPlugin.getTableName());
}
// create the table if needed
dbpPlugin.createTableIfNotExists();
}
Initializes this service. |