View Javadoc

1   /*
2    * $Id: QuartzConnector.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.quartz;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.providers.AbstractConnector;
15  import org.mule.umo.UMOException;
16  import org.mule.umo.lifecycle.InitialisationException;
17  import org.mule.umo.provider.ConnectorException;
18  import org.mule.util.ClassUtils;
19  
20  import java.util.Properties;
21  
22  import org.quartz.Scheduler;
23  import org.quartz.SchedulerFactory;
24  import org.quartz.impl.StdSchedulerFactory;
25  
26  /**
27   * Creates a connection to a Quartz sheduler. This allows events to be sheduled at
28   * specific times, with repeated occurences.
29   */
30  public class QuartzConnector extends AbstractConnector
31  {
32  
33      public static final String PROPERTY_CRON_EXPRESSION = "cronExpression";
34      public static final String PROPERTY_REPEAT_INTERVAL = "repeatInterval";
35      public static final String PROPERTY_REPEAT_COUNT = "repeatCount";
36      public static final String PROPERTY_START_DELAY = "startDelay";
37      public static final String PROPERTY_PAYLOAD = "payload";
38      public static final String PROPERTY_JOB_DISPATCH_ENDPOINT = "jobDispatchEndpoint";
39      public static final String PROPERTY_JOB_RECEIVE_ENDPOINT = "jobReceiveEndpoint";
40      public static final String PROPERTY_JOB_RECEIVE_TIMEOUT = "jobReceiveTimeout";
41  
42      /** deprecated: use PROPERTY_PAYLOAD_REFERENCE */
43      public static final String PROPERTY_PAYLOAD_CLASS_NAME = "payloadClassName";
44  
45      public static final String PROPERTY_PAYLOAD_REFERENCE = "payloadRef";
46      public static final String PROPERTY_GROUP_NAME = "groupName";
47      public static final String PROPERTY_JOB_GROUP_NAME = "jobGroupName";
48      public static final String PROPERTY_JOB_REF = "jobRef";
49      public static final String PROPERTY_JOB_CLASS = "jobClass";
50      public static final String PROPERTY_JOB_OBJECT = "jobObject";
51  
52      public static final String DEFAULT_GROUP_NAME = "mule";
53  
54      private String factoryClassName = StdSchedulerFactory.class.getName();
55  
56      private SchedulerFactory factory;
57  
58      private Properties factoryProperties;
59  
60      private Scheduler quartzScheduler;
61  
62      protected void doInitialise() throws InitialisationException
63      {
64          try
65          {
66              if (quartzScheduler == null)
67              {
68                  if (factory == null)
69                  {
70                      Object[] args = null;
71                      if (factoryProperties != null)
72                      {
73                          args = new Object[]{factoryProperties};
74                      }
75                      factory = (SchedulerFactory)ClassUtils.instanciateClass(factoryClassName, args);
76                  }
77                  quartzScheduler = factory.getScheduler();
78              }
79          }
80          catch (Exception e)
81          {
82              throw new InitialisationException(CoreMessages.initialisationFailure("Quartz provider"), e);
83          }
84      }
85  
86      protected void doDispose()
87      {
88          // template method
89      }
90  
91      protected void doConnect() throws Exception
92      {
93          // template method
94      }
95  
96      protected void doDisconnect() throws Exception
97      {
98          // template method
99      }
100 
101     protected void doStart() throws UMOException
102     {
103         try
104         {
105             quartzScheduler.start();
106         }
107         catch (Exception e)
108         {
109             throw new ConnectorException(CoreMessages.failedToStart("Quartz provider"), this, e);
110         }
111     }
112 
113     protected void doStop() throws UMOException
114     {
115         try
116         {
117             if (quartzScheduler != null)
118             {
119                 quartzScheduler.shutdown();
120             }
121         }
122         catch (Exception e)
123         {
124             throw new ConnectorException(CoreMessages.failedToStop("Quartz provider"), this, e);
125         }
126     }
127 
128     public String getProtocol()
129     {
130         return "quartz";
131     }
132 
133     public SchedulerFactory getFactory()
134     {
135         return factory;
136     }
137 
138     public void setFactory(SchedulerFactory factory)
139     {
140         this.factory = factory;
141     }
142 
143     public Scheduler getQuartzScheduler()
144     {
145         return quartzScheduler;
146     }
147 
148     public void setQuartzScheduler(Scheduler scheduler)
149     {
150         this.quartzScheduler = scheduler;
151     }
152 
153     public String getFactoryClassName()
154     {
155         return factoryClassName;
156     }
157 
158     public void setFactoryClassName(String factoryClassName)
159     {
160         this.factoryClassName = factoryClassName;
161     }
162 
163     public Properties getFactoryProperties()
164     {
165         return factoryProperties;
166     }
167 
168     public void setFactoryProperties(Properties factoryProperties)
169     {
170         this.factoryProperties = factoryProperties;
171     }
172 
173 }