diff --git a/spring-integration-core/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java b/spring-integration-core/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java index 46e3099694..0a101d5df8 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/gateway/AbstractMessagingGateway.java @@ -192,14 +192,15 @@ public abstract class AbstractMessagingGateway extends AbstractEndpoint { Throwable error = null; try { reply = this.channelTemplate.sendAndReceive(message, this.requestChannel); + if (reply instanceof ErrorMessage) { + error = ((ErrorMessage) reply).getPayload(); + } } catch (Exception e) { logger.warn("failure occurred in gateway sendAndReceive.", e); error = e; } - if (reply instanceof ErrorMessage) { - error = ((ErrorMessage) reply).getPayload(); - } + if (error != null && this.exceptionMapper != null) { try { // create a reply message from the error diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests-context.xml index e6dbc34122..d1a10e700b 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests-context.xml @@ -2,8 +2,10 @@ + http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd + http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" + xmlns:si="http://www.springframework.org/schema/integration" + xmlns:task="http://www.springframework.org/schema/task"> - + + + + + + + + + + + + + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests.java index a6c8be97c4..c1863cd2d4 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests.java @@ -57,6 +57,16 @@ public class GatewayInvokingMessageHandlerTests { SimpleGateway gatewayWithErrorAndMapper; + @Autowired + @Qualifier("gatewayWithErrorAsync") + SimpleGateway gatewayWithErrorAsync; + + @Autowired + @Qualifier("gatewayWithErrorAsyncAndMapper") + SimpleGateway gatewayWithErrorAsyncAndMapper; + + + @Autowired @Qualifier("inputB") SubscribableChannel output; @@ -118,6 +128,33 @@ public class GatewayInvokingMessageHandlerTests { } } + @Test + public void validateGatewayWithErrorAsync() { + try { + gatewayWithErrorAsync.sendRecieve("echoWithErrorAsyncChannel"); + Assert.fail(); + } catch (Exception e) { + Assert.assertTrue(e instanceof MessageHandlingException); + } + } + + @Test + public void validateGatewayWithErrorAsyncAndMaper() { + try { + gatewayWithErrorAsync.sendRecieve("echoWithErrorAsyncChannel"); + Assert.fail(); + } catch (Exception e) { + Assert.assertTrue(e instanceof MessageHandlingException); + } + + try { + Object result = gatewayWithErrorAsyncAndMapper.sendRecieve("echoWithErrorAsyncChannel"); + Assert.assertEquals("Error happened in message: echoWithErrorAsyncChannel", result); + } catch (Exception e) { + Assert.fail(); + } + } + public static class SampleExceptionMapper implements InboundMessageMapper{ public Message toMessage(Throwable object) throws Exception { @@ -142,6 +179,10 @@ public class GatewayInvokingMessageHandlerTests { public MessageHandlingException echoWithMessagingException(String value) { throw new MessageHandlingException(new StringMessage(value)); } + public RuntimeException echoWithErrorAsync(String value) { + throw new RuntimeException(value); + } + } public static class SampleCheckedException extends Exception {