View Javadoc

1   /*
2    * $Id: QuartzConnector.java 11613 2008-04-20 20:30:10Z rossmason $
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.transport.quartz;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.lifecycle.InitialisationException;
15  import org.mule.api.transport.ConnectorException;
16  import org.mule.config.i18n.CoreMessages;
17  import org.mule.transport.AbstractConnector;
18  
19  import java.util.Properties;
20  
21  import org.quartz.Scheduler;
22  import org.quartz.SchedulerFactory;
23  import org.quartz.impl.StdSchedulerFactory;
24  
25  /**
26   * Creates a connection to a Quartz scheduler. This allows events to be scheduled at
27   * specific times, with repeated occurrences.
28   */
29  public class QuartzConnector extends AbstractConnector
30  {
31  
32      public static final String QUARTZ = "quartz";
33  
34      public static final String PROPERTY_CRON_EXPRESSION = "cronExpression";
35      public static final String PROPERTY_REPEAT_INTERVAL = "repeatInterval";
36      public static final String PROPERTY_REPEAT_COUNT = "repeatCount";
37      public static final String PROPERTY_START_DELAY = "startDelay";
38      public static final String PROPERTY_PAYLOAD = "payload";
39  
40      public static final String PROPERTY_JOB_CONFIG = "jobConfig";
41  
42      public static final String PROPERTY_JOB_REF = "jobRef";
43      public static final String PROPERTY_JOB_OBJECT = "jobObject";
44  
45      public static final String DEFAULT_GROUP_NAME = "mule";
46  
47      /**
48       * Properties to be used for creating the scheduler.  If no properties are given, the
49       * scheduler will be created by StdSchedulerFactory.getDefaultScheduler()
50       */
51      private Properties factoryProperties = null;
52  
53      /**
54       * The scheduler instance.  This can be configured by the user and injected as a bean
55       * or if not, it will be created by Mule upon initialization.
56       */
57      private Scheduler quartzScheduler = null;
58  
59      protected void doInitialise() throws InitialisationException
60      {
61          try
62          {
63              if (quartzScheduler == null)
64              {
65                  if (factoryProperties != null)
66                  {
67                      SchedulerFactory factory = new StdSchedulerFactory(factoryProperties);
68                      quartzScheduler = factory.getScheduler();
69                  }
70                  else
71                  {
72                      quartzScheduler = StdSchedulerFactory.getDefaultScheduler();
73                  }
74              }
75          }
76          catch (Exception e)
77          {
78              throw new InitialisationException(CoreMessages.initialisationFailure("Quartz provider"), e, this);
79          }
80      }
81  
82      protected void doDispose()
83      {
84          // template method
85      }
86  
87      protected void doConnect() throws Exception
88      {
89          // template method
90      }
91  
92      protected void doDisconnect() throws Exception
93      {
94          // template method
95      }
96  
97      protected void doStart() throws MuleException
98      {
99          try
100         {
101             quartzScheduler.start();
102         }
103         catch (Exception e)
104         {
105             throw new ConnectorException(CoreMessages.failedToStart("Quartz provider"), this, e);
106         }
107     }
108 
109     protected void doStop() throws MuleException
110     {
111         try
112         {
113             if (quartzScheduler != null)
114             {
115                 quartzScheduler.shutdown();
116             }
117         }
118         catch (Exception e)
119         {
120             throw new ConnectorException(CoreMessages.failedToStop("Quartz provider"), this, e);
121         }
122     }
123 
124     public String getProtocol()
125     {
126         return QUARTZ;
127     }
128 
129     public Scheduler getQuartzScheduler()
130     {
131         return quartzScheduler;
132     }
133 
134     public void setQuartzScheduler(Scheduler scheduler)
135     {
136         this.quartzScheduler = scheduler;
137     }
138 
139     public Properties getFactoryProperties()
140     {
141         return factoryProperties;
142     }
143 
144     public void setFactoryProperties(Properties factoryProperties)
145     {
146         this.factoryProperties = factoryProperties;
147     }
148 
149 }