1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.bpm; |
12 | |
|
13 | |
import org.mule.impl.MuleMessage; |
14 | |
import org.mule.providers.AbstractMessageReceiver; |
15 | |
import org.mule.umo.UMOComponent; |
16 | |
import org.mule.umo.UMOException; |
17 | |
import org.mule.umo.UMOMessage; |
18 | |
import org.mule.umo.endpoint.UMOEndpoint; |
19 | |
import org.mule.umo.lifecycle.InitialisationException; |
20 | |
import org.mule.umo.provider.UMOConnector; |
21 | |
|
22 | |
import java.util.Map; |
23 | |
|
24 | |
import javax.resource.spi.work.Work; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class ProcessMessageReceiver extends AbstractMessageReceiver { |
30 | |
|
31 | 2 | private ProcessConnector connector = null; |
32 | |
|
33 | |
public ProcessMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint) |
34 | |
throws InitialisationException { |
35 | 2 | super(connector, component, endpoint); |
36 | 2 | this.connector = (ProcessConnector) connector; |
37 | 2 | } |
38 | |
|
39 | |
public UMOMessage generateSynchronousEvent(String endpoint, Object payload, Map messageProperties) throws UMOException { |
40 | 0 | logger.debug("Executing process is sending an event (synchronously) to Mule endpoint = " + endpoint); |
41 | 0 | UMOMessage response = generateEvent(endpoint, payload, messageProperties, true); |
42 | 0 | if (logger.isDebugEnabled()) |
43 | |
{ |
44 | 0 | logger.debug("Synchronous response is " + (response != null ? response.getPayload() : null )); |
45 | |
} |
46 | 0 | return response; |
47 | |
} |
48 | |
|
49 | |
public void generateAsynchronousEvent(String endpoint, Object payload, Map messageProperties) throws UMOException { |
50 | 0 | logger.debug("Executing process is dispatching an event (asynchronously) to Mule endpoint = " + endpoint); |
51 | |
try { |
52 | 0 | getWorkManager().scheduleWork(new Worker(endpoint, payload, messageProperties)); |
53 | 0 | } catch (Exception e) { |
54 | 0 | handleException(e); |
55 | 0 | } |
56 | 0 | } |
57 | |
|
58 | |
protected UMOMessage generateEvent(String endpoint, Object payload, Map messageProperties, boolean synchronous) throws UMOException { |
59 | |
UMOMessage message; |
60 | 0 | if (payload instanceof UMOMessage) { |
61 | 0 | message = (UMOMessage) payload; |
62 | |
} else { |
63 | 0 | message = new MuleMessage(connector.getMessageAdapter(payload)); |
64 | |
} |
65 | 0 | message.addProperties(messageProperties); |
66 | |
|
67 | 0 | if (connector.isAllowGlobalDispatcher()) { |
68 | |
|
69 | 0 | if (synchronous) { |
70 | 0 | return connector.getMuleClient().send(endpoint, message); |
71 | |
} else { |
72 | 0 | connector.getMuleClient().dispatch(endpoint, message); |
73 | 0 | return null; |
74 | |
} |
75 | |
} |
76 | |
else { |
77 | 0 | message.setStringProperty(ProcessConnector.PROPERTY_ENDPOINT, endpoint); |
78 | 0 | return routeMessage(message, synchronous); |
79 | |
} |
80 | |
} |
81 | |
|
82 | |
private class Worker implements Work { |
83 | |
private String endpoint; |
84 | |
private Object payload; |
85 | |
private Map messageProperties; |
86 | |
|
87 | 0 | public Worker(String endpoint, Object payload, Map messageProperties) { |
88 | 0 | this.endpoint = endpoint; |
89 | 0 | this.payload = payload; |
90 | 0 | this.messageProperties = messageProperties; |
91 | 0 | } |
92 | |
|
93 | |
public void run() { |
94 | |
try { |
95 | 0 | generateEvent(endpoint, payload, messageProperties, false); |
96 | 0 | } catch (Exception e) { |
97 | 0 | getConnector().handleException(e); |
98 | 0 | } |
99 | 0 | } |
100 | |
|
101 | 0 | public void release() { } |
102 | |
} |
103 | |
|
104 | |
protected void doConnect() throws Exception |
105 | |
{ |
106 | |
|
107 | 0 | } |
108 | |
|
109 | |
protected void doDisconnect() throws Exception |
110 | |
{ |
111 | |
|
112 | 0 | } |
113 | |
|
114 | |
protected void doStart() throws UMOException |
115 | |
{ |
116 | |
|
117 | 0 | } |
118 | |
|
119 | |
protected void doStop() throws UMOException |
120 | |
{ |
121 | |
|
122 | 0 | } |
123 | |
|
124 | |
protected void doDispose() |
125 | |
{ |
126 | |
|
127 | 2 | } |
128 | |
|
129 | |
} |