View Javadoc

1   /*
2    * $Id: QuartzConnector.java 19191 2010-08-25 21:05:23Z tcarlson $
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  
47      public static final String DEFAULT_GROUP_NAME = "mule";
48  
49      /**
50       * Properties to be used for creating the scheduler.  If no properties are given, the
51       * scheduler will be created by <code>StdSchedulerFactory.getDefaultScheduler()</code>
52       */
53      private Properties factoryProperties = null;
54  
55      /**
56       * The scheduler instance.  This can be configured by the user and injected as a bean
57       * or if not, it will be created by Mule upon initialization.
58       */
59      private Scheduler quartzScheduler = null;
60  
61      public QuartzConnector(MuleContext context)
62      {
63          super(context);
64      }
65      
66      @Override
67      protected void doInitialise() throws InitialisationException
68      {
69          if (factoryProperties == null)
70          {
71              factoryProperties = new Properties();
72          }
73  
74          //Set the thread count, we can't seem to plug in our work manager unfortunately
75          factoryProperties.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
76          factoryProperties.setProperty("org.quartz.threadPool.threadCount", String.valueOf(getReceiverThreadingProfile().getMaxThreadsActive()));
77  
78          try
79          {
80              if (quartzScheduler == null)
81              {
82                  SchedulerFactory factory = new StdSchedulerFactory(factoryProperties);
83                  quartzScheduler = factory.getScheduler();
84              }
85              quartzScheduler.getContext().put(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext);            
86          
87          }
88          catch (Exception e)
89          {
90              throw new InitialisationException(CoreMessages.initialisationFailure("Quartz conntector"), e, this);
91          }
92      }
93  
94      @Override
95      protected void doDispose()
96      {
97          // template method
98      }
99  
100     @Override
101     protected void doConnect() throws Exception
102     {
103         // template method
104     }
105 
106     @Override
107     protected void doDisconnect() throws Exception
108     {
109         // template method
110     }
111 
112     @Override
113     protected void doStart() throws MuleException
114     {
115         try
116         {
117             quartzScheduler.start();
118         }
119         catch (Exception e)
120         {
121             throw new ConnectorException(CoreMessages.failedToStart("Quartz provider"), this, e);
122         }
123     }
124 
125     @Override
126     protected void doStop() throws MuleException
127     {
128         try
129         {
130             if (quartzScheduler != null)
131             {
132                 quartzScheduler.shutdown();
133             }
134         }
135         catch (Exception e)
136         {
137             throw new ConnectorException(CoreMessages.failedToStop("Quartz provider"), this, e);
138         }
139     }
140 
141     public String getProtocol()
142     {
143         return QUARTZ;
144     }
145 
146     public Scheduler getQuartzScheduler()
147     {
148         return quartzScheduler;
149     }
150 
151     public void setQuartzScheduler(Scheduler scheduler)
152     {
153         this.quartzScheduler = scheduler;
154     }
155 
156     public Properties getFactoryProperties()
157     {
158         return factoryProperties;
159     }
160 
161     public void setFactoryProperties(Properties factoryProperties)
162     {
163         this.factoryProperties = factoryProperties;
164     }
165 }