1 22 package org.jboss.ejb.txtimer; 23 24 26 import java.io.Serializable ; 27 import java.sql.SQLException ; 28 import java.util.ArrayList ; 29 import java.util.Date ; 30 import java.util.List ; 31 32 import javax.ejb.TimerService ; 33 import javax.management.ObjectName ; 34 import javax.transaction.SystemException ; 35 import javax.transaction.Transaction ; 36 import javax.transaction.TransactionManager ; 37 38 import org.jboss.ejb.ContainerMBean; 39 import org.jboss.logging.Logger; 40 import org.jboss.mx.util.MBeanProxyExt; 41 import org.jboss.system.ServiceMBeanSupport; 42 import org.jboss.tm.TransactionManagerLocator; 43 44 54 public class DatabasePersistencePolicy extends ServiceMBeanSupport 55 implements DatabasePersistencePolicyMBean 56 { 57 private static Logger log = Logger.getLogger(DatabasePersistencePolicy.class); 59 60 private DatabasePersistencePlugin dbpPlugin; 62 63 private ObjectName dataSource; 65 private String dbpPluginClassName; 66 67 private TransactionManager tm; 69 70 private List timersToRestore; 72 73 76 public void startService() throws Exception 77 { 78 tm = TransactionManagerLocator.getInstance().locate(); 79 80 if (dbpPluginClassName != null) 82 { 83 Class dbpPolicyClass = Thread.currentThread().getContextClassLoader().loadClass(dbpPluginClassName); 84 dbpPlugin = (DatabasePersistencePlugin)dbpPolicyClass.newInstance(); 85 } 86 else 87 { 88 dbpPlugin = new GeneralPurposeDatabasePersistencePlugin(); 89 } 90 91 dbpPlugin.init(server, dataSource); 93 94 dbpPlugin.createTableIfNotExists(); 96 } 97 98 107 public void insertTimer(String timerId, TimedObjectId timedObjectId, Date firstEvent, long intervalDuration, Serializable info) 108 { 109 try 110 { 111 dbpPlugin.insertTimer(timerId, timedObjectId, firstEvent, intervalDuration, info); 112 } 113 catch (SQLException e) 114 { 115 RuntimeException ex = new IllegalStateException ("Unable to persist timer"); 116 ex.initCause(e); 117 throw ex; 118 } 119 } 120 121 126 public void deleteTimer(String timerId, TimedObjectId timedObjectId) 127 { 128 Transaction threadTx = suspendTransaction(); 130 131 try 132 { 133 dbpPlugin.deleteTimer(timerId, timedObjectId); 134 } 135 catch (SQLException e) 136 { 137 log.warn("Unable to delete timer", e); 138 } 139 finally 140 { 141 resumeTransaction(threadTx); 143 } 144 } 145 146 153 public List listTimerHandles(ObjectName containerId, ClassLoader loader) 154 { 155 List list = new ArrayList (); 156 157 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 158 try 159 { 160 if (loader != null) 161 { 162 Thread.currentThread().setContextClassLoader(loader); 163 } 164 list.addAll(dbpPlugin.selectTimers(containerId)); 165 } 166 catch (SQLException e) 167 { 168 log.warn("Unable to get timer handles for containerId: " + containerId, e); 169 } 170 finally 171 { 172 Thread.currentThread().setContextClassLoader(oldCl); 174 } 175 return list; 176 } 177 178 181 public List listTimerHandles() 182 { 183 List list = new ArrayList (); 184 try 185 { 186 list.addAll(dbpPlugin.selectTimers(null)); 187 } 188 catch (SQLException e) 189 { 190 log.warn("Unable to get timer handles", e); 191 } 192 return list; 193 } 194 195 198 public void restoreTimers() 199 { 200 if (timersToRestore != null && timersToRestore.size() > 0) 201 { 202 log.debug("Restoring " + timersToRestore.size() + " timer(s)"); 203 204 for (int i = 0; i < timersToRestore.size(); i++) 206 { 207 TimerHandleImpl handle = (TimerHandleImpl)timersToRestore.get(i); 208 209 try 210 { 211 TimedObjectId targetId = handle.getTimedObjectId(); 212 ObjectName containerName = targetId.getContainerId(); 213 ContainerMBean container = (ContainerMBean)MBeanProxyExt.create(ContainerMBean.class, containerName, server); 214 TimerService timerService = container.getTimerService(targetId.getInstancePk()); 215 timerService.createTimer(handle.getFirstTime(), handle.getPeriode(), handle.getInfo()); 216 } 217 catch (Exception e) 218 { 219 log.warn("Unable to restore timer record: " + handle); 220 } 221 } 222 timersToRestore.clear(); 223 } 224 } 225 226 229 public void clearTimers() 230 { 231 try 232 { 233 dbpPlugin.clearTimers(); 234 } 235 catch (SQLException e) 236 { 237 log.warn("Unable to clear timers", e); 238 } 239 } 240 241 246 public void resetAndRestoreTimers() throws SQLException 247 { 248 timersToRestore = dbpPlugin.selectTimers(null); 249 log.debug("Found " + timersToRestore.size() + " timer(s)"); 250 if (timersToRestore.size() > 0) 251 { 252 clearTimers(); 254 } 255 restoreTimers(); 256 } 257 258 260 263 public ObjectName getDataSource() 264 { 265 return dataSource; 266 } 267 268 271 public void setDataSource(ObjectName dataSource) 272 { 273 this.dataSource = dataSource; 274 } 275 276 279 public String getDatabasePersistencePlugin() 280 { 281 return dbpPluginClassName; 282 } 283 284 287 public void setDatabasePersistencePlugin(String dbpPluginClass) 288 { 289 this.dbpPluginClassName = dbpPluginClass; 290 } 291 293 private Transaction suspendTransaction() 294 { 295 Transaction threadTx = null; 296 try 297 { 298 threadTx = tm.suspend(); 299 } 300 catch (SystemException e) 301 { 302 log.warn("Cannot suspend Tx: " + e.toString()); 303 } 304 return threadTx; 305 } 306 307 private void resumeTransaction(Transaction threadTx) 308 { 309 try 310 { 311 if (threadTx != null) 312 tm.resume(threadTx); 313 } 314 catch (Exception e) 315 { 316 log.warn("Cannot resume Tx: " + e.toString()); 317 } 318 } 319 } 320 321 | Popular Tags |