diff --git a/intermediate/tcp-client-server-multiplex/README.md b/intermediate/tcp-client-server-multiplex/README.md index 96af3f97..35d56291 100644 --- a/intermediate/tcp-client-server-multiplex/README.md +++ b/intermediate/tcp-client-server-multiplex/README.md @@ -26,7 +26,7 @@ on the socket. This sample now shows how to use the `group-timeout` on the aggregator to release the group under this condition. Further, it routes the discarded message to a service activator which return a `MessagingTimeoutException` which -is routed to the waiting thread. +is routed to the waiting thread and thrown to the caller. ```` gateway -> outbound-channel-adapter @@ -38,6 +38,12 @@ aggregator(group-timeout discard)->service-activator A service activator is used here instead of a transformer because you may wish to take some other action when the timeout condition occurs. -Normal gateway processing detects that the payload is an `Exception` and throws it to the caller. +There are two test cases, one throws an exception; the other returns one as the message payload. -Thus, this shows how to return an exception to a gateway caller, even when the messaging is entirely asynchronous. +When the payload of the reply messsage is a `Throwable` normal gateway processing detects that and throws it to the +caller. + +Similarly, when the async flow throws an exception, it is wrapped in an `ErrorMessage` and routed to the caller. + +Thus, this shows both techniques for returning an exception to a gateway caller, even when the messaging is entirely +asynchronous. diff --git a/intermediate/tcp-client-server-multiplex/src/main/java/org/springframework/integration/samples/tcpclientserver/EchoService.java b/intermediate/tcp-client-server-multiplex/src/main/java/org/springframework/integration/samples/tcpclientserver/EchoService.java index e4771b1e..785d8ee8 100644 --- a/intermediate/tcp-client-server-multiplex/src/main/java/org/springframework/integration/samples/tcpclientserver/EchoService.java +++ b/intermediate/tcp-client-server-multiplex/src/main/java/org/springframework/integration/samples/tcpclientserver/EchoService.java @@ -31,7 +31,7 @@ public class EchoService { if ("FAIL".equals(input)) { throw new RuntimeException("Failure Demonstration"); } - else if("TIMEOUT_TEST".equals(input)){ + else if(input.startsWith("TIMEOUT_TEST")) { Thread.sleep(3000); } @@ -39,7 +39,12 @@ public class EchoService { } public MessageTimeoutException noResponse(String input) { - return new MessageTimeoutException("No response received for " + input); + if ("TIMEOUT_TEST_THROW".equals(input)) { + throw new MessageTimeoutException("No response received for " + input); + } + else { + return new MessageTimeoutException("No response received for " + input); + } } } diff --git a/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java b/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java index 9a6f4752..bf9ebe73 100644 --- a/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java +++ b/intermediate/tcp-client-server-multiplex/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java @@ -33,10 +33,10 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.task.SimpleAsyncTaskExecutor; import org.springframework.core.task.TaskExecutor; -import org.springframework.integration.MessageTimeoutException; import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory; import org.springframework.integration.ip.util.TestingUtilities; import org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader; +import org.springframework.messaging.MessagingException; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -97,12 +97,23 @@ public class TcpClientServerDemoTest { } @Test - public void testTimeout() { + public void testTimeoutThrow() { try { - gw.send("TIMEOUT_TEST"); + gw.send("TIMEOUT_TEST_THROW"); fail("expected exception"); } - catch (MessageTimeoutException e) { + catch (MessagingException e) { + assertThat(e.getMessage(), containsString("No response received for TIMEOUT_TEST")); + } + } + + @Test + public void testTimeoutReturn() { + try { + gw.send("TIMEOUT_TEST_RETURN"); + fail("expected exception"); + } + catch (MessagingException e) { assertThat(e.getMessage(), containsString("No response received for TIMEOUT_TEST")); } }