1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class QuartzMessageDispatcher extends AbstractMessageDispatcher |
41 | |
{ |
42 | |
|
43 | |
public QuartzMessageDispatcher(OutboundEndpoint endpoint) |
44 | |
{ |
45 | 6 | super(endpoint); |
46 | 6 | } |
47 | |
|
48 | |
protected void doDispose() |
49 | |
{ |
50 | |
|
51 | 6 | } |
52 | |
|
53 | |
protected void doDispatch(MuleEvent event) throws Exception |
54 | |
{ |
55 | 6 | JobConfig jobConfig = (JobConfig)endpoint.getProperty(QuartzConnector.PROPERTY_JOB_CONFIG); |
56 | 6 | if(jobConfig==null) |
57 | |
{ |
58 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage()); |
59 | |
} |
60 | |
|
61 | 6 | JobDetail jobDetail = new JobDetail(); |
62 | |
|
63 | 6 | jobDetail.setName(event.getEndpoint().getEndpointURI().getAddress() + "-" + event.getId()); |
64 | |
|
65 | 6 | JobDataMap jobDataMap = new JobDataMap(); |
66 | 6 | MuleMessage msg = event.getMessage(); |
67 | 6 | for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();) |
68 | |
{ |
69 | 42 | String propertyKey = (String)iterator.next(); |
70 | 42 | jobDataMap.put(propertyKey, msg.getProperty(propertyKey)); |
71 | 42 | } |
72 | 6 | jobDetail.setJobDataMap(jobDataMap); |
73 | |
|
74 | 6 | Job job = null; |
75 | |
|
76 | 6 | Object payload = event.transformMessage(); |
77 | |
|
78 | 6 | if(jobConfig instanceof CustomJobConfig) |
79 | |
{ |
80 | 0 | job = ((CustomJobConfig)jobConfig).getJob(); |
81 | |
} |
82 | 6 | else if(jobConfig instanceof CustomJobFromMessageConfig) |
83 | |
{ |
84 | 4 | job = ((CustomJobFromMessageConfig)jobConfig).getJob(msg); |
85 | |
|
86 | 4 | jobConfig = ((CustomJobFromMessageConfig)jobConfig).getJobConfig(msg); |
87 | |
} |
88 | |
|
89 | 6 | jobDataMap.put(QuartzConnector.PROPERTY_JOB_CONFIG, jobConfig); |
90 | 6 | jobDetail.setJobClass(jobConfig.getJobClass()); |
91 | |
|
92 | 6 | if (job!=null ) |
93 | |
{ |
94 | 4 | jobDataMap.put(QuartzConnector.PROPERTY_JOB_OBJECT, job); |
95 | 4 | jobDetail.setJobClass(CustomJob.class); |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | 6 | jobDataMap.put(QuartzConnector.PROPERTY_PAYLOAD, payload); |
103 | |
|
104 | |
Trigger trigger; |
105 | 6 | String cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION); |
106 | 6 | String repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL); |
107 | 6 | String repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT); |
108 | 6 | String startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY); |
109 | 6 | String groupName = jobConfig.getGroupName(); |
110 | 6 | String jobGroupName = jobConfig.getJobGroupName(); |
111 | |
|
112 | 6 | if (groupName == null) |
113 | |
{ |
114 | 0 | groupName = QuartzConnector.DEFAULT_GROUP_NAME; |
115 | |
} |
116 | 6 | if (jobGroupName == null) |
117 | |
{ |
118 | 0 | jobGroupName = groupName; |
119 | |
} |
120 | |
|
121 | 6 | jobDetail.setGroup(groupName); |
122 | |
|
123 | 6 | if (cronExpression != null) |
124 | |
{ |
125 | 0 | CronTrigger ctrigger = new CronTrigger(); |
126 | 0 | ctrigger.setCronExpression(cronExpression); |
127 | 0 | trigger = ctrigger; |
128 | 0 | } |
129 | 6 | else if (repeatInterval != null) |
130 | |
{ |
131 | 6 | SimpleTrigger strigger = new SimpleTrigger(); |
132 | 6 | strigger.setRepeatInterval(Long.parseLong(repeatInterval)); |
133 | 6 | if (repeatCount != null) |
134 | |
{ |
135 | 0 | strigger.setRepeatCount(Integer.parseInt(repeatCount)); |
136 | |
} |
137 | |
else |
138 | |
{ |
139 | 6 | strigger.setRepeatCount(-1); |
140 | |
} |
141 | 6 | trigger = strigger; |
142 | 6 | } |
143 | |
else |
144 | |
{ |
145 | 0 | throw new IllegalArgumentException( |
146 | |
QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage()); |
147 | |
} |
148 | 6 | long start = System.currentTimeMillis(); |
149 | 6 | if (startDelay != null) |
150 | |
{ |
151 | 0 | start += Long.parseLong(startDelay); |
152 | |
} |
153 | 6 | trigger.setStartTime(new Date(start)); |
154 | 6 | trigger.setName(event.getEndpoint().getEndpointURI().toString() + "-" + event.getId()); |
155 | 6 | trigger.setGroup(groupName); |
156 | 6 | trigger.setJobName(jobDetail.getName()); |
157 | 6 | trigger.setJobGroup(jobGroupName); |
158 | |
|
159 | 6 | Scheduler scheduler = ((QuartzConnector)this.getConnector()).getQuartzScheduler(); |
160 | 6 | scheduler.scheduleJob(jobDetail, trigger); |
161 | |
|
162 | 6 | } |
163 | |
|
164 | |
protected MuleMessage doSend(MuleEvent event) throws Exception |
165 | |
{ |
166 | 6 | doDispatch(event); |
167 | 6 | return null; |
168 | |
} |
169 | |
|
170 | |
protected void doConnect() throws Exception |
171 | |
{ |
172 | |
|
173 | 6 | } |
174 | |
|
175 | |
protected void doDisconnect() throws Exception |
176 | |
{ |
177 | |
|
178 | 6 | } |
179 | |
|
180 | |
} |