KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > outbound > ExceptionBasedRouter


1 /*
2  * $Id: ExceptionBasedRouter.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.routing.outbound;
12
13 import org.mule.config.i18n.Message;
14 import org.mule.config.i18n.Messages;
15 import org.mule.umo.UMOException;
16 import org.mule.umo.UMOExceptionPayload;
17 import org.mule.umo.UMOMessage;
18 import org.mule.umo.UMOSession;
19 import org.mule.umo.endpoint.UMOEndpoint;
20 import org.mule.umo.routing.CouldNotRouteOutboundMessageException;
21 import org.mule.umo.routing.RoutePathNotFoundException;
22 import org.mule.umo.routing.RoutingException;
23
24 /**
25  * <code>ExceptionBasedRouter</code> Will send the current event to the first
26  * endpoint that doesn't throw an exception. If all attempted endpoints fail then an
27  * exception is thrown. <p/> The router will override the sync/async mode of the
28  * endpoint and force the sync mode for all endpoints except the last one.
29  * <code>remoteSync</code> is also enforced.
30  */

31
32 public class ExceptionBasedRouter extends FilteringOutboundRouter
33 {
34
35     public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
36         throws RoutingException
37     {
38         if (endpoints == null || endpoints.size() == 0)
39         {
40             throw new RoutePathNotFoundException(new Message(Messages.NO_ENDPOINTS_FOR_ROUTER), message, null);
41         }
42
43         final int endpointsCount = endpoints.size();
44
45         if (enableCorrelation != ENABLE_CORRELATION_NEVER)
46         {
47             boolean correlationSet = message.getCorrelationId() != null;
48             if (correlationSet && (enableCorrelation == ENABLE_CORRELATION_IF_NOT_SET))
49             {
50                 logger.debug("CorrelationId is already set, not setting Correlation group size");
51             }
52             else
53             {
54                 // the correlationId will be set by the AbstractOutboundRouter
55
message.setCorrelationGroupSize(endpointsCount);
56             }
57         }
58
59         UMOMessage result = null;
60         // need that ref for an error message
61
UMOEndpoint endpoint = null;
62         boolean success = false;
63
64         synchronized (endpoints)
65         {
66             for (int i = 0; i < endpointsCount; i++)
67             {
68                 // apply endpoint URI templates if any
69
endpoint = getEndpoint(i, message);
70                 boolean lastEndpoint = (i == endpointsCount - 1);
71
72                 if (!lastEndpoint)
73                 {
74                     logger.info("Sync mode will be forced for " + endpoint.getEndpointURI()
75                                 + ", as there are more endpoints available.");
76                 }
77
78                 if (!lastEndpoint || synchronous)
79                 {
80                     try
81                     {
82                         result = send(session, message, endpoint);
83                         if (!exceptionPayloadAvailable(result))
84                         {
85                             if (logger.isDebugEnabled())
86                             {
87                                 logger.debug("Successful invocation detected, stopping further processing.");
88                             }
89                             success = true;
90                             break;
91                         }
92                     }
93                     catch (UMOException e)
94                     {
95                         logger.info("Failed to send to endpoint: " + endpoint.getEndpointURI().toString()
96                                     + ". Error was: " + e.getMessage() + ". Trying next endpoint");
97                     }
98                 }
99                 else
100                 {
101                     try
102                     {
103                         dispatch(session, message, endpoint);
104                         success = true;
105                         break;
106                     }
107                     catch (UMOException e)
108                     {
109                         logger.info("Failed to dispatch to endpoint: " + endpoint.getEndpointURI().toString()
110                                     + ". Error was: " + e.getMessage() + ". Trying next endpoint");
111                     }
112                 }
113             }
114         }
115
116         if (!success)
117         {
118             throw new CouldNotRouteOutboundMessageException(message, endpoint);
119         }
120
121         return result;
122     }
123
124     public void addEndpoint(UMOEndpoint endpoint)
125     {
126         if (!endpoint.isRemoteSync())
127         {
128             logger.debug("Endpoint: "
129                          + endpoint.getEndpointURI()
130                          + " registered on ExceptionBasedRouter needs to be RemoteSync enabled. Setting this property now.");
131             endpoint.setRemoteSync(true);
132         }
133         super.addEndpoint(endpoint);
134     }
135
136     /**
137      * @param message message to check
138      * @return true if there was an exception payload set
139      */

140     protected boolean exceptionPayloadAvailable(UMOMessage message)
141     {
142         if (message == null)
143         {
144             return false;
145         }
146
147         final UMOExceptionPayload exceptionPayload = message.getExceptionPayload();
148         if (exceptionPayload != null)
149         {
150             logger.info("Failure returned, will try next endpoint. Exception payload is: " + exceptionPayload);
151             return true;
152         }
153         else
154         {
155             return false;
156         }
157     }
158 }
159
Popular Tags