1 22 package org.jboss.ejb3.timerservice.quartz; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.sql.SQLException ; 27 import java.util.Properties ; 28 29 import javax.ejb.TimerService ; 30 import javax.management.ObjectName ; 31 import javax.naming.InitialContext ; 32 import javax.transaction.HeuristicMixedException ; 33 import javax.transaction.HeuristicRollbackException ; 34 import javax.transaction.NotSupportedException ; 35 import javax.transaction.RollbackException ; 36 import javax.transaction.Status ; 37 import javax.transaction.SystemException ; 38 import javax.transaction.TransactionManager ; 39 40 import org.jboss.ejb3.timerservice.TimedObjectInvoker; 41 import org.jboss.ejb3.timerservice.TimerServiceFactory; 42 import org.jboss.logging.Logger; 43 import org.jboss.tm.TransactionManagerService; 44 import org.quartz.Scheduler; 45 import org.quartz.SchedulerFactory; 46 import org.quartz.impl.StdSchedulerFactory; 47 import org.quartz.utils.DBConnectionManager; 48 import org.quartz.utils.JNDIConnectionProvider; 49 50 62 public class QuartzTimerServiceFactory extends TimerServiceFactory 63 { 64 @SuppressWarnings ("unused") 65 private static final Logger log = Logger.getLogger(QuartzTimerServiceFactory.class); 66 67 private TransactionManager tm; 68 69 private static Scheduler scheduler; 70 71 private Properties properties; 72 73 76 private Properties sqlProperties; 77 78 private void createSchema() 79 { 80 try 81 { 82 tm.begin(); 83 try 84 { 85 Connection conn = getConnection(); 86 try 87 { 88 boolean success = execute(conn, "CREATE_TABLE_JOB_DETAILS"); 89 if(success) 90 { 91 execute(conn, "CREATE_TABLE_JOB_LISTENERS"); 92 execute(conn, "CREATE_TABLE_TRIGGERS"); 93 execute(conn, "CREATE_TABLE_SIMPLE_TRIGGERS"); 94 execute(conn, "CREATE_TABLE_CRON_TRIGGERS"); 95 execute(conn, "CREATE_TABLE_BLOB_TRIGGERS"); 96 execute(conn, "CREATE_TABLE_TRIGGER_LISTENERS"); 97 execute(conn, "CREATE_TABLE_CALENDARS"); 98 execute(conn, "CREATE_TABLE_PAUSED_TRIGGER_GRPS"); 99 execute(conn, "CREATE_TABLE_FIRED_TRIGGERS"); 100 execute(conn, "CREATE_TABLE_SCHEDULER_STATE"); 101 execute(conn, "CREATE_TABLE_LOCKS"); 102 103 execute(conn, "INSERT_TRIGGER_ACCESS"); 104 execute(conn, "INSERT_JOB_ACCESS"); 105 execute(conn, "INSERT_CALENDAR_ACCESS"); 106 execute(conn, "INSERT_STATE_ACCESS"); 107 execute(conn, "INSERT_MISFIRE_ACCESS"); 108 } 109 } 110 finally 111 { 112 conn.close(); 113 } 114 tm.commit(); 115 } 116 catch(SQLException e) 117 { 118 throw new RuntimeException (e); 119 } 120 catch (RollbackException e) 121 { 122 throw new RuntimeException (e); 123 } 124 catch (HeuristicMixedException e) 125 { 126 throw new RuntimeException (e); 127 } 128 catch (HeuristicRollbackException e) 129 { 130 throw new RuntimeException (e); 131 } 132 finally 133 { 134 if(tm.getStatus() == Status.STATUS_ACTIVE) 135 tm.rollback(); 136 } 137 } 138 catch(SystemException e) 139 { 140 throw new RuntimeException (e); 141 } 142 catch (NotSupportedException e) 143 { 144 throw new RuntimeException (e); 145 } 146 } 147 148 155 public TimerService createTimerService(ObjectName objectName, TimedObjectInvoker invoker) 156 { 157 Scheduler scheduler = getScheduler(); 158 if (scheduler == null) return null; 159 160 return new TimerServiceImpl(scheduler, objectName, invoker); 161 } 162 163 private boolean execute(Connection conn, String stmtName) throws SQLException 164 { 165 String sql = sqlProperties.getProperty(stmtName); 166 if(sql == null) 167 throw new IllegalStateException ("No sql set for '" + stmtName + "'"); 168 169 PreparedStatement stmt = conn.prepareStatement(sql); 170 try 171 { 172 stmt.execute(); 173 return true; 174 } 175 catch(SQLException e) 176 { 177 log.warn("sql failed: " + sql); 178 if(log.isDebugEnabled()) 179 log.debug("sql failed: " + sql, e); 180 return false; 181 } 182 finally 183 { 184 stmt.close(); 185 } 186 } 187 188 private Connection getConnection() throws SQLException 189 { 190 return DBConnectionManager.getInstance().getConnection("myDS"); 191 } 192 193 196 protected static Scheduler getScheduler() 197 { 198 if(scheduler == null) 199 { 200 return null; 201 } 203 204 return scheduler; 205 } 206 207 public void removeTimerService(TimerService aTimerService) 208 { 209 TimerServiceImpl timerService = (TimerServiceImpl) aTimerService; 210 timerService.shutdown(); 211 } 212 213 public void restoreTimerService(TimerService aTimerService) 214 { 215 } 217 218 public void setDataSource(String jndiName) 219 { 220 JNDIConnectionProvider connectionProvider = new JNDIConnectionProvider(jndiName, false); 221 DBConnectionManager.getInstance().addConnectionProvider("myDS", connectionProvider); 223 } 224 225 public void setProperties(final Properties props) 226 { 227 230 properties = props; 232 } 233 234 public void setSqlProperties(Properties props) 235 { 236 this.sqlProperties = props; 237 } 238 239 public synchronized void start() throws Exception 240 { 241 if(scheduler != null) 242 throw new IllegalStateException ("already started"); 243 244 log.debug("properties = " + properties); 245 246 InitialContext ctx = new InitialContext (); 247 tm = (TransactionManager ) ctx.lookup(TransactionManagerService.JNDI_NAME); 248 249 createSchema(); 250 251 SchedulerFactory factory; 253 if(properties == null) 254 factory = new StdSchedulerFactory(); 255 else 256 factory = new StdSchedulerFactory(properties); 257 scheduler = factory.getScheduler(); 258 scheduler.start(); 260 } 261 262 public synchronized void stop() throws Exception 263 { 264 if(scheduler == null) 265 throw new IllegalStateException ("already stopped"); 266 267 269 scheduler.shutdown(); 271 272 scheduler = null; 273 } 274 } 275 | Popular Tags |