1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.quartz; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.providers.AbstractMessageDispatcher; |
15 | |
import org.mule.providers.quartz.i18n.QuartzMessages; |
16 | |
import org.mule.providers.quartz.jobs.DelegatingJob; |
17 | |
import org.mule.umo.UMOEvent; |
18 | |
import org.mule.umo.UMOMessage; |
19 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
20 | |
import org.mule.umo.provider.DispatchException; |
21 | |
import org.mule.util.ClassUtils; |
22 | |
|
23 | |
import java.util.Date; |
24 | |
import java.util.Iterator; |
25 | |
|
26 | |
import org.quartz.CronTrigger; |
27 | |
import org.quartz.Job; |
28 | |
import org.quartz.JobDataMap; |
29 | |
import org.quartz.JobDetail; |
30 | |
import org.quartz.Scheduler; |
31 | |
import org.quartz.SimpleTrigger; |
32 | |
import org.quartz.Trigger; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class QuartzMessageDispatcher extends AbstractMessageDispatcher |
40 | |
{ |
41 | |
|
42 | |
public QuartzMessageDispatcher(UMOImmutableEndpoint endpoint) |
43 | |
{ |
44 | 0 | super(endpoint); |
45 | 0 | } |
46 | |
|
47 | |
protected void doDispose() |
48 | |
{ |
49 | |
|
50 | 0 | } |
51 | |
|
52 | |
protected void doDispatch(UMOEvent event) throws Exception |
53 | |
{ |
54 | 0 | JobDetail jobDetail = new JobDetail(); |
55 | |
|
56 | 0 | jobDetail.setName(event.getEndpoint().getEndpointURI().toString() + "-" + event.getId()); |
57 | |
|
58 | 0 | JobDataMap jobDataMap = new JobDataMap(); |
59 | 0 | UMOMessage msg = event.getMessage(); |
60 | 0 | for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();) |
61 | |
{ |
62 | 0 | String propertyKey = (String)iterator.next(); |
63 | 0 | jobDataMap.put(propertyKey, msg.getProperty(propertyKey)); |
64 | |
} |
65 | 0 | jobDetail.setJobDataMap(jobDataMap); |
66 | |
|
67 | |
Job job; |
68 | |
|
69 | 0 | Object payload = event.getTransformedMessage(); |
70 | |
|
71 | 0 | String jobClass = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_CLASS); |
72 | |
|
73 | |
|
74 | |
|
75 | 0 | if (payload instanceof Job) |
76 | |
{ |
77 | 0 | job = (Job)payload; |
78 | 0 | jobDataMap.put(QuartzConnector.PROPERTY_JOB_OBJECT, job); |
79 | 0 | jobDetail.setJobClass(DelegatingJob.class); |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | 0 | else if (jobClass != null) |
85 | |
{ |
86 | 0 | jobDetail.setJobClass(ClassUtils.loadClass(jobClass, getClass())); |
87 | |
} |
88 | |
|
89 | |
else |
90 | |
{ |
91 | |
|
92 | 0 | Object tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_OBJECT); |
93 | 0 | if (tempJob == null) |
94 | |
{ |
95 | |
|
96 | 0 | tempJob = jobDataMap.get(QuartzConnector.PROPERTY_JOB_REF); |
97 | 0 | if (tempJob == null) |
98 | |
{ |
99 | |
|
100 | 0 | throw new DispatchException(QuartzMessages.invalidPayloadType(), |
101 | |
event.getMessage(), event.getEndpoint()); |
102 | |
} |
103 | |
else |
104 | |
{ |
105 | 0 | tempJob = MuleManager.getInstance().getContainerContext().getComponent(tempJob); |
106 | 0 | if (!(tempJob instanceof Job)) |
107 | |
{ |
108 | 0 | throw new DispatchException(QuartzMessages.invalidJobObject(), |
109 | |
event.getMessage(), event.getEndpoint()); |
110 | |
} |
111 | |
} |
112 | |
} |
113 | 0 | else if (!(tempJob instanceof Job)) |
114 | |
{ |
115 | 0 | throw new DispatchException(QuartzMessages.invalidJobObject(), |
116 | |
event.getMessage(), event.getEndpoint()); |
117 | |
} |
118 | |
|
119 | |
|
120 | 0 | jobDetail.setJobClass(DelegatingJob.class); |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | 0 | jobDataMap.put(QuartzConnector.PROPERTY_PAYLOAD, payload); |
126 | |
|
127 | 0 | Trigger trigger = null; |
128 | 0 | String cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION); |
129 | 0 | String repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL); |
130 | 0 | String repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT); |
131 | 0 | String startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY); |
132 | 0 | String groupName = jobDataMap.getString(QuartzConnector.PROPERTY_GROUP_NAME); |
133 | 0 | String jobGroupName = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_GROUP_NAME); |
134 | |
|
135 | 0 | if (groupName == null) |
136 | |
{ |
137 | 0 | groupName = QuartzConnector.DEFAULT_GROUP_NAME; |
138 | |
} |
139 | 0 | if (jobGroupName == null) |
140 | |
{ |
141 | 0 | jobGroupName = groupName; |
142 | |
} |
143 | |
|
144 | 0 | jobDetail.setGroup(groupName); |
145 | |
|
146 | 0 | if (cronExpression != null) |
147 | |
{ |
148 | 0 | CronTrigger ctrigger = new CronTrigger(); |
149 | 0 | ctrigger.setCronExpression(cronExpression); |
150 | 0 | trigger = ctrigger; |
151 | |
} |
152 | 0 | else if (repeatInterval != null) |
153 | |
{ |
154 | 0 | SimpleTrigger strigger = new SimpleTrigger(); |
155 | 0 | strigger.setRepeatInterval(Long.parseLong(repeatInterval)); |
156 | 0 | if (repeatCount != null) |
157 | |
{ |
158 | 0 | strigger.setRepeatCount(Integer.parseInt(repeatCount)); |
159 | |
} |
160 | |
else |
161 | |
{ |
162 | 0 | strigger.setRepeatCount(-1); |
163 | |
} |
164 | 0 | trigger = strigger; |
165 | |
} |
166 | |
else |
167 | |
{ |
168 | 0 | throw new IllegalArgumentException( |
169 | |
QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage()); |
170 | |
} |
171 | 0 | long start = System.currentTimeMillis(); |
172 | 0 | if (startDelay != null) |
173 | |
{ |
174 | 0 | start += Long.parseLong(startDelay); |
175 | |
} |
176 | 0 | trigger.setStartTime(new Date(start)); |
177 | 0 | trigger.setName(event.getEndpoint().getEndpointURI().toString() + "-" + event.getId()); |
178 | 0 | trigger.setGroup(groupName); |
179 | 0 | trigger.setJobName(jobDetail.getName()); |
180 | 0 | trigger.setJobGroup(jobGroupName); |
181 | |
|
182 | 0 | Scheduler scheduler = ((QuartzConnector)this.getConnector()).getQuartzScheduler(); |
183 | 0 | scheduler.scheduleJob(jobDetail, trigger); |
184 | 0 | } |
185 | |
|
186 | |
protected UMOMessage doSend(UMOEvent event) throws Exception |
187 | |
{ |
188 | 0 | doDispatch(event); |
189 | 0 | return null; |
190 | |
} |
191 | |
|
192 | |
protected void doConnect() throws Exception |
193 | |
{ |
194 | |
|
195 | 0 | } |
196 | |
|
197 | |
protected void doDisconnect() throws Exception |
198 | |
{ |
199 | |
|
200 | 0 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
protected UMOMessage doReceive(long timeout) throws Exception |
215 | |
{ |
216 | 0 | throw new UnsupportedOperationException("doReceive"); |
217 | |
} |
218 | |
|
219 | |
} |