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.endpoint.EndpointException;
15 import org.mule.api.endpoint.InboundEndpoint;
16 import org.mule.api.lifecycle.CreateException;
17 import org.mule.api.service.Service;
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.i18n.QuartzMessages;
22 import org.mule.transport.quartz.config.JobConfig;
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(Connector connector, Service service, InboundEndpoint endpoint)
47 throws CreateException
48 {
49 super(connector, service, endpoint);
50 this.connector = (QuartzConnector) connector;
51 }
52
53 protected void doDispose()
54 {
55
56 }
57
58 protected void doStart() throws MuleException
59 {
60 try
61 {
62 Scheduler scheduler = connector.getQuartzScheduler();
63
64 JobConfig jobConfig = (JobConfig)endpoint.getProperty(QuartzConnector.PROPERTY_JOB_CONFIG);
65 if(jobConfig==null)
66 {
67 throw new IllegalArgumentException(CoreMessages.objectIsNull(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage());
68 }
69 JobDetail jobDetail = new JobDetail();
70 jobDetail.setName(endpoint.getEndpointURI().getAddress());
71 jobDetail.setJobClass(jobConfig.getJobClass());
72 JobDataMap jobDataMap = new JobDataMap();
73 jobDataMap.put(QUARTZ_RECEIVER_PROPERTY, this.getReceiverKey());
74 jobDataMap.put(QUARTZ_CONNECTOR_PROPERTY, this.connector.getName());
75 jobDataMap.putAll(endpoint.getProperties());
76 jobDetail.setJobDataMap(jobDataMap);
77
78 Trigger trigger;
79 String cronExpression = (String)endpoint.getProperty(QuartzConnector.PROPERTY_CRON_EXPRESSION);
80 String repeatInterval = (String)endpoint.getProperty(QuartzConnector.PROPERTY_REPEAT_INTERVAL);
81 String repeatCount = (String)endpoint.getProperty(QuartzConnector.PROPERTY_REPEAT_COUNT);
82 String startDelay = (String)endpoint.getProperty(QuartzConnector.PROPERTY_START_DELAY);
83 String groupName = jobConfig.getGroupName();
84 String jobGroupName = jobConfig.getJobGroupName();
85
86 if (groupName == null)
87 {
88 groupName = QuartzConnector.DEFAULT_GROUP_NAME;
89 }
90 if (jobGroupName == null)
91 {
92 jobGroupName = groupName;
93 }
94
95 jobDetail.setGroup(groupName);
96
97 if (cronExpression != null)
98 {
99 CronTrigger ctrigger = new CronTrigger();
100 ctrigger.setCronExpression(cronExpression);
101 trigger = ctrigger;
102 }
103 else if (repeatInterval != null)
104 {
105 SimpleTrigger strigger = new SimpleTrigger();
106 strigger.setRepeatInterval(Long.parseLong(repeatInterval));
107 if (repeatCount != null)
108 {
109 strigger.setRepeatCount(Integer.parseInt(repeatCount));
110 }
111 else
112 {
113 strigger.setRepeatCount(-1);
114 }
115 trigger = strigger;
116 }
117 else
118 {
119 throw new IllegalArgumentException(
120 QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage());
121 }
122 long start = System.currentTimeMillis();
123 if (startDelay != null)
124 {
125 start += Long.parseLong(startDelay);
126 }
127 trigger.setStartTime(new Date(start));
128 trigger.setName(endpoint.getEndpointURI().getAddress());
129 trigger.setGroup(groupName);
130 trigger.setJobName(endpoint.getEndpointURI().getAddress());
131 trigger.setJobGroup(jobGroupName);
132
133
134
135 try
136 {
137 scheduler.scheduleJob(jobDetail, trigger);
138 }
139 catch (ObjectAlreadyExistsException oaee)
140 {
141
142 logger.warn("A quartz Job with name: " + endpoint.getEndpointURI().getAddress() +
143 " has already been registered. Cannot register again");
144 }
145
146 scheduler.start();
147 }
148 catch (Exception e)
149 {
150 throw new EndpointException(CoreMessages.failedToStart("Quartz receiver"), e);
151 }
152 }
153
154 protected void doStop() throws MuleException
155 {
156
157 }
158
159 protected void doConnect() throws Exception
160 {
161
162 }
163
164 protected void doDisconnect() throws Exception
165 {
166
167 }
168
169 }