INTSAMPLES-13 Add error-channel to inbound channel adapter and tests

This commit is contained in:
Gary Russell
2010-11-17 21:46:09 -05:00
parent 3714a8a569
commit 12863155aa
5 changed files with 58 additions and 7 deletions

View File

@@ -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.

View File

@@ -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;
}
}

View File

@@ -21,6 +21,6 @@ package org.springframework.integration.samples.tcpclientserver;
*/
public interface SimpleGateway {
public byte[] send(String text);
public String send(String text);
}

View File

@@ -25,10 +25,15 @@
<ip:tcp-outbound-gateway id="outGateway"
request-channel="input"
reply-channel="clientBytes2StringChannel"
connection-factory="client"
request-timeout="10000"
reply-timeout="10000"
/>
<transformer id="clientBytes2String"
input-channel="clientBytes2StringChannel"
expression="new String(payload)"/>
<!-- Server side -->
@@ -38,7 +43,8 @@
<ip:tcp-inbound-gateway id="gatewayCrLf"
connection-factory="crLfServer"
request-channel="toSA" />
request-channel="serverBytes2StringChannel"
error-channel="errorChannel"/>
<channel id="toSA" />
@@ -50,4 +56,13 @@
<beans:bean id="echoService"
class="org.springframework.integration.samples.tcpclientserver.EchoService" />
<transformer id="serverBytes2String"
input-channel="serverBytes2StringChannel"
output-channel="toSA"
expression="new String(payload)"/>
<transformer id="errorHandler"
input-channel="errorChannel"
expression="payload.failedMessage.payload + ':' + payload.cause.message"/>
</beans:beans>

View File

@@ -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);
}
}