Merge pull request #18 from garyrussell/INTSAMPLES-37

INTSAMPLES-37 Add ConversionService to TCP Sample
This commit is contained in:
Gunnar Hillert
2011-12-14 08:52:16 -08:00
4 changed files with 144 additions and 3 deletions

View File

@@ -7,6 +7,10 @@ and the result is returned to the client that invoked the original SimpleGateway
To run sample simply execute a test case in org.springframework.integration.samples.tcpclientservice package.
Note that the test case includes an alternative configuration that uses the in-built conversion service
and the channel dataType attribute, instead of explicit transformers, to convert from byte arrays to Strings.
Simply change the @ContextConfiguration to switch between the two techniques.
In addition, a simple telnet server is provided; see TelnetServer in src/main/java. Run this class as a
java application and then use telnet to connect to the service ('telnet localhost 11111').

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2002-2011 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.tcpclientserver;
import java.io.UnsupportedEncodingException;
import org.springframework.core.convert.converter.Converter;
/**
* Simple byte array to String converter; allowing the character set
* to be specified.
*
* @author Gary Russell
* @since 2.1
*
*/
public class ByteArrayToStringConverter implements Converter<byte[], String> {
private String charSet = "UTF-8";
public String convert(byte[] bytes) {
try {
return new String(bytes, this.charSet);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return new String(bytes);
}
}
/**
* @return the charSet
*/
public String getCharSet() {
return charSet;
}
/**
* @param charSet the charSet to set
*/
public void setCharSet(String charSet) {
this.charSet = charSet;
}
}

View File

@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/integration"
xmlns:ip="http://www.springframework.org/schema/integration/ip"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">
<beans:description>
This version demonstrates the use of a conversion service and channel 'dataType'
instead of explicit transformers to convert from byte array to String.
</beans:description>
<converter>
<beans:bean class="org.springframework.integration.samples.tcpclientserver.ByteArrayToStringConverter" />
</converter>
<!-- Client side -->
<gateway id="gw"
service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
default-request-channel="input"
default-reply-channel="reply"/>
<ip:tcp-connection-factory id="client"
type="client"
host="localhost"
port="11111"
single-use="true"
so-timeout="10000"
/>
<channel id="input" />
<ip:tcp-outbound-gateway id="outGateway"
request-channel="input"
reply-channel="reply"
connection-factory="client"
request-timeout="10000"
reply-timeout="10000"
/>
<!-- dataType attribute invokes the conversion service -->
<channel id="reply" datatype="java.lang.String" />
<!-- Server side -->
<ip:tcp-connection-factory id="crLfServer"
type="server"
port="11111"/>
<ip:tcp-inbound-gateway id="gatewayCrLf"
connection-factory="crLfServer"
request-channel="toSA"
error-channel="errorChannel"/>
<!-- dataType attribute invokes the conversion service -->
<channel id="toSA" datatype="java.lang.String" />
<service-activator input-channel="toSA"
ref="echoService"
method="test"
/>
<beans:bean id="echoService"
class="org.springframework.integration.samples.tcpclientserver.EchoService" />
<transformer id="errorHandler"
input-channel="errorChannel"
expression="payload.failedMessage.payload + ':' + payload.cause.message"/>
</beans:beans>

View File

@@ -30,24 +30,31 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* to the inbound gateway. In turn the inbound gateway sends the message to an
* echo service and the echoed response comes back over tcp and is returned to
* the test case for verification.
*
*
* The alternate configuration shows how the conversion service can be used
* instead of explicit transformers to convert the byte array payloads to
* Strings.
*
* @author Gary Russell
*
*/
// This one uses transformers
@ContextConfiguration("/META-INF/spring/integration/tcpClientServerDemo-context.xml")
// This one uses the conversion service
//@ContextConfiguration("/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class TcpClientServerDemoTest {
@Autowired
SimpleGateway gw;
@Test
public void testHappyDay() {
String result = gw.send("Hello world!");
System.out.println(result);
assertEquals("echo:Hello world!", result);
}
@Test
public void testZeroLength() {
String result = gw.send("");