View Javadoc

1   /*
2    * $Id: QuartzMessageDispatcher.java 11678 2008-05-02 12:03:07Z 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.transport.quartz;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.endpoint.OutboundEndpoint;
16  import org.mule.config.i18n.CoreMessages;
17  import org.mule.transport.AbstractMessageDispatcher;
18  import org.mule.transport.quartz.config.JobConfig;
19  import org.mule.transport.quartz.i18n.QuartzMessages;
20  import org.mule.transport.quartz.jobs.CustomJob;
21  import org.mule.transport.quartz.jobs.CustomJobConfig;
22  import org.mule.transport.quartz.jobs.CustomJobFromMessageConfig;
23  
24  import java.util.Date;
25  import java.util.Iterator;
26  
27  import org.quartz.CronTrigger;
28  import org.quartz.Job;
29  import org.quartz.JobDataMap;
30  import org.quartz.JobDetail;
31  import org.quartz.Scheduler;
32  import org.quartz.SimpleTrigger;
33  import org.quartz.Trigger;
34  
35  /**
36   * Can schedule a Job with the Quartz scheduler. The event must contain the Job to
37   * invoke or have it set as a property. Time triggger properties can be set on the
38   * event to control how and when the event is fired.
39   */
40  public class QuartzMessageDispatcher extends AbstractMessageDispatcher
41  {
42  
43      public QuartzMessageDispatcher(OutboundEndpoint endpoint)
44      {
45          super(endpoint);
46      }
47  
48      protected void doDispose()
49      {
50          // template method
51      }
52  
53      protected void doDispatch(MuleEvent event) throws Exception
54      {
55          JobConfig jobConfig = (JobConfig)endpoint.getProperty(QuartzConnector.PROPERTY_JOB_CONFIG);
56          if(jobConfig==null)
57          {
58              throw new IllegalArgumentException(CoreMessages.objectIsNull(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage());
59          }
60  
61          JobDetail jobDetail = new JobDetail();
62          // make the job name unique per endpoint (MULE-753)
63          jobDetail.setName(event.getEndpoint().getEndpointURI().getAddress() + "-" + event.getId());
64  
65          JobDataMap jobDataMap = new JobDataMap();
66          MuleMessage msg = event.getMessage();
67          for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();)
68          {
69              String propertyKey = (String)iterator.next();
70              jobDataMap.put(propertyKey, msg.getProperty(propertyKey));
71          }
72          jobDetail.setJobDataMap(jobDataMap);
73  
74          Job job = null;
75          // work out what we're actually calling
76          Object payload = event.transformMessage();
77  
78          if(jobConfig instanceof CustomJobConfig)
79          {
80              job = ((CustomJobConfig)jobConfig).getJob();
81          }
82          else if(jobConfig instanceof CustomJobFromMessageConfig)
83          {
84              job = ((CustomJobFromMessageConfig)jobConfig).getJob(msg);
85              //rewrite the jobConfig to the real Jobconfig on the message
86              jobConfig = ((CustomJobFromMessageConfig)jobConfig).getJobConfig(msg);
87          }
88  
89          jobDataMap.put(QuartzConnector.PROPERTY_JOB_CONFIG, jobConfig);        
90          jobDetail.setJobClass(jobConfig.getJobClass());
91          // If there has been a job created or found then we default to a customJob configuration
92          if (job!=null )
93          {
94              jobDataMap.put(QuartzConnector.PROPERTY_JOB_OBJECT, job);
95              jobDetail.setJobClass(CustomJob.class);
96          }
97  
98         
99          // The payload will be ignored by the CustomJob - don't know why
100         // we need it here
101         //RM: The custom job may want the message and the Job type may not be delegating job
102         jobDataMap.put(QuartzConnector.PROPERTY_PAYLOAD, payload);
103 
104         Trigger trigger;
105         String cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION);
106         String repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL);
107         String repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT);
108         String startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY);
109         String groupName = jobConfig.getGroupName();
110         String jobGroupName = jobConfig.getJobGroupName();
111 
112         if (groupName == null)
113         {
114             groupName = QuartzConnector.DEFAULT_GROUP_NAME;
115         }
116         if (jobGroupName == null)
117         {
118             jobGroupName = groupName;
119         }
120 
121         jobDetail.setGroup(groupName);
122 
123         if (cronExpression != null)
124         {
125             CronTrigger ctrigger = new CronTrigger();
126             ctrigger.setCronExpression(cronExpression);
127             trigger = ctrigger;
128         }
129         else if (repeatInterval != null)
130         {
131             SimpleTrigger strigger = new SimpleTrigger();
132             strigger.setRepeatInterval(Long.parseLong(repeatInterval));
133             if (repeatCount != null)
134             {
135                 strigger.setRepeatCount(Integer.parseInt(repeatCount));
136             }
137             else
138             {
139                 strigger.setRepeatCount(-1);
140             }
141             trigger = strigger;
142         }
143         else
144         {
145             throw new IllegalArgumentException(
146                 QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage());
147         }
148         long start = System.currentTimeMillis();
149         if (startDelay != null)
150         {
151             start += Long.parseLong(startDelay);
152         }
153         trigger.setStartTime(new Date(start));
154         trigger.setName(event.getEndpoint().getEndpointURI().toString() + "-" + event.getId());
155         trigger.setGroup(groupName);
156         trigger.setJobName(jobDetail.getName());
157         trigger.setJobGroup(jobGroupName);
158 
159         Scheduler scheduler = ((QuartzConnector)this.getConnector()).getQuartzScheduler();
160         scheduler.scheduleJob(jobDetail, trigger);
161        // scheduler.start();
162     }
163 
164     protected MuleMessage doSend(MuleEvent event) throws Exception
165     {
166         doDispatch(event);
167         return null;
168     }
169 
170     protected void doConnect() throws Exception
171     {
172         // template method
173     }
174 
175     protected void doDisconnect() throws Exception
176     {
177         // template method
178     }
179 
180 }