INTSAMPLES-13 Add error-channel to inbound channel adapter and tests
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,6 +21,6 @@ package org.springframework.integration.samples.tcpclientserver;
|
||||
*/
|
||||
public interface SimpleGateway {
|
||||
|
||||
public byte[] send(String text);
|
||||
public String send(String text);
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user