1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.quartz; |
12 | |
|
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.construct.FlowConstruct; |
15 | |
import org.mule.api.endpoint.EndpointException; |
16 | |
import org.mule.api.endpoint.InboundEndpoint; |
17 | |
import org.mule.api.lifecycle.CreateException; |
18 | |
import org.mule.api.transport.Connector; |
19 | |
import org.mule.config.i18n.CoreMessages; |
20 | |
import org.mule.transport.AbstractMessageReceiver; |
21 | |
import org.mule.transport.quartz.config.JobConfig; |
22 | |
import org.mule.transport.quartz.i18n.QuartzMessages; |
23 | |
import org.mule.transport.quartz.jobs.CustomJobConfig; |
24 | |
import org.mule.transport.quartz.jobs.EventGeneratorJobConfig; |
25 | |
|
26 | |
import java.util.Date; |
27 | |
|
28 | |
import org.quartz.CronTrigger; |
29 | |
import org.quartz.Job; |
30 | |
import org.quartz.JobDataMap; |
31 | |
import org.quartz.JobDetail; |
32 | |
import org.quartz.ObjectAlreadyExistsException; |
33 | |
import org.quartz.Scheduler; |
34 | |
import org.quartz.SimpleTrigger; |
35 | |
import org.quartz.Trigger; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class QuartzMessageReceiver extends AbstractMessageReceiver |
42 | |
{ |
43 | |
|
44 | |
public static final String QUARTZ_RECEIVER_PROPERTY = "mule.quartz.receiver"; |
45 | |
public static final String QUARTZ_CONNECTOR_PROPERTY = "mule.quartz.connector"; |
46 | |
|
47 | |
private final QuartzConnector connector; |
48 | |
|
49 | |
public QuartzMessageReceiver(Connector connector, FlowConstruct flowConstruct, InboundEndpoint endpoint) |
50 | |
throws CreateException |
51 | |
{ |
52 | 0 | super(connector, flowConstruct, endpoint); |
53 | 0 | this.connector = (QuartzConnector) connector; |
54 | 0 | } |
55 | |
|
56 | |
@Override |
57 | |
protected void doDispose() |
58 | |
{ |
59 | |
|
60 | 0 | } |
61 | |
|
62 | |
@Override |
63 | |
protected void doStart() throws MuleException |
64 | |
{ |
65 | |
try |
66 | |
{ |
67 | 0 | Scheduler scheduler = connector.getQuartzScheduler(); |
68 | |
|
69 | 0 | JobConfig jobConfig = (JobConfig) endpoint.getProperty(QuartzConnector.PROPERTY_JOB_CONFIG); |
70 | 0 | if (jobConfig == null) |
71 | |
{ |
72 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage()); |
73 | |
} |
74 | |
|
75 | 0 | JobDetail jobDetail = new JobDetail(); |
76 | 0 | jobDetail.setName(endpoint.getEndpointURI().getAddress()); |
77 | 0 | jobDetail.setJobClass(jobConfig.getJobClass()); |
78 | 0 | JobDataMap jobDataMap = new JobDataMap(); |
79 | 0 | jobDataMap.put(QUARTZ_RECEIVER_PROPERTY, this.getReceiverKey()); |
80 | 0 | jobDataMap.put(QUARTZ_CONNECTOR_PROPERTY, this.connector.getName()); |
81 | 0 | jobDataMap.putAll(endpoint.getProperties()); |
82 | |
|
83 | 0 | if (jobConfig instanceof EventGeneratorJobConfig) |
84 | |
{ |
85 | 0 | jobDataMap.put(QuartzConnector.PROPERTY_PAYLOAD, ((EventGeneratorJobConfig) jobConfig).getPayload()); |
86 | |
} |
87 | 0 | jobDataMap.put(QuartzConnector.PROPERTY_JOB_CONFIG, jobConfig); |
88 | |
|
89 | 0 | Job job = null; |
90 | 0 | if (jobConfig instanceof CustomJobConfig) |
91 | |
{ |
92 | 0 | job = ((CustomJobConfig) jobConfig).getJob(); |
93 | |
} |
94 | |
|
95 | 0 | if (job != null) |
96 | |
{ |
97 | 0 | jobDataMap.put(QuartzConnector.PROPERTY_JOB_OBJECT, job); |
98 | 0 | jobDetail.setJobClass(jobConfig.getJobClass()); |
99 | |
} |
100 | |
|
101 | 0 | jobDetail.setJobDataMap(jobDataMap); |
102 | |
|
103 | |
Trigger trigger; |
104 | 0 | String cronExpression = (String)endpoint.getProperty(QuartzConnector.PROPERTY_CRON_EXPRESSION); |
105 | 0 | String repeatInterval = (String)endpoint.getProperty(QuartzConnector.PROPERTY_REPEAT_INTERVAL); |
106 | 0 | String repeatCount = (String)endpoint.getProperty(QuartzConnector.PROPERTY_REPEAT_COUNT); |
107 | 0 | String startDelay = (String)endpoint.getProperty(QuartzConnector.PROPERTY_START_DELAY); |
108 | 0 | String groupName = jobConfig.getGroupName(); |
109 | 0 | String jobGroupName = jobConfig.getJobGroupName(); |
110 | |
|
111 | 0 | if (groupName == null) |
112 | |
{ |
113 | 0 | groupName = QuartzConnector.DEFAULT_GROUP_NAME; |
114 | |
} |
115 | 0 | if (jobGroupName == null) |
116 | |
{ |
117 | 0 | jobGroupName = groupName; |
118 | |
} |
119 | |
|
120 | 0 | jobDetail.setGroup(groupName); |
121 | |
|
122 | 0 | if (cronExpression != null) |
123 | |
{ |
124 | 0 | CronTrigger ctrigger = new CronTrigger(); |
125 | 0 | ctrigger.setCronExpression(cronExpression); |
126 | 0 | trigger = ctrigger; |
127 | 0 | } |
128 | 0 | else if (repeatInterval != null) |
129 | |
{ |
130 | 0 | SimpleTrigger strigger = new SimpleTrigger(); |
131 | 0 | strigger.setRepeatInterval(Long.parseLong(repeatInterval)); |
132 | 0 | if (repeatCount != null) |
133 | |
{ |
134 | 0 | strigger.setRepeatCount(Integer.parseInt(repeatCount)); |
135 | |
} |
136 | |
else |
137 | |
{ |
138 | 0 | strigger.setRepeatCount(-1); |
139 | |
} |
140 | 0 | trigger = strigger; |
141 | 0 | } |
142 | |
else |
143 | |
{ |
144 | 0 | throw new IllegalArgumentException( |
145 | |
QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage()); |
146 | |
} |
147 | |
|
148 | 0 | trigger.setName(endpoint.getEndpointURI().getAddress()); |
149 | 0 | trigger.setGroup(groupName); |
150 | 0 | trigger.setJobName(endpoint.getEndpointURI().getAddress()); |
151 | 0 | trigger.setJobGroup(jobGroupName); |
152 | |
|
153 | |
|
154 | 0 | long start = System.currentTimeMillis(); |
155 | 0 | if (startDelay != null) |
156 | |
{ |
157 | 0 | start += Long.parseLong(startDelay); |
158 | |
} |
159 | 0 | trigger.setStartTime(new Date(start)); |
160 | |
|
161 | |
|
162 | |
try |
163 | |
{ |
164 | 0 | scheduler.scheduleJob(jobDetail, trigger); |
165 | |
} |
166 | 0 | catch (ObjectAlreadyExistsException oaee) |
167 | |
{ |
168 | 0 | logger.warn("A quartz Job with name: " + endpoint.getEndpointURI().getAddress() + |
169 | |
" has already been registered. Cannot register again"); |
170 | 0 | } |
171 | |
} |
172 | 0 | catch (Exception e) |
173 | |
{ |
174 | 0 | throw new EndpointException(CoreMessages.failedToStart("Quartz receiver"), e); |
175 | 0 | } |
176 | 0 | } |
177 | |
|
178 | |
@Override |
179 | |
protected void doStop() throws MuleException |
180 | |
{ |
181 | |
|
182 | 0 | } |
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 | |
} |