KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > quartz > QuartzMessageDispatcher


1 /*
2  * $Id: QuartzMessageDispatcher.java 3982 2006-11-22 14:28:01Z lajos $
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 MuleSource MPL
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.MuleManager;
14 import org.mule.config.i18n.Message;
15 import org.mule.providers.AbstractMessageDispatcher;
16 import org.mule.providers.quartz.jobs.DelegatingJob;
17 import org.mule.umo.UMOEvent;
18 import org.mule.umo.UMOException;
19 import org.mule.umo.UMOMessage;
20 import org.mule.umo.endpoint.UMOImmutableEndpoint;
21 import org.mule.umo.provider.DispatchException;
22 import org.mule.util.ClassUtils;
23 import org.quartz.CronTrigger;
24 import org.quartz.Job;
25 import org.quartz.JobDataMap;
26 import org.quartz.JobDetail;
27 import org.quartz.Scheduler;
28 import org.quartz.SimpleTrigger;
29 import org.quartz.Trigger;
30
31 import java.util.Date JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35  * Can schedule a Job with the Quartz scheduler. The event must contain the Job to
36  * invoke or have it set as a property. Time triggger properties can be set on the
37  * event to control how and when the event is fired.
38  */

39 public class QuartzMessageDispatcher extends AbstractMessageDispatcher
40 {
41
42     public QuartzMessageDispatcher(UMOImmutableEndpoint endpoint)
43     {
44         super(endpoint);
45     }
46
47     protected void doDispose()
48     {
49         // template method
50
}
51
52     protected void doDispatch(UMOEvent event) throws Exception JavaDoc
53     {
54         JobDetail jobDetail = new JobDetail();
55         // make the job name unique per endpoint (MULE-753)
56
jobDetail.setName(event.getEndpoint().getEndpointURI().toString() + "-" + event.getId());
57
58         JobDataMap jobDataMap = new JobDataMap();
59         UMOMessage msg = event.getMessage();
60         for (Iterator JavaDoc iterator = msg.getPropertyNames().iterator(); iterator.hasNext();)
61         {
62             String JavaDoc propertyKey = (String JavaDoc)iterator.next();
63             jobDataMap.put(propertyKey, msg.getProperty(propertyKey));
64         }
65         jobDetail.setJobDataMap(jobDataMap);
66
67         Job job;
68         // work out what we're actually calling
69
Object JavaDoc payload = event.getTransformedMessage();
70
71         String JavaDoc jobClass = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_CLASS);
72         if (payload instanceof Job)
73         {
74             job = (Job)payload;
75             jobDataMap.put(QuartzConnector.PROPERTY_JOB_OBJECT, job);
76             jobDetail.setJobClass(DelegatingJob.class);
77         }
78         else if (jobClass != null)
79         {
80             jobDetail.setJobClass(ClassUtils.loadClass(jobClass, getClass()));
81         }
82         else
83         {
84             Object JavaDoc tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_OBJECT);
85             if (tempJob == null)
86             {
87                 tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_REF);
88                 if (tempJob == null)
89                 {
90                     throw new DispatchException(new Message("quartz", 2), event.getMessage(),
91                         event.getEndpoint());
92                 }
93                 else
94                 {
95                     tempJob = MuleManager.getInstance().getContainerContext().getComponent(tempJob);
96                     if (!(tempJob instanceof Job))
97                     {
98                         throw new DispatchException(new Message("quartz", 3), event.getMessage(),
99                             event.getEndpoint());
100                     }
101                 }
102             }
103             else if (!(tempJob instanceof Job))
104             {
105                 throw new DispatchException(new Message("quartz", 3), event.getMessage(), event.getEndpoint());
106             }
107             jobDetail.setJobClass(DelegatingJob.class);
108         }
109
110         jobDataMap.put(QuartzConnector.PROPERTY_PAYLOAD, payload);
111
112         Trigger trigger = null;
113         String JavaDoc cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION);
114         String JavaDoc repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL);
115         String JavaDoc repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT);
116         String JavaDoc startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY);
117         String JavaDoc groupName = jobDataMap.getString(QuartzConnector.PROPERTY_GROUP_NAME);
118         String JavaDoc jobGroupName = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_GROUP_NAME);
119
120         if (groupName == null)
121         {
122             groupName = QuartzConnector.DEFAULT_GROUP_NAME;
123         }
124         if (jobGroupName == null)
125         {
126             jobGroupName = groupName;
127         }
128
129         jobDetail.setGroup(groupName);
130
131         if (cronExpression != null)
132         {
133             CronTrigger ctrigger = new CronTrigger();
134             ctrigger.setCronExpression(cronExpression);
135             trigger = ctrigger;
136         }
137         else if (repeatInterval != null)
138         {
139             SimpleTrigger strigger = new SimpleTrigger();
140             strigger.setRepeatInterval(Long.parseLong(repeatInterval));
141             if (repeatCount != null)
142             {
143                 strigger.setRepeatCount(Integer.parseInt(repeatCount));
144             }
145             else
146             {
147                 strigger.setRepeatCount(-1);
148             }
149             trigger = strigger;
150         }
151         else
152         {
153             throw new IllegalArgumentException JavaDoc(new Message("quartz", 1).getMessage());
154         }
155         long start = System.currentTimeMillis();
156         if (startDelay != null)
157         {
158             start += Long.parseLong(startDelay);
159         }
160         trigger.setStartTime(new Date JavaDoc(start));
161         trigger.setName(event.getEndpoint().getEndpointURI().toString());
162         trigger.setGroup(groupName);
163         trigger.setJobName(jobDetail.getName());
164         trigger.setJobGroup(jobGroupName);
165
166         Scheduler scheduler = ((QuartzConnector)this.getConnector()).getScheduler();
167         scheduler.scheduleJob(jobDetail, trigger);
168     }
169
170     protected UMOMessage doSend(UMOEvent event) throws Exception JavaDoc
171     {
172         doDispatch(event);
173         return null;
174     }
175
176     protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception JavaDoc
177     {
178         // template method
179
}
180
181     protected void doDisconnect() throws Exception JavaDoc
182     {
183         // template method
184
}
185
186     /**
187      * Make a specific request to the underlying transport
188      *
189      * @param endpoint the endpoint to use when connecting to the resource
190      * @param timeout the maximum time the operation should block before returning.
191      * The call should return immediately if there is data available. If
192      * no data becomes available before the timeout elapses, null will be
193      * returned
194      * @return the result of the request wrapped in a UMOMessage object. Null will be
195      * returned if no data was avaialable
196      * @throws Exception if the call to the underlying protocal cuases an exception
197      */

198     protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception JavaDoc
199     {
200         throw new UnsupportedOperationException JavaDoc("doReceive");
201     }
202
203     public Object JavaDoc getDelegateSession() throws UMOException
204     {
205         return null;
206     }
207 }
208
Popular Tags