From 89d2f3ad836a384be5e28954fee5c6c0d9448f07 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 7 Aug 2012 22:23:30 -0400 Subject: [PATCH] INT-2704 Exception Processing in TCP Adapter When using an outbound adapter with a server connection factory (an inbound adapter 'owns' the connections), the outbound adapter simply logged exceptions. There are use cases where flows need to know the exception occurs. Change the adapter to throw a MessagingException. Update the 2.1 to 2.2. Migration guide explaining that the ExpressionEvaluatingRequestHandlerAdvice can be used to restore 2.1 behavior, and trap the exception. --- ...ressionEvaluatingRequestHandlerAdvice.java | 10 ++-- .../ip/tcp/TcpSendingMessageHandler.java | 15 +++-- .../tcp/TcpSendingNoSocketTests-context.xml | 33 ++++++++++ .../ip/tcp/TcpSendingNoSocketTests.java | 60 +++++++++++++++++++ 4 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpSendingNoSocketTests-context.xml create mode 100644 spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpSendingNoSocketTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/advice/ExpressionEvaluatingRequestHandlerAdvice.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/advice/ExpressionEvaluatingRequestHandlerAdvice.java index 9bca05badb..953d86d51b 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/advice/ExpressionEvaluatingRequestHandlerAdvice.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/advice/ExpressionEvaluatingRequestHandlerAdvice.java @@ -134,15 +134,17 @@ public class ExpressionEvaluatingRequestHandlerAdvice extends AbstractRequestHan protected Object doInvoke(ExecutionCallback callback, Object target, Message message) throws Exception { try { Object result = callback.execute(); - if (onSuccessMessageProcessor != null) { + if (this.onSuccessMessageProcessor != null) { evaluateExpression(message, this.onSuccessMessageProcessor, this.successChannel, this.propagateOnSuccessEvaluationFailures); } return result; } catch (Exception e) { - Object evalResult = evaluateExpression(message, this.onFailureMessageProcessor, this.failureChannel, false); - if (this.returnFailureExpressionResult) { - return evalResult; + if (this.onFailureMessageProcessor != null) { + Object evalResult = evaluateExpression(message, this.onFailureMessageProcessor, this.failureChannel, false); + if (this.returnFailureExpressionResult) { + return evalResult; + } } if (!this.trapException) { throw e; diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/TcpSendingMessageHandler.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/TcpSendingMessageHandler.java index 8566b2aab3..25f80bfe9e 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/TcpSendingMessageHandler.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/TcpSendingMessageHandler.java @@ -22,9 +22,7 @@ import java.util.concurrent.ScheduledFuture; import org.springframework.context.SmartLifecycle; import org.springframework.integration.Message; -import org.springframework.integration.MessageDeliveryException; import org.springframework.integration.MessageHandlingException; -import org.springframework.integration.MessageRejectedException; import org.springframework.integration.handler.AbstractMessageHandler; import org.springframework.integration.ip.IpHeaders; import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory; @@ -93,8 +91,9 @@ public class TcpSendingMessageHandler extends AbstractMessageHandler implements * message format. * @see org.springframework.integration.core.MessageHandler#handleMessage(org.springframework.integration.Message) */ - public void handleMessageInternal(final Message message) throws MessageRejectedException, - MessageHandlingException, MessageDeliveryException { + @Override + public void handleMessageInternal(final Message message) throws + MessageHandlingException { if (this.serverConnectionFactory != null) { // We don't own the connection, we are asynchronously replying Object connectionId = message.getHeaders().get(IpHeaders.CONNECTION_ID); @@ -108,9 +107,16 @@ public class TcpSendingMessageHandler extends AbstractMessageHandler implements } catch (Exception e) { logger.error("Error sending message", e); connection.close(); + if (e instanceof MessageHandlingException) { + throw (MessageHandlingException) e; + } + else { + throw new MessageHandlingException(message, "Error sending message", e); + } } } else { logger.error("Unable to find outbound socket for " + message); + throw new MessageHandlingException(message, "Unable to find outbound socket"); } return; } @@ -180,6 +186,7 @@ public class TcpSendingMessageHandler extends AbstractMessageHandler implements connections.remove(connection.getConnectionId()); } + @Override public String getComponentType(){ return "ip:tcp-outbound-channel-adapter"; } diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpSendingNoSocketTests-context.xml b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpSendingNoSocketTests-context.xml new file mode 100644 index 0000000000..05533d8e1c --- /dev/null +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpSendingNoSocketTests-context.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpSendingNoSocketTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpSendingNoSocketTests.java new file mode 100644 index 0000000000..7e92b2e299 --- /dev/null +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/TcpSendingNoSocketTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2002-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.ip.tcp; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.MessageChannel; +import org.springframework.integration.MessageHandlingException; +import org.springframework.integration.message.GenericMessage; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Gary Russell + * @since 2.2 + * + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class TcpSendingNoSocketTests { + + @Autowired + private MessageChannel shouldFail; + + @Autowired + private MessageChannel advised; + + @Test + public void exceptionExpected() { + try { + shouldFail.send(new GenericMessage("foo")); + fail("Exception expected"); + } + catch (MessageHandlingException e) { + assertEquals("Unable to find outbound socket", e.getMessage()); + } + } + + @Test + public void exceptionTrapped() { + advised.send(new GenericMessage("foo")); + } +}