View Javadoc

1   /*
2    * $Id: QuartzConnector.java 22586 2011-08-04 17:29:56Z svacas $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport.quartz;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.transport.ConnectorException;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.transport.AbstractConnector;
20  
21  import java.util.Properties;
22  
23  import org.quartz.Scheduler;
24  import org.quartz.SchedulerFactory;
25  import org.quartz.impl.StdSchedulerFactory;
26  
27  /**
28   * Creates a connection to a Quartz scheduler. This allows events to be scheduled at
29   * specific times, with repeated occurrences.
30   */
31  public class QuartzConnector extends AbstractConnector
32  {
33      public static final String QUARTZ = "quartz";
34  
35      public static final String PROPERTY_CRON_EXPRESSION = "cronExpression";
36      public static final String PROPERTY_REPEAT_INTERVAL = "repeatInterval";
37      public static final String PROPERTY_REPEAT_COUNT = "repeatCount";
38      public static final String PROPERTY_START_DELAY = "startDelay";
39      public static final String PROPERTY_PAYLOAD = "payload";
40  
41      public static final String PROPERTY_JOB_CONFIG = "jobConfig";
42      public static final String PROPERTY_JOB_DATA = "jobData";
43  
44      public static final String PROPERTY_JOB_REF = "jobRef";
45      public static final String PROPERTY_JOB_OBJECT = "jobObject";
46      public static final String PROPERTY_JOB_DYNAMIC = "jobDynamic";
47  
48      public static final String DEFAULT_GROUP_NAME = "mule";
49  
50      /**
51       * Properties to be used for creating the scheduler.  If no properties are given, the
52       * scheduler will be created by <code>StdSchedulerFactory.getDefaultScheduler()</code>
53       */
54      private Properties factoryProperties = null;
55  
56      /**
57       * The scheduler instance.  This can be configured by the user and injected as a bean
58       * or if not, it will be created by Mule upon initialization.
59       */
60      private Scheduler quartzScheduler = null;
61  
62      public QuartzConnector(MuleContext context)
63      {
64          super(context);
65      }
66      
67      @Override
68      protected void doInitialise() throws InitialisationException
69      {
70          if (factoryProperties == null)
71          {
72              factoryProperties = new Properties();
73          }
74  
75          //Set the thread count, we can't seem to plug in our work manager unfortunately
76          factoryProperties.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
77          factoryProperties.setProperty("org.quartz.threadPool.threadCount", String.valueOf(getReceiverThreadingProfile().getMaxThreadsActive()));
78          String instanceName = factoryProperties.getProperty("org.quartz.scheduler.instanceName");
79          if (instanceName == null)
80          {
81              factoryProperties.setProperty("org.quartz.scheduler.instanceName",
82                  "scheduler-" + muleContext.getConfiguration().getId());
83          }
84  
85          try
86          {
87              if (quartzScheduler == null)
88              {
89                  SchedulerFactory factory = new StdSchedulerFactory(factoryProperties);
90                  quartzScheduler = factory.getScheduler();
91              }
92              quartzScheduler.getContext().put(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext);            
93          
94          }
95          catch (Exception e)
96          {
97              throw new InitialisationException(CoreMessages.initialisationFailure("Quartz conntector"), e, this);
98          }
99      }
100 
101     @Override
102     protected void doDispose()
103     {
104         try
105         {
106             if (quartzScheduler != null)
107             {
108                 quartzScheduler.shutdown();
109             }
110         }
111         catch (Exception e)
112         {
113             logger.warn(CoreMessages.failedToStop("Quartz provider"), e);
114         }
115     }
116 
117     @Override
118     protected void doConnect() throws Exception
119     {
120         // template method
121     }
122 
123     @Override
124     protected void doDisconnect() throws Exception
125     {
126         // template method
127     }
128 
129     @Override
130     protected void doStart() throws MuleException
131     {
132         try
133         {
134             quartzScheduler.start();
135         }
136         catch (Exception e)
137         {
138             throw new ConnectorException(CoreMessages.failedToStart("Quartz provider"), this, e);
139         }
140     }
141 
142     @Override
143     protected void doStop() throws MuleException
144     {
145         try
146         {
147             if (quartzScheduler != null)
148             {
149                 quartzScheduler.standby();
150             }
151         }
152         catch (Exception e)
153         {
154             throw new ConnectorException(CoreMessages.failedToStop("Quartz provider"), this, e);
155         }
156     }
157 
158     public String getProtocol()
159     {
160         return QUARTZ;
161     }
162 
163     public Scheduler getQuartzScheduler()
164     {
165         return quartzScheduler;
166     }
167 
168     public void setQuartzScheduler(Scheduler scheduler)
169     {
170         this.quartzScheduler = scheduler;
171     }
172 
173     public Properties getFactoryProperties()
174     {
175         return factoryProperties;
176     }
177 
178     public void setFactoryProperties(Properties factoryProperties)
179     {
180         this.factoryProperties = factoryProperties;
181     }
182 }