View Javadoc

1   /*
2    * $Id: QuartzMessageReceiver.java 7963 2007-08-21 08:53:15Z 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.AbstractMessageReceiver;
15  import org.mule.providers.quartz.i18n.QuartzMessages;
16  import org.mule.providers.quartz.jobs.MuleReceiverJob;
17  import org.mule.umo.UMOComponent;
18  import org.mule.umo.UMOException;
19  import org.mule.umo.endpoint.EndpointException;
20  import org.mule.umo.endpoint.UMOEndpoint;
21  import org.mule.umo.lifecycle.InitialisationException;
22  import org.mule.umo.provider.UMOConnector;
23  
24  import java.util.Date;
25  
26  import org.quartz.CronTrigger;
27  import org.quartz.JobDataMap;
28  import org.quartz.JobDetail;
29  import org.quartz.ObjectAlreadyExistsException;
30  import org.quartz.Scheduler;
31  import org.quartz.SimpleTrigger;
32  import org.quartz.Trigger;
33  
34  /**
35   * Listens for Quartz sheduled events using the Receiver Job and fires events to the
36   * component associated with this receiver.
37   */
38  public class QuartzMessageReceiver extends AbstractMessageReceiver
39  {
40  
41      public static final String QUARTZ_RECEIVER_PROPERTY = "mule.quartz.receiver";
42      public static final String QUARTZ_CONNECTOR_PROPERTY = "mule.quartz.connector";
43  
44      private final QuartzConnector connector;
45  
46      public QuartzMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint)
47          throws InitialisationException
48      {
49          super(connector, component, endpoint);
50          this.connector = (QuartzConnector)connector;
51      }
52  
53      protected void doDispose()
54      {
55          // template method
56      }
57  
58      protected void doStart() throws UMOException
59      {
60          try
61          {
62              Scheduler scheduler = connector.getQuartzScheduler();
63  
64              JobDetail jobDetail = new JobDetail();
65              jobDetail.setName(endpoint.getEndpointURI().toString());
66              jobDetail.setJobClass(MuleReceiverJob.class);
67              JobDataMap jobDataMap = new JobDataMap();
68              jobDataMap.put(QUARTZ_RECEIVER_PROPERTY, this.getReceiverKey());
69              jobDataMap.put(QUARTZ_CONNECTOR_PROPERTY, this.connector.getName());
70              jobDataMap.putAll(endpoint.getProperties());
71              jobDetail.setJobDataMap(jobDataMap);
72  
73              Trigger trigger = null;
74              String cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION);
75              String repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL);
76              String repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT);
77              String startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY);
78              String groupName = jobDataMap.getString(QuartzConnector.PROPERTY_GROUP_NAME);
79              String jobGroupName = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_GROUP_NAME);
80  
81              if (groupName == null)
82              {
83                  groupName = QuartzConnector.DEFAULT_GROUP_NAME;
84              }
85              if (jobGroupName == null)
86              {
87                  jobGroupName = groupName;
88              }
89  
90              jobDetail.setGroup(groupName);
91  
92              if (cronExpression != null)
93              {
94                  CronTrigger ctrigger = new CronTrigger();
95                  ctrigger.setCronExpression(cronExpression);
96                  trigger = ctrigger;
97              }
98              else if (repeatInterval != null)
99              {
100                 SimpleTrigger strigger = new SimpleTrigger();
101                 strigger.setRepeatInterval(Long.parseLong(repeatInterval));
102                 if (repeatCount != null)
103                 {
104                     strigger.setRepeatCount(Integer.parseInt(repeatCount));
105                 }
106                 else
107                 {
108                     strigger.setRepeatCount(-1);
109                 }
110                 trigger = strigger;
111             }
112             else
113             {
114                 throw new IllegalArgumentException(
115                     QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage());
116             }
117             long start = System.currentTimeMillis();
118             if (startDelay != null)
119             {
120                 start += Long.parseLong(startDelay);
121             }
122             trigger.setStartTime(new Date(start));
123             trigger.setName(endpoint.getEndpointURI().toString());
124             trigger.setGroup(groupName);
125             trigger.setJobName(endpoint.getEndpointURI().toString());
126             trigger.setJobGroup(jobGroupName);
127 
128             // We need to handle cases when the job has already been
129             // persisted
130             try
131             {
132                 scheduler.scheduleJob(jobDetail, trigger);
133             }
134             catch (ObjectAlreadyExistsException oaee)
135             {
136                 // Do anything here?
137             }
138 
139             scheduler.start();
140         }
141         catch (Exception e)
142         {
143             throw new EndpointException(CoreMessages.failedToStart("Quartz receiver"), e);
144         }
145     }
146 
147     protected void doStop() throws UMOException
148     {
149         // nothing to do
150     }
151 
152     protected void doConnect() throws Exception
153     {
154         // nothing to do
155     }
156 
157     protected void doDisconnect() throws Exception
158     {
159         // nothing to do
160     }
161 
162 }