From 12863155aae326f6d48ebb6758941518a4a2f674 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Wed, 17 Nov 2010 21:46:09 -0500 Subject: [PATCH] INTSAMPLES-13 Add error-channel to inbound channel adapter and tests --- basic/tcp-client-server/readme.txt | 26 +++++++++++++++++++ .../samples/tcpclientserver/EchoService.java | 7 +++-- .../tcpclientserver/SimpleGateway.java | 2 +- .../tcpClientServerDemo-context.xml | 17 +++++++++++- .../TcpClientServerDemoTest.java | 13 +++++++--- 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/basic/tcp-client-server/readme.txt b/basic/tcp-client-server/readme.txt index 3296d5f5..3567dd93 100644 --- a/basic/tcp-client-server/readme.txt +++ b/basic/tcp-client-server/readme.txt @@ -26,3 +26,29 @@ echo:Test telnet> quit Connection closed. + + +Note that the test case also demonstrates error handling on an inbound gateway using direct channels. +If the payload is 'FAIL', the EchoService throws an exception. The gateway is configured +with an error-channel attribute. Messages sent to that channel are consumed by a transformer +that concatenates the inbound message payload with the message text from the thrown +exception, returning 'FAIL:Failure Demonstration' over the TCP socket. + +This can also be demonstrated with the telnet client thus... + +$ telnet localhost 11111 +Trying 127.0.0.1... +Connected to localhost. +Escape character is '^]'. +Hello world! +echo:Hello world! +FAIL +FAIL:Failure Demonstration +Hello +echo:Hello +^] + +telnet> quit +Connection closed. + + diff --git a/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/EchoService.java b/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/EchoService.java index 8cf27748..e0015dc9 100644 --- a/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/EchoService.java +++ b/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/EchoService.java @@ -24,8 +24,11 @@ package org.springframework.integration.samples.tcpclientserver; */ public class EchoService { - public String test(byte[] bytes) { - return "echo:" + new String(bytes); + public String test(String input) { + if ("FAIL".equals(input)) { + throw new RuntimeException("Failure Demonstration"); + } + return "echo:" + input; } } \ No newline at end of file diff --git a/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/SimpleGateway.java b/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/SimpleGateway.java index e5ee0b47..df7d1b51 100644 --- a/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/SimpleGateway.java +++ b/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/SimpleGateway.java @@ -21,6 +21,6 @@ package org.springframework.integration.samples.tcpclientserver; */ public interface SimpleGateway { - public byte[] send(String text); + public String send(String text); } \ No newline at end of file diff --git a/basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-context.xml b/basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-context.xml index dc2abdf5..e4d865c9 100644 --- a/basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-context.xml +++ b/basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-context.xml @@ -25,10 +25,15 @@ + + @@ -38,7 +43,8 @@ + request-channel="serverBytes2StringChannel" + error-channel="errorChannel"/> @@ -50,4 +56,13 @@ + + + + diff --git a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java index 504441cf..d4c645e7 100644 --- a/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java +++ b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java @@ -42,10 +42,17 @@ public class TcpClientServerDemoTest { SimpleGateway gw; @Test - public void test() { - byte[] echo = gw.send("Hello world!"); - String result = new String(echo); + public void testHappyDay() { + String result = gw.send("Hello world!"); System.out.println(result); assertEquals("echo:Hello world!", result); } + + @Test + public void testFail() { + String result = gw.send("FAIL"); + System.out.println(result); + assertEquals("FAIL:Failure Demonstration", result); + } + }