1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.quartz; |
8 | |
|
9 | |
import org.mule.DefaultMuleMessage; |
10 | |
import org.mule.api.MuleEvent; |
11 | |
import org.mule.api.MuleMessage; |
12 | |
import org.mule.api.endpoint.OutboundEndpoint; |
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.transport.AbstractMessageDispatcher; |
15 | |
import org.mule.transport.NullPayload; |
16 | |
import org.mule.transport.quartz.config.JobConfig; |
17 | |
import org.mule.transport.quartz.i18n.QuartzMessages; |
18 | |
import org.mule.transport.quartz.jobs.CustomJob; |
19 | |
import org.mule.transport.quartz.jobs.CustomJobConfig; |
20 | |
import org.mule.transport.quartz.jobs.CustomJobFromMessageConfig; |
21 | |
import org.mule.transport.quartz.jobs.ScheduledDispatchJobConfig; |
22 | |
|
23 | |
import java.util.Date; |
24 | |
|
25 | |
import org.quartz.CronTrigger; |
26 | |
import org.quartz.Job; |
27 | |
import org.quartz.JobDataMap; |
28 | |
import org.quartz.JobDetail; |
29 | |
import org.quartz.Scheduler; |
30 | |
import org.quartz.SimpleTrigger; |
31 | |
import org.quartz.Trigger; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class QuartzMessageDispatcher extends AbstractMessageDispatcher |
39 | |
{ |
40 | |
|
41 | |
public QuartzMessageDispatcher(OutboundEndpoint endpoint) |
42 | |
{ |
43 | 0 | super(endpoint); |
44 | 0 | } |
45 | |
|
46 | |
@Override |
47 | |
protected void doDispose() |
48 | |
{ |
49 | |
|
50 | 0 | } |
51 | |
|
52 | |
@Override |
53 | |
protected void doDispatch(MuleEvent event) throws Exception |
54 | |
{ |
55 | 0 | JobConfig jobConfig = (JobConfig) endpoint.getProperty(QuartzConnector.PROPERTY_JOB_CONFIG); |
56 | 0 | if (jobConfig == null) |
57 | |
{ |
58 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage()); |
59 | |
} |
60 | |
|
61 | 0 | JobDetail jobDetail = new JobDetail(); |
62 | |
|
63 | 0 | jobDetail.setName(event.getEndpoint().getEndpointURI().getAddress() + "-" + event.getId()); |
64 | |
|
65 | 0 | JobDataMap jobDataMap = new JobDataMap(); |
66 | 0 | MuleMessage msg = event.getMessage(); |
67 | |
|
68 | 0 | for (String key : msg.getInvocationPropertyNames()) |
69 | |
{ |
70 | 0 | jobDataMap.put(key, msg.getInvocationProperty(key)); |
71 | |
} |
72 | 0 | for (String key : msg.getOutboundPropertyNames()) |
73 | |
{ |
74 | 0 | jobDataMap.put(key, msg.getOutboundProperty(key)); |
75 | |
} |
76 | |
|
77 | 0 | if (jobConfig instanceof ScheduledDispatchJobConfig) |
78 | |
{ |
79 | 0 | ScheduledDispatchJobConfig scheduledDispatchJobConfig = (ScheduledDispatchJobConfig) jobConfig; |
80 | 0 | String endpointRef = event.getMuleContext().getExpressionManager().parse( |
81 | |
scheduledDispatchJobConfig.getEndpointRef(), event.getMessage()); |
82 | |
|
83 | 0 | jobDataMap.put("endpointRef", endpointRef); |
84 | |
} |
85 | 0 | jobDetail.setJobDataMap(jobDataMap); |
86 | |
|
87 | 0 | Job job = null; |
88 | |
|
89 | 0 | Object payload = event.getMessage().getPayload(); |
90 | |
|
91 | 0 | if(jobConfig instanceof CustomJobConfig) |
92 | |
{ |
93 | 0 | job = ((CustomJobConfig) jobConfig).getJob(); |
94 | |
} |
95 | 0 | else if(jobConfig instanceof CustomJobFromMessageConfig) |
96 | |
{ |
97 | 0 | job = ((CustomJobFromMessageConfig) jobConfig).getJob(msg); |
98 | |
|
99 | 0 | jobConfig = ((CustomJobFromMessageConfig) jobConfig).getJobConfig(msg); |
100 | |
} |
101 | |
|
102 | 0 | jobDataMap.put(QuartzConnector.PROPERTY_JOB_CONFIG, jobConfig); |
103 | 0 | jobDetail.setJobClass(jobConfig.getJobClass()); |
104 | |
|
105 | 0 | if (job != null) |
106 | |
{ |
107 | 0 | jobDataMap.put(QuartzConnector.PROPERTY_JOB_OBJECT, job); |
108 | 0 | jobDetail.setJobClass(CustomJob.class); |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | 0 | jobDataMap.put(QuartzConnector.PROPERTY_PAYLOAD, payload); |
114 | |
|
115 | |
Trigger trigger; |
116 | 0 | String cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION); |
117 | 0 | String repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL); |
118 | 0 | String repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT); |
119 | 0 | String startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY); |
120 | 0 | String groupName = jobConfig.getGroupName(); |
121 | 0 | String jobGroupName = jobConfig.getJobGroupName(); |
122 | |
|
123 | 0 | if (groupName == null) |
124 | |
{ |
125 | 0 | groupName = QuartzConnector.DEFAULT_GROUP_NAME; |
126 | |
} |
127 | 0 | if (jobGroupName == null) |
128 | |
{ |
129 | 0 | jobGroupName = groupName; |
130 | |
} |
131 | |
|
132 | 0 | jobDetail.setGroup(groupName); |
133 | |
|
134 | 0 | if (cronExpression != null) |
135 | |
{ |
136 | 0 | CronTrigger ctrigger = new CronTrigger(); |
137 | 0 | ctrigger.setCronExpression(cronExpression); |
138 | 0 | trigger = ctrigger; |
139 | 0 | } |
140 | 0 | else if (repeatInterval != null) |
141 | |
{ |
142 | 0 | SimpleTrigger strigger = new SimpleTrigger(); |
143 | 0 | strigger.setRepeatInterval(Long.parseLong(repeatInterval)); |
144 | 0 | if (repeatCount != null) |
145 | |
{ |
146 | 0 | strigger.setRepeatCount(Integer.parseInt(repeatCount)); |
147 | |
} |
148 | |
else |
149 | |
{ |
150 | 0 | strigger.setRepeatCount(-1); |
151 | |
} |
152 | 0 | trigger = strigger; |
153 | 0 | } |
154 | |
else |
155 | |
{ |
156 | 0 | throw new IllegalArgumentException( |
157 | |
QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage()); |
158 | |
} |
159 | 0 | trigger.setName(event.getEndpoint().getEndpointURI().toString() + "-" + event.getId()); |
160 | 0 | trigger.setGroup(groupName); |
161 | 0 | trigger.setJobName(jobDetail.getName()); |
162 | 0 | trigger.setJobGroup(jobGroupName); |
163 | |
|
164 | 0 | Scheduler scheduler = ((QuartzConnector) this.getConnector()).getQuartzScheduler(); |
165 | |
|
166 | |
|
167 | 0 | long start = System.currentTimeMillis(); |
168 | 0 | if (startDelay != null) |
169 | |
{ |
170 | 0 | start += Long.parseLong(startDelay); |
171 | |
} |
172 | 0 | trigger.setStartTime(new Date(start)); |
173 | |
|
174 | 0 | scheduler.scheduleJob(jobDetail, trigger); |
175 | 0 | } |
176 | |
|
177 | |
@Override |
178 | |
protected MuleMessage doSend(MuleEvent event) throws Exception |
179 | |
{ |
180 | 0 | doDispatch(event); |
181 | 0 | return new DefaultMuleMessage(NullPayload.getInstance(), connector.getMuleContext()); |
182 | |
} |
183 | |
|
184 | |
@Override |
185 | |
protected void doConnect() throws Exception |
186 | |
{ |
187 | |
|
188 | 0 | } |
189 | |
|
190 | |
@Override |
191 | |
protected void doDisconnect() throws Exception |
192 | |
{ |
193 | |
|
194 | 0 | } |
195 | |
|
196 | |
} |