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