1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.quartz.jobs; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.ThreadSafeAccess; |
16 | |
import org.mule.api.config.MuleProperties; |
17 | |
import org.mule.api.endpoint.EndpointBuilder; |
18 | |
import org.mule.api.endpoint.InboundEndpoint; |
19 | |
import org.mule.api.transaction.Transaction; |
20 | |
import org.mule.api.transaction.TransactionCallback; |
21 | |
import org.mule.api.transport.PropertyScope; |
22 | |
import org.mule.module.client.MuleClient; |
23 | |
import org.mule.transaction.MuleTransactionConfig; |
24 | |
import org.mule.transaction.TransactionCoordination; |
25 | |
import org.mule.transaction.TransactionTemplate; |
26 | |
import org.mule.transport.AbstractMessageReceiver; |
27 | |
import org.mule.transport.quartz.QuartzConnector; |
28 | |
import org.mule.transport.quartz.QuartzMessageReceiver; |
29 | |
import org.mule.transport.quartz.i18n.QuartzMessages; |
30 | |
|
31 | |
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; |
32 | |
import org.apache.commons.logging.Log; |
33 | |
import org.apache.commons.logging.LogFactory; |
34 | |
import org.quartz.Job; |
35 | |
import org.quartz.JobDataMap; |
36 | |
import org.quartz.JobExecutionContext; |
37 | |
import org.quartz.JobExecutionException; |
38 | |
import org.quartz.SchedulerException; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class EndpointPollingJob implements Job |
44 | |
{ |
45 | |
|
46 | |
|
47 | |
|
48 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
49 | |
|
50 | |
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException |
51 | |
{ |
52 | |
MuleContext muleContext; |
53 | |
try |
54 | |
{ |
55 | 0 | muleContext = (MuleContext)jobExecutionContext.getScheduler().getContext().get(MuleProperties.MULE_CONTEXT_PROPERTY); |
56 | |
} |
57 | 0 | catch (SchedulerException e) |
58 | |
{ |
59 | 0 | throw new JobExecutionException("Failed to retrieve Mulecontext from the Scheduler Context: " + e.getMessage(), e); |
60 | 0 | } |
61 | |
|
62 | 0 | final JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap(); |
63 | |
|
64 | 0 | String receiverKey = (String) jobDataMap.get(QuartzMessageReceiver.QUARTZ_RECEIVER_PROPERTY); |
65 | 0 | if (receiverKey == null) |
66 | |
{ |
67 | 0 | throw new JobExecutionException(QuartzMessages.receiverNotInJobDataMap().getMessage()); |
68 | |
} |
69 | |
|
70 | 0 | String connectorName = (String) jobDataMap.get(QuartzMessageReceiver.QUARTZ_CONNECTOR_PROPERTY); |
71 | 0 | if (connectorName == null) |
72 | |
{ |
73 | 0 | throw new JobExecutionException(QuartzMessages.connectorNotInJobDataMap().getMessage()); |
74 | |
} |
75 | |
|
76 | 0 | final QuartzConnector connector = (QuartzConnector) muleContext.getRegistry().lookupConnector(connectorName); |
77 | 0 | if (connector == null) |
78 | |
{ |
79 | 0 | throw new JobExecutionException(QuartzMessages.noConnectorFound(connectorName).getMessage()); |
80 | |
} |
81 | |
|
82 | 0 | final AbstractMessageReceiver receiver = (AbstractMessageReceiver) connector.lookupReceiver(receiverKey); |
83 | 0 | if (receiver == null) |
84 | |
{ |
85 | 0 | throw new JobExecutionException( |
86 | |
QuartzMessages.noReceiverInConnector(receiverKey, connectorName).getMessage()); |
87 | |
} |
88 | |
|
89 | |
|
90 | 0 | final EndpointPollingJobConfig jobConfig = (EndpointPollingJobConfig) jobDataMap.get(QuartzConnector.PROPERTY_JOB_CONFIG); |
91 | 0 | if (jobConfig == null) |
92 | |
{ |
93 | 0 | throw new JobExecutionException( |
94 | |
QuartzMessages.missingJobDetail(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage()); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
try |
99 | |
{ |
100 | 0 | logger.debug("Attempting to receive event on: " + jobConfig.getEndpointRef()); |
101 | |
TransactionTemplate<Void> tt; |
102 | 0 | final AtomicBoolean pollGlobalEndpoint = new AtomicBoolean(false); |
103 | |
|
104 | |
|
105 | 0 | InboundEndpoint endpoint = muleContext.getRegistry().lookupObject(jobConfig.getEndpointRef() + ".quartz-job"); |
106 | 0 | if(endpoint==null) |
107 | |
{ |
108 | 0 | final EndpointBuilder epBuilder = muleContext.getRegistry().lookupEndpointBuilder(jobConfig.getEndpointRef()); |
109 | 0 | pollGlobalEndpoint.set(epBuilder != null); |
110 | |
|
111 | 0 | if (pollGlobalEndpoint.get()) |
112 | |
{ |
113 | |
|
114 | 0 | endpoint = epBuilder.buildInboundEndpoint(); |
115 | |
|
116 | |
|
117 | 0 | muleContext.getRegistry().registerObject(jobConfig.getEndpointRef() + ".quartz-job", endpoint); |
118 | 0 | tt = new TransactionTemplate<Void>(endpoint.getTransactionConfig(), muleContext); |
119 | |
} |
120 | |
else |
121 | |
{ |
122 | |
|
123 | 0 | tt = new TransactionTemplate<Void>(new MuleTransactionConfig(), muleContext); |
124 | |
} |
125 | 0 | } |
126 | |
else |
127 | |
{ |
128 | 0 | tt = new TransactionTemplate<Void>(endpoint.getTransactionConfig(), muleContext); |
129 | |
} |
130 | |
|
131 | |
|
132 | 0 | final InboundEndpoint finalEndpoint = endpoint; |
133 | 0 | TransactionCallback<Void> cb = new TransactionCallback<Void>() |
134 | 0 | { |
135 | |
public Void doInTransaction() throws Exception |
136 | |
{ |
137 | 0 | Transaction tx = TransactionCoordination.getInstance().getTransaction(); |
138 | 0 | if (tx != null) |
139 | |
{ |
140 | 0 | tx.begin(); |
141 | |
} |
142 | |
|
143 | 0 | MuleMessage result = null; |
144 | 0 | if (pollGlobalEndpoint.get()) |
145 | |
{ |
146 | 0 | result = finalEndpoint.getConnector().request(finalEndpoint, jobConfig.getTimeout()); |
147 | |
} |
148 | |
else |
149 | |
{ |
150 | 0 | MuleClient client = new MuleClient(connector.getMuleContext()); |
151 | 0 | result = client.request(jobConfig.getEndpointRef(), jobConfig.getTimeout()); |
152 | |
} |
153 | |
|
154 | 0 | if (result != null) |
155 | |
{ |
156 | 0 | if (logger.isDebugEnabled()) |
157 | |
{ |
158 | 0 | logger.debug("Received event on: " + jobConfig.getEndpointRef()); |
159 | |
} |
160 | 0 | if (pollGlobalEndpoint.get()) |
161 | |
{ |
162 | 0 | result.applyTransformers(null, finalEndpoint.getTransformers()); |
163 | |
} |
164 | |
|
165 | |
|
166 | 0 | result = (MuleMessage) ((ThreadSafeAccess) result).newThreadCopy(); |
167 | |
|
168 | |
|
169 | 0 | result.addProperties(jobDataMap, PropertyScope.INVOCATION); |
170 | |
|
171 | 0 | receiver.routeMessage(result); |
172 | |
} |
173 | |
|
174 | 0 | return null; |
175 | |
} |
176 | |
}; |
177 | |
|
178 | 0 | tt.execute(cb); |
179 | |
} |
180 | 0 | catch (RuntimeException rex) |
181 | |
{ |
182 | |
|
183 | 0 | throw rex; |
184 | |
} |
185 | 0 | catch (Exception e) |
186 | |
{ |
187 | 0 | throw new JobExecutionException(e); |
188 | 0 | } |
189 | 0 | } |
190 | |
} |