View Javadoc

1   /*
2    * $Id: QuartzMessageReceiver.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.MuleException;
14  import org.mule.api.construct.FlowConstruct;
15  import org.mule.api.endpoint.EndpointException;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.api.lifecycle.CreateException;
18  import org.mule.api.transport.Connector;
19  import org.mule.config.i18n.CoreMessages;
20  import org.mule.transport.AbstractMessageReceiver;
21  import org.mule.transport.quartz.config.JobConfig;
22  import org.mule.transport.quartz.i18n.QuartzMessages;
23  import org.mule.transport.quartz.jobs.CustomJobConfig;
24  import org.mule.transport.quartz.jobs.EventGeneratorJobConfig;
25  
26  import java.util.Date;
27  
28  import org.quartz.CronTrigger;
29  import org.quartz.Job;
30  import org.quartz.JobDataMap;
31  import org.quartz.JobDetail;
32  import org.quartz.ObjectAlreadyExistsException;
33  import org.quartz.Scheduler;
34  import org.quartz.SimpleTrigger;
35  import org.quartz.Trigger;
36  
37  /**
38   * Listens for Quartz sheduled events using the Receiver Job and fires events to the
39   * service associated with this receiver.
40   */
41  public class QuartzMessageReceiver extends AbstractMessageReceiver
42  {
43  
44      public static final String QUARTZ_RECEIVER_PROPERTY = "mule.quartz.receiver";
45      public static final String QUARTZ_CONNECTOR_PROPERTY = "mule.quartz.connector";
46  
47      private final QuartzConnector connector;
48  
49      public QuartzMessageReceiver(Connector connector, FlowConstruct flowConstruct, InboundEndpoint endpoint)
50              throws CreateException
51      {
52          super(connector, flowConstruct, endpoint);
53          this.connector = (QuartzConnector) connector;
54      }
55  
56      @Override
57      protected void doDispose()
58      {
59          // template method
60      }
61  
62      @Override
63      protected void doStart() throws MuleException
64      {
65          try
66          {
67              Scheduler scheduler = connector.getQuartzScheduler();
68  
69              JobConfig jobConfig = (JobConfig) endpoint.getProperty(QuartzConnector.PROPERTY_JOB_CONFIG);
70              if (jobConfig == null)
71              {
72                  throw new IllegalArgumentException(CoreMessages.objectIsNull(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage());
73              }
74              
75              JobDetail jobDetail = new JobDetail();
76              jobDetail.setName(endpoint.getEndpointURI().getAddress());
77              jobDetail.setJobClass(jobConfig.getJobClass());
78              JobDataMap jobDataMap = new JobDataMap();
79              jobDataMap.put(QUARTZ_RECEIVER_PROPERTY, this.getReceiverKey());
80              jobDataMap.put(QUARTZ_CONNECTOR_PROPERTY, this.connector.getName());
81              jobDataMap.putAll(endpoint.getProperties());
82  
83              if (jobConfig instanceof EventGeneratorJobConfig)
84              {
85                  jobDataMap.put(QuartzConnector.PROPERTY_PAYLOAD, ((EventGeneratorJobConfig) jobConfig).getPayload());
86              }
87              jobDataMap.put(QuartzConnector.PROPERTY_JOB_CONFIG, jobConfig);
88              
89              Job job = null;
90              if (jobConfig instanceof CustomJobConfig)
91              {
92                  job = ((CustomJobConfig) jobConfig).getJob();
93              }
94              // If there has been a job created or found then we default to a custom Job configuration
95              if (job != null)
96              {
97                  jobDataMap.put(QuartzConnector.PROPERTY_JOB_OBJECT, job);
98                  jobDetail.setJobClass(jobConfig.getJobClass());
99              }
100 
101             jobDetail.setJobDataMap(jobDataMap);
102             
103             Trigger trigger;
104             String cronExpression = (String)endpoint.getProperty(QuartzConnector.PROPERTY_CRON_EXPRESSION);
105             String repeatInterval = (String)endpoint.getProperty(QuartzConnector.PROPERTY_REPEAT_INTERVAL);
106             String repeatCount = (String)endpoint.getProperty(QuartzConnector.PROPERTY_REPEAT_COUNT);
107             String startDelay = (String)endpoint.getProperty(QuartzConnector.PROPERTY_START_DELAY);
108             String groupName = jobConfig.getGroupName();
109             String jobGroupName = jobConfig.getJobGroupName();
110 
111             if (groupName == null)
112             {
113                 groupName = QuartzConnector.DEFAULT_GROUP_NAME;
114             }
115             if (jobGroupName == null)
116             {
117                 jobGroupName = groupName;
118             }
119 
120             jobDetail.setGroup(groupName);
121 
122             if (cronExpression != null)
123             {
124                 CronTrigger ctrigger = new CronTrigger();
125                 ctrigger.setCronExpression(cronExpression);
126                 trigger = ctrigger;
127             }
128             else if (repeatInterval != null)
129             {
130                 SimpleTrigger strigger = new SimpleTrigger();
131                 strigger.setRepeatInterval(Long.parseLong(repeatInterval));
132                 if (repeatCount != null)
133                 {
134                     strigger.setRepeatCount(Integer.parseInt(repeatCount));
135                 }
136                 else
137                 {
138                     strigger.setRepeatCount(-1);
139                 }
140                 trigger = strigger;
141             }
142             else
143             {
144                 throw new IllegalArgumentException(
145                         QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage());
146             }
147 
148             trigger.setName(endpoint.getEndpointURI().getAddress());
149             trigger.setGroup(groupName);
150             trigger.setJobName(endpoint.getEndpointURI().getAddress());
151             trigger.setJobGroup(jobGroupName);
152 
153             // Minimize the the time window capturing the start time and scheduling the job.
154             long start = System.currentTimeMillis();
155             if (startDelay != null)
156             {
157                 start += Long.parseLong(startDelay);
158             }
159             trigger.setStartTime(new Date(start));
160 
161             // We need to handle cases when the job has already been persisted
162             try
163             {
164                 scheduler.scheduleJob(jobDetail, trigger);
165             }
166             catch (ObjectAlreadyExistsException oaee)
167             {
168                 logger.warn("A quartz Job with name: " + endpoint.getEndpointURI().getAddress() +
169                         " has already been registered. Cannot register again");
170             }
171         }
172         catch (Exception e)
173         {
174             throw new EndpointException(CoreMessages.failedToStart("Quartz receiver"), e);
175         }
176     }
177 
178     @Override
179     protected void doStop() throws MuleException
180     {
181         // nothing to do
182     }
183 
184     @Override
185     protected void doConnect() throws Exception
186     {
187         // nothing to do
188     }
189 
190     @Override
191     protected void doDisconnect() throws Exception
192     {
193         // nothing to do
194     }
195 
196 }