1 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.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 27 28 public class ChainingRouter extends FilteringOutboundRouter 29 { 30 31 public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous) 32 throws RoutingException 33 { 34 UMOMessage resultToReturn = null; 35 if (endpoints == null || endpoints.size() == 0) 36 { 37 throw new RoutePathNotFoundException(new Message(Messages.NO_ENDPOINTS_FOR_ROUTER), message, null); 38 } 39 40 final int endpointsCount = endpoints.size(); 41 if (logger.isDebugEnabled()) 42 { 43 logger.debug("About to chain " + endpointsCount + " endpoints."); 44 } 45 46 UMOEndpoint endpoint = null; 48 try 49 { 50 UMOMessage intermediaryResult = message; 51 52 for (int i = 0; i < endpointsCount; i++) 53 { 54 endpoint = getEndpoint(i, intermediaryResult); 55 boolean lastEndpointInChain = (i == endpointsCount - 1); 58 59 if (logger.isDebugEnabled()) 60 { 61 logger.debug("Sending Chained message '" + i + "': " 62 + (intermediaryResult == null ? "null" : intermediaryResult.toString())); 63 } 64 65 if (!lastEndpointInChain) 66 { 67 UMOMessage localResult = send(session, intermediaryResult, endpoint); 68 if (localResult != null && intermediaryResult != null) 72 { 73 processIntermediaryResult(localResult, intermediaryResult); 74 } 75 intermediaryResult = localResult; 76 77 if (logger.isDebugEnabled()) 78 { 79 logger.debug("Received Chain result '" + i + "': " 80 + (intermediaryResult != null ? intermediaryResult.toString() : "null")); 81 } 82 83 if (intermediaryResult == null) 84 { 85 logger.warn("Chaining router cannot process any further endpoints. " 86 + "There was no result returned from endpoint invocation: " + endpoint); 87 break; 88 } 89 } 90 else 91 { 92 if (synchronous) 95 { 96 resultToReturn = send(session, intermediaryResult, endpoint); 97 if (logger.isDebugEnabled()) 98 { 99 logger.debug("Received final Chain result '" + i + "': " 100 + (resultToReturn == null ? "null" : resultToReturn.toString())); 101 } 102 } 103 else 104 { 105 resultToReturn = null; 107 dispatch(session, intermediaryResult, endpoint); 108 } 109 } 110 } 111 112 } 113 catch (UMOException e) 114 { 115 throw new CouldNotRouteOutboundMessageException(message, endpoint, e); 116 } 117 return resultToReturn; 118 } 119 120 public void addEndpoint(UMOEndpoint endpoint) 121 { 122 if (!endpoint.isRemoteSync()) 123 { 124 logger.debug("Endpoint: " 125 + endpoint.getEndpointURI() 126 + " registered on chaining router needs to be RemoteSync enabled. Setting this property now"); 127 endpoint.setRemoteSync(true); 128 } 129 super.addEndpoint(endpoint); 130 } 131 132 152 protected void processIntermediaryResult(UMOMessage localResult, UMOMessage intermediaryResult) 153 { 154 localResult.setCorrelationId(intermediaryResult.getCorrelationId()); 155 localResult.setCorrelationSequence(intermediaryResult.getCorrelationSequence()); 156 localResult.setCorrelationGroupSize(intermediaryResult.getCorrelationGroupSize()); 157 localResult.setReplyTo(intermediaryResult.getReplyTo()); 158 } 159 } 160 | Popular Tags |