FileDocCategorySizeDatePackage
QuartzTimerServiceFactory.javaAPI DocJBoss 4.2.18593Fri Jul 13 20:53:54 BST 2007org.jboss.ejb3.timerservice.quartz

QuartzTimerServiceFactory

public class QuartzTimerServiceFactory extends org.jboss.ejb3.timerservice.TimerServiceFactory
Creates timer service objects for use in EJB3 containers. For this two methods are provided: createTimerService and removeTimerService. The factory can be started and stopped within both an embedded and full stack. For now only one scheduler is supported. Each bean container has its own job and trigger group within Quartz.
author
Carlo de Wolf
version
$Revision: 56116 $

Fields Summary
private static final Logger
log
private TransactionManager
tm
private static org.quartz.Scheduler
scheduler
private Properties
properties
private Properties
sqlProperties
Contains the sql statements to create the database schema.
Constructors Summary
Methods Summary
private voidcreateSchema()

   
     
   
      try
      {
         tm.begin();
         try
         {
            Connection conn = getConnection();
            try
            {
               boolean success = execute(conn, "CREATE_TABLE_JOB_DETAILS");
               if(success)
               {
                  execute(conn, "CREATE_TABLE_JOB_LISTENERS");
                  execute(conn, "CREATE_TABLE_TRIGGERS");
                  execute(conn, "CREATE_TABLE_SIMPLE_TRIGGERS");
                  execute(conn, "CREATE_TABLE_CRON_TRIGGERS");
                  execute(conn, "CREATE_TABLE_BLOB_TRIGGERS");
                  execute(conn, "CREATE_TABLE_TRIGGER_LISTENERS");
                  execute(conn, "CREATE_TABLE_CALENDARS");
                  execute(conn, "CREATE_TABLE_PAUSED_TRIGGER_GRPS");
                  execute(conn, "CREATE_TABLE_FIRED_TRIGGERS");
                  execute(conn, "CREATE_TABLE_SCHEDULER_STATE");
                  execute(conn, "CREATE_TABLE_LOCKS");
                  
                  execute(conn, "INSERT_TRIGGER_ACCESS");
                  execute(conn, "INSERT_JOB_ACCESS");
                  execute(conn, "INSERT_CALENDAR_ACCESS");
                  execute(conn, "INSERT_STATE_ACCESS");
                  execute(conn, "INSERT_MISFIRE_ACCESS");
               }
            }
            finally
            {
               conn.close();
            }
            tm.commit();
         }
         catch(SQLException e)
         {
            throw new RuntimeException(e);
         }
         catch (RollbackException e)
         {
            throw new RuntimeException(e);
         }
         catch (HeuristicMixedException e)
         {
            throw new RuntimeException(e);
         }
         catch (HeuristicRollbackException e)
         {
            throw new RuntimeException(e);
         }
         finally
         {
            if(tm.getStatus() == Status.STATUS_ACTIVE)
               tm.rollback();
         }
      }
      catch(SystemException e)
      {
         throw new RuntimeException(e);
      }
      catch (NotSupportedException e)
      {
         throw new RuntimeException(e);
      }
   
public javax.ejb.TimerServicecreateTimerService(javax.management.ObjectName objectName, org.jboss.ejb3.timerservice.TimedObjectInvoker invoker)
Create a TimerService for use in a bean container.

param
objectName the name of the bean container
param
invoker the invoker to call on timeouts
return
an EJB TimerService

      Scheduler scheduler = getScheduler();
      if (scheduler == null) return null;
      
      return new TimerServiceImpl(scheduler, objectName, invoker);
   
private booleanexecute(java.sql.Connection conn, java.lang.String stmtName)

      String sql = sqlProperties.getProperty(stmtName);
      if(sql == null)
         throw new IllegalStateException("No sql set for '" + stmtName + "'");
      
      PreparedStatement stmt = conn.prepareStatement(sql);
      try
      {
         stmt.execute();
         return true;
      }
      catch(SQLException e)
      {
         log.warn("sql failed: " + sql);
         if(log.isDebugEnabled())
            log.debug("sql failed: " + sql, e);
         return false;
      }
      finally
      {
         stmt.close();
      }
   
private java.sql.ConnectiongetConnection()

      return DBConnectionManager.getInstance().getConnection("myDS");
   
protected static org.quartz.SchedulergetScheduler()

return
the scheduler for package use

      if(scheduler == null)
      {
         return null;
         //throw new IllegalStateException("TimerServiceFactory hasn't been started yet");
      }
      
      return scheduler;
   
public voidremoveTimerService(javax.ejb.TimerService aTimerService)

      TimerServiceImpl timerService = (TimerServiceImpl) aTimerService;
      timerService.shutdown();
   
public voidrestoreTimerService(javax.ejb.TimerService aTimerService)

      // TODO: implement Quartz restore timer service
   
public voidsetDataSource(java.lang.String jndiName)

      JNDIConnectionProvider connectionProvider = new JNDIConnectionProvider(jndiName, false);
      // FIXME: remove hardcoding
      DBConnectionManager.getInstance().addConnectionProvider("myDS", connectionProvider);
   
public voidsetProperties(java.util.Properties props)

//      if(scheduler != null)
//         throw new IllegalStateException("already started");
      
      // TODO: precondition the prop
      properties = props;
   
public voidsetSqlProperties(java.util.Properties props)

      this.sqlProperties = props;
   
public synchronized voidstart()

      if(scheduler != null)
         throw new IllegalStateException("already started");
      
      log.debug("properties = " + properties);
      
      InitialContext ctx = new InitialContext();
      tm = (TransactionManager) ctx.lookup(TransactionManagerService.JNDI_NAME);
      
      createSchema();
      
      // TODO: bind in JNDI, or is this done by the JMX bean?
      SchedulerFactory factory;
      if(properties == null)
         factory = new StdSchedulerFactory();
      else
         factory = new StdSchedulerFactory(properties);
      scheduler = factory.getScheduler();
      // TODO: really start right away?
      scheduler.start();
   
public synchronized voidstop()

      if(scheduler == null)
         throw new IllegalStateException("already stopped");
      
      // TODO: unbind from JNDI
      
      // TODO: standby or shutdown?
      scheduler.shutdown();
      
      scheduler = null;