1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.outbound; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.umo.UMOException; |
15 | |
import org.mule.umo.UMOExceptionPayload; |
16 | |
import org.mule.umo.UMOMessage; |
17 | |
import org.mule.umo.UMOSession; |
18 | |
import org.mule.umo.endpoint.UMOEndpoint; |
19 | |
import org.mule.umo.routing.CouldNotRouteOutboundMessageException; |
20 | |
import org.mule.umo.routing.RoutePathNotFoundException; |
21 | |
import org.mule.umo.routing.RoutingException; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 10 | public class ExceptionBasedRouter extends FilteringOutboundRouter |
32 | |
{ |
33 | |
|
34 | |
public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous) |
35 | |
throws RoutingException |
36 | |
{ |
37 | 12 | if (endpoints == null || endpoints.size() == 0) |
38 | |
{ |
39 | 0 | throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), message, null); |
40 | |
} |
41 | |
|
42 | 12 | final int endpointsCount = endpoints.size(); |
43 | |
|
44 | 12 | if (enableCorrelation != ENABLE_CORRELATION_NEVER) |
45 | |
{ |
46 | 12 | boolean correlationSet = message.getCorrelationId() != null; |
47 | 12 | if (correlationSet && (enableCorrelation == ENABLE_CORRELATION_IF_NOT_SET)) |
48 | |
{ |
49 | 0 | logger.debug("CorrelationId is already set, not setting Correlation group size"); |
50 | |
} |
51 | |
else |
52 | |
{ |
53 | |
|
54 | 12 | message.setCorrelationGroupSize(endpointsCount); |
55 | |
} |
56 | |
} |
57 | |
|
58 | 12 | UMOMessage result = null; |
59 | |
|
60 | 12 | UMOEndpoint endpoint = null; |
61 | 12 | boolean success = false; |
62 | |
|
63 | 12 | synchronized (endpoints) |
64 | |
{ |
65 | 22 | for (int i = 0; i < endpointsCount; i++) |
66 | |
{ |
67 | |
|
68 | 20 | endpoint = getEndpoint(i, message); |
69 | 20 | boolean lastEndpoint = (i == endpointsCount - 1); |
70 | |
|
71 | 20 | if (!lastEndpoint) |
72 | |
{ |
73 | 12 | logger.info("Sync mode will be forced for " + endpoint.getEndpointURI() |
74 | |
+ ", as there are more endpoints available."); |
75 | |
} |
76 | |
|
77 | 20 | if (!lastEndpoint || synchronous) |
78 | |
{ |
79 | |
try |
80 | |
{ |
81 | 16 | result = send(session, message, endpoint); |
82 | 10 | if (!exceptionPayloadAvailable(result)) |
83 | |
{ |
84 | 8 | if (logger.isDebugEnabled()) |
85 | |
{ |
86 | 0 | logger.debug("Successful invocation detected, stopping further processing."); |
87 | |
} |
88 | 8 | success = true; |
89 | 8 | break; |
90 | |
} |
91 | |
} |
92 | 6 | catch (UMOException e) |
93 | |
{ |
94 | 6 | logger.info("Failed to send to endpoint: " + endpoint.getEndpointURI().toString() |
95 | |
+ ". Error was: " + e.getMessage() + ". Trying next endpoint"); |
96 | 8 | } |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | |
try |
101 | |
{ |
102 | 4 | dispatch(session, message, endpoint); |
103 | 2 | success = true; |
104 | 2 | break; |
105 | |
} |
106 | 2 | catch (UMOException e) |
107 | |
{ |
108 | 2 | logger.info("Failed to dispatch to endpoint: " + endpoint.getEndpointURI().toString() |
109 | |
+ ". Error was: " + e.getMessage() + ". Trying next endpoint"); |
110 | |
} |
111 | |
} |
112 | |
} |
113 | 12 | } |
114 | |
|
115 | 12 | if (!success) |
116 | |
{ |
117 | 2 | throw new CouldNotRouteOutboundMessageException(message, endpoint); |
118 | |
} |
119 | |
|
120 | 10 | return result; |
121 | |
} |
122 | |
|
123 | |
public void addEndpoint(UMOEndpoint endpoint) |
124 | |
{ |
125 | 22 | if (!endpoint.isRemoteSync()) |
126 | |
{ |
127 | 22 | logger.debug("Endpoint: " |
128 | |
+ endpoint.getEndpointURI() |
129 | |
+ " registered on ExceptionBasedRouter needs to be RemoteSync enabled. Setting this property now."); |
130 | 22 | endpoint.setRemoteSync(true); |
131 | |
} |
132 | 22 | super.addEndpoint(endpoint); |
133 | 22 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
protected boolean exceptionPayloadAvailable(UMOMessage message) |
140 | |
{ |
141 | 10 | if (message == null) |
142 | |
{ |
143 | 2 | return false; |
144 | |
} |
145 | |
|
146 | 8 | final UMOExceptionPayload exceptionPayload = message.getExceptionPayload(); |
147 | 8 | if (exceptionPayload != null) |
148 | |
{ |
149 | 2 | logger.info("Failure returned, will try next endpoint. Exception payload is: " + exceptionPayload); |
150 | 2 | return true; |
151 | |
} |
152 | |
else |
153 | |
{ |
154 | 6 | return false; |
155 | |
} |
156 | |
} |
157 | |
} |