From 8fe5d44d038135c41556216e2381acb308a58cea Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Thu, 6 Jun 2013 13:07:38 -0400 Subject: [PATCH] INT-3031 Fix RMI Remote Failed Message When a remote flow throws a `MessagingException`, it is unwrapped and the local `failedMessage` property is the remote failed message instead of the message that arrived at the outbound gateway. Detect a remote MessagingException and wrapt it in a new MessagingException so the `failedMessage` property is the original message and the `failedMessage` on the cause is the remote failed message. --- .../integration/rmi/RmiOutboundGateway.java | 9 ++++-- .../rmi/RmiOutboundGatewayTests.java | 30 +++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/spring-integration-rmi/src/main/java/org/springframework/integration/rmi/RmiOutboundGateway.java b/spring-integration-rmi/src/main/java/org/springframework/integration/rmi/RmiOutboundGateway.java index c6c4eafcdf..49b0422c59 100644 --- a/spring-integration-rmi/src/main/java/org/springframework/integration/rmi/RmiOutboundGateway.java +++ b/spring-integration-rmi/src/main/java/org/springframework/integration/rmi/RmiOutboundGateway.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -21,6 +21,7 @@ import java.io.Serializable; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; import org.springframework.integration.MessageHandlingException; +import org.springframework.integration.MessagingException; import org.springframework.integration.gateway.RequestReplyExchanger; import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.integration.support.MessageBuilder; @@ -29,8 +30,9 @@ import org.springframework.remoting.rmi.RmiProxyFactoryBean; /** * An outbound Messaging Gateway for RMI-based remoting. - * + * * @author Mark Fisher + * @author Gary Russell */ public class RmiOutboundGateway extends AbstractReplyProducingMessageHandler { @@ -62,6 +64,9 @@ public class RmiOutboundGateway extends AbstractReplyProducingMessageHandler { } return reply; } + catch (MessagingException e) { + throw new MessageHandlingException(message, e); + } catch (RemoteAccessException e) { throw new MessageHandlingException(message, "remote failure in RmiOutboundGateway", e); } diff --git a/spring-integration-rmi/src/test/java/org/springframework/integration/rmi/RmiOutboundGatewayTests.java b/spring-integration-rmi/src/test/java/org/springframework/integration/rmi/RmiOutboundGatewayTests.java index 26b5979c03..af3aaa0275 100644 --- a/spring-integration-rmi/src/test/java/org/springframework/integration/rmi/RmiOutboundGatewayTests.java +++ b/spring-integration-rmi/src/test/java/org/springframework/integration/rmi/RmiOutboundGatewayTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2013 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. @@ -18,7 +18,9 @@ package org.springframework.integration.rmi; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.rmi.RemoteException; @@ -27,8 +29,10 @@ import org.junit.Test; import org.springframework.integration.Message; import org.springframework.integration.MessageHandlingException; +import org.springframework.integration.MessagingException; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.gateway.RequestReplyExchanger; +import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.support.MessageBuilder; import org.springframework.remoting.RemoteLookupFailureException; @@ -36,6 +40,7 @@ import org.springframework.remoting.rmi.RmiServiceExporter; /** * @author Mark Fisher + * @author Gary Russell */ public class RmiOutboundGatewayTests { @@ -43,7 +48,6 @@ public class RmiOutboundGatewayTests { private final QueueChannel output = new QueueChannel(1); - @Before public void initializeGateway() { this.gateway.setOutputChannel(this.output); @@ -67,6 +71,19 @@ public class RmiOutboundGatewayTests { assertEquals("TEST", replyMessage.getPayload()); } + @Test + public void failedMessage() throws RemoteException { + GenericMessage message = new GenericMessage("fail"); + try { + gateway.handleMessage(message); + fail("Exception expected"); + } + catch (MessagingException e) { + assertSame(message, e.getFailedMessage()); + assertEquals("bar", ((MessagingException) e.getCause()).getFailedMessage().getPayload()); + } + } + @Test public void serializableAttribute() throws RemoteException { Message requestMessage = MessageBuilder.withPayload("test") @@ -140,6 +157,15 @@ public class RmiOutboundGatewayTests { private static class TestExchanger implements RequestReplyExchanger { public Message exchange(Message message) { + if (message.getPayload().equals("fail")) { + new AbstractReplyProducingMessageHandler() { + + @Override + protected Object handleRequestMessage(Message requestMessage) { + throw new RuntimeException("foo"); + } + }.handleMessage(new GenericMessage("bar")); + } return new GenericMessage(message.getPayload().toString().toUpperCase(), message.getHeaders()); } }