1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.quartz; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.providers.AbstractMessageReceiver; |
15 | |
import org.mule.providers.quartz.i18n.QuartzMessages; |
16 | |
import org.mule.providers.quartz.jobs.MuleReceiverJob; |
17 | |
import org.mule.umo.UMOComponent; |
18 | |
import org.mule.umo.UMOException; |
19 | |
import org.mule.umo.endpoint.EndpointException; |
20 | |
import org.mule.umo.endpoint.UMOEndpoint; |
21 | |
import org.mule.umo.lifecycle.InitialisationException; |
22 | |
import org.mule.umo.provider.UMOConnector; |
23 | |
|
24 | |
import java.util.Date; |
25 | |
|
26 | |
import org.quartz.CronTrigger; |
27 | |
import org.quartz.JobDataMap; |
28 | |
import org.quartz.JobDetail; |
29 | |
import org.quartz.ObjectAlreadyExistsException; |
30 | |
import org.quartz.Scheduler; |
31 | |
import org.quartz.SimpleTrigger; |
32 | |
import org.quartz.Trigger; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class QuartzMessageReceiver extends AbstractMessageReceiver |
39 | |
{ |
40 | |
|
41 | |
public static final String QUARTZ_RECEIVER_PROPERTY = "mule.quartz.receiver"; |
42 | |
public static final String QUARTZ_CONNECTOR_PROPERTY = "mule.quartz.connector"; |
43 | |
|
44 | |
private final QuartzConnector connector; |
45 | |
|
46 | |
public QuartzMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint) |
47 | |
throws InitialisationException |
48 | |
{ |
49 | 6 | super(connector, component, endpoint); |
50 | 6 | this.connector = (QuartzConnector)connector; |
51 | 6 | } |
52 | |
|
53 | |
protected void doDispose() |
54 | |
{ |
55 | |
|
56 | 6 | } |
57 | |
|
58 | |
protected void doStart() throws UMOException |
59 | |
{ |
60 | |
try |
61 | |
{ |
62 | 4 | Scheduler scheduler = connector.getQuartzScheduler(); |
63 | |
|
64 | 4 | JobDetail jobDetail = new JobDetail(); |
65 | 4 | jobDetail.setName(endpoint.getEndpointURI().toString()); |
66 | 6 | jobDetail.setJobClass(MuleReceiverJob.class); |
67 | 4 | JobDataMap jobDataMap = new JobDataMap(); |
68 | 4 | jobDataMap.put(QUARTZ_RECEIVER_PROPERTY, this.getReceiverKey()); |
69 | 4 | jobDataMap.put(QUARTZ_CONNECTOR_PROPERTY, this.connector.getName()); |
70 | 4 | jobDataMap.putAll(endpoint.getProperties()); |
71 | 4 | jobDetail.setJobDataMap(jobDataMap); |
72 | |
|
73 | 4 | Trigger trigger = null; |
74 | 4 | String cronExpression = jobDataMap.getString(QuartzConnector.PROPERTY_CRON_EXPRESSION); |
75 | 4 | String repeatInterval = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_INTERVAL); |
76 | 4 | String repeatCount = jobDataMap.getString(QuartzConnector.PROPERTY_REPEAT_COUNT); |
77 | 4 | String startDelay = jobDataMap.getString(QuartzConnector.PROPERTY_START_DELAY); |
78 | 4 | String groupName = jobDataMap.getString(QuartzConnector.PROPERTY_GROUP_NAME); |
79 | 4 | String jobGroupName = jobDataMap.getString(QuartzConnector.PROPERTY_JOB_GROUP_NAME); |
80 | |
|
81 | 4 | if (groupName == null) |
82 | |
{ |
83 | 4 | groupName = QuartzConnector.DEFAULT_GROUP_NAME; |
84 | |
} |
85 | 4 | if (jobGroupName == null) |
86 | |
{ |
87 | 4 | jobGroupName = groupName; |
88 | |
} |
89 | |
|
90 | 4 | jobDetail.setGroup(groupName); |
91 | |
|
92 | 4 | if (cronExpression != null) |
93 | |
{ |
94 | 0 | CronTrigger ctrigger = new CronTrigger(); |
95 | 0 | ctrigger.setCronExpression(cronExpression); |
96 | 0 | trigger = ctrigger; |
97 | 0 | } |
98 | 4 | else if (repeatInterval != null) |
99 | |
{ |
100 | 4 | SimpleTrigger strigger = new SimpleTrigger(); |
101 | 4 | strigger.setRepeatInterval(Long.parseLong(repeatInterval)); |
102 | 4 | if (repeatCount != null) |
103 | |
{ |
104 | 4 | strigger.setRepeatCount(Integer.parseInt(repeatCount)); |
105 | |
} |
106 | |
else |
107 | |
{ |
108 | 0 | strigger.setRepeatCount(-1); |
109 | |
} |
110 | 4 | trigger = strigger; |
111 | 4 | } |
112 | |
else |
113 | |
{ |
114 | 0 | throw new IllegalArgumentException( |
115 | |
QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage()); |
116 | |
} |
117 | 4 | long start = System.currentTimeMillis(); |
118 | 4 | if (startDelay != null) |
119 | |
{ |
120 | 0 | start += Long.parseLong(startDelay); |
121 | |
} |
122 | 4 | trigger.setStartTime(new Date(start)); |
123 | 4 | trigger.setName(endpoint.getEndpointURI().toString()); |
124 | 4 | trigger.setGroup(groupName); |
125 | 4 | trigger.setJobName(endpoint.getEndpointURI().toString()); |
126 | 4 | trigger.setJobGroup(jobGroupName); |
127 | |
|
128 | |
|
129 | |
|
130 | |
try |
131 | |
{ |
132 | 4 | scheduler.scheduleJob(jobDetail, trigger); |
133 | |
} |
134 | 0 | catch (ObjectAlreadyExistsException oaee) |
135 | |
{ |
136 | |
|
137 | 4 | } |
138 | |
|
139 | 4 | scheduler.start(); |
140 | |
} |
141 | 0 | catch (Exception e) |
142 | |
{ |
143 | 0 | throw new EndpointException(CoreMessages.failedToStart("Quartz receiver"), e); |
144 | 4 | } |
145 | 4 | } |
146 | |
|
147 | |
protected void doStop() throws UMOException |
148 | |
{ |
149 | |
|
150 | 4 | } |
151 | |
|
152 | |
protected void doConnect() throws Exception |
153 | |
{ |
154 | |
|
155 | 4 | } |
156 | |
|
157 | |
protected void doDisconnect() throws Exception |
158 | |
{ |
159 | |
|
160 | 4 | } |
161 | |
|
162 | |
} |