Merge pull request #52 from ghillert/INTSAMPLES-76
This commit is contained in:
@@ -15,7 +15,12 @@
|
||||
*/
|
||||
package org.springframework.integration.samples.tcpclientserver;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
|
||||
/**
|
||||
* The configured inbound gateway uses CRLF delimited messages which means
|
||||
@@ -23,15 +28,31 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
* telnet localhost 11111 - each time you hit enter you should see your input
|
||||
* echoed back, preceded by 'echo:'.
|
||||
*
|
||||
* Alternatively, you can also customize the port by providing an additional system
|
||||
* property at startup e.g. <i>-DavailableServerSocket=7777</i>
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @author Gunnar Hillert
|
||||
*
|
||||
*/
|
||||
public class TelnetServer {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new ClassPathXmlApplicationContext("/META-INF/spring/integration/tcpClientServerDemo-context.xml");
|
||||
|
||||
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"/META-INF/spring/integration/tcpClientServerDemo-context.xml"}, false);
|
||||
|
||||
final Map<String, Object> sockets = new HashMap<String, Object>();
|
||||
sockets.put("availableServerSocket", 11111);
|
||||
|
||||
final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);
|
||||
|
||||
context.getEnvironment().getPropertySources().addLast(propertySource);
|
||||
context.refresh();
|
||||
|
||||
System.out.println("Use telnet and connect to port: " + context.getEnvironment().getProperty("availableServerSocket"));
|
||||
System.out.println("Press Enter/Return in the console to exit");
|
||||
System.in.read();
|
||||
System.out.println("exiting application...bye.\n\n");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.support;
|
||||
|
||||
import java.net.ServerSocket;
|
||||
|
||||
import javax.net.ServerSocketFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
*
|
||||
*/
|
||||
public final class SocketUtils {
|
||||
|
||||
public static int findAvailableServerSocket(int seed) {
|
||||
for (int i = seed; i < seed+200; i++) {
|
||||
try {
|
||||
ServerSocket sock = ServerSocketFactory.getDefault().createServerSocket(i);
|
||||
sock.close();
|
||||
return i;
|
||||
} catch (Exception e) { }
|
||||
}
|
||||
throw new RuntimeException("Cannot find a free server socket");
|
||||
}
|
||||
|
||||
public static int findAvailableServerSocket() {
|
||||
return findAvailableServerSocket(5678);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,68 +1,69 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
<beans xmlns="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"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">
|
||||
|
||||
<!-- Client side -->
|
||||
|
||||
<gateway id="gw"
|
||||
service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
|
||||
default-request-channel="input"/>
|
||||
<context:property-placeholder />
|
||||
|
||||
<ip:tcp-connection-factory id="client"
|
||||
<!-- Client side -->
|
||||
|
||||
<int:gateway id="gw"
|
||||
service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
|
||||
default-request-channel="input"/>
|
||||
|
||||
<int-ip:tcp-connection-factory id="client"
|
||||
type="client"
|
||||
host="localhost"
|
||||
port="11111"
|
||||
port="${availableServerSocket}"
|
||||
single-use="true"
|
||||
so-timeout="10000"
|
||||
/>
|
||||
|
||||
<channel id="input" />
|
||||
|
||||
<ip:tcp-outbound-gateway id="outGateway"
|
||||
so-timeout="10000"/>
|
||||
|
||||
<int:channel id="input" />
|
||||
|
||||
<int-ip:tcp-outbound-gateway id="outGateway"
|
||||
request-channel="input"
|
||||
reply-channel="clientBytes2StringChannel"
|
||||
connection-factory="client"
|
||||
request-timeout="10000"
|
||||
reply-timeout="10000"
|
||||
/>
|
||||
reply-timeout="10000"/>
|
||||
|
||||
<transformer id="clientBytes2String"
|
||||
<int:transformer id="clientBytes2String"
|
||||
input-channel="clientBytes2StringChannel"
|
||||
expression="new String(payload)"/>
|
||||
|
||||
|
||||
<!-- Server side -->
|
||||
|
||||
<ip:tcp-connection-factory id="crLfServer"
|
||||
|
||||
<int-ip:tcp-connection-factory id="crLfServer"
|
||||
type="server"
|
||||
port="11111"/>
|
||||
|
||||
<ip:tcp-inbound-gateway id="gatewayCrLf"
|
||||
port="${availableServerSocket}"/>
|
||||
|
||||
<int-ip:tcp-inbound-gateway id="gatewayCrLf"
|
||||
connection-factory="crLfServer"
|
||||
request-channel="serverBytes2StringChannel"
|
||||
request-channel="serverBytes2StringChannel"
|
||||
error-channel="errorChannel"/>
|
||||
|
||||
<channel id="toSA" />
|
||||
|
||||
<service-activator input-channel="toSA"
|
||||
ref="echoService"
|
||||
method="test"
|
||||
/>
|
||||
<int:channel id="toSA" />
|
||||
|
||||
<beans:bean id="echoService"
|
||||
class="org.springframework.integration.samples.tcpclientserver.EchoService" />
|
||||
|
||||
<transformer id="serverBytes2String"
|
||||
<int:service-activator input-channel="toSA"
|
||||
ref="echoService"
|
||||
method="test"/>
|
||||
|
||||
<bean id="echoService"
|
||||
class="org.springframework.integration.samples.tcpclientserver.EchoService" />
|
||||
|
||||
<int:transformer id="serverBytes2String"
|
||||
input-channel="serverBytes2StringChannel"
|
||||
output-channel="toSA"
|
||||
output-channel="toSA"
|
||||
expression="new String(payload)"/>
|
||||
|
||||
<transformer id="errorHandler"
|
||||
|
||||
<int:transformer id="errorHandler"
|
||||
input-channel="errorChannel"
|
||||
expression="payload.failedMessage.payload + ':' + payload.cause.message"/>
|
||||
|
||||
</beans:beans>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -1,39 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
<beans xmlns="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"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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>
|
||||
<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>
|
||||
</description>
|
||||
|
||||
<converter>
|
||||
<beans:bean class="org.springframework.integration.samples.tcpclientserver.ByteArrayToStringConverter" />
|
||||
</converter>
|
||||
<bean id="integrationConversionService"
|
||||
class="org.springframework.context.support.ConversionServiceFactoryBean">
|
||||
<property name="converters">
|
||||
<list>
|
||||
<bean class="org.springframework.integration.samples.tcpclientserver.ByteArrayToStringConverter" />
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<context:property-placeholder />
|
||||
|
||||
<!-- Client side -->
|
||||
|
||||
<gateway id="gw"
|
||||
service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
|
||||
default-request-channel="input"
|
||||
default-reply-channel="reply"/>
|
||||
<int: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"
|
||||
<int-ip:tcp-connection-factory id="client"
|
||||
type="client"
|
||||
host="localhost"
|
||||
port="11111"
|
||||
port="${availableServerSocket}"
|
||||
single-use="true"
|
||||
so-timeout="10000"
|
||||
/>
|
||||
|
||||
<channel id="input" />
|
||||
<int:channel id="input" />
|
||||
|
||||
<ip:tcp-outbound-gateway id="outGateway"
|
||||
<int-ip:tcp-outbound-gateway id="outGateway"
|
||||
request-channel="input"
|
||||
reply-channel="reply"
|
||||
connection-factory="client"
|
||||
@@ -42,32 +51,33 @@
|
||||
/>
|
||||
|
||||
<!-- dataType attribute invokes the conversion service -->
|
||||
<channel id="reply" datatype="java.lang.String" />
|
||||
<int:channel id="reply" datatype="java.lang.String" />
|
||||
|
||||
<!-- Server side -->
|
||||
|
||||
<ip:tcp-connection-factory id="crLfServer"
|
||||
<int-ip:tcp-connection-factory id="crLfServer"
|
||||
type="server"
|
||||
port="11111"/>
|
||||
port="${availableServerSocket}"/>
|
||||
|
||||
<ip:tcp-inbound-gateway id="gatewayCrLf"
|
||||
<int-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" />
|
||||
<int:channel id="toSA" datatype="java.lang.String" />
|
||||
|
||||
<service-activator input-channel="toSA"
|
||||
ref="echoService"
|
||||
method="test"
|
||||
<int:service-activator input-channel="toSA"
|
||||
ref="echoService"
|
||||
method="test"
|
||||
/>
|
||||
|
||||
<beans:bean id="echoService"
|
||||
<bean id="echoService"
|
||||
class="org.springframework.integration.samples.tcpclientserver.EchoService" />
|
||||
|
||||
<transformer id="errorHandler"
|
||||
<int:transformer id="errorHandler"
|
||||
input-channel="errorChannel"
|
||||
expression="payload.failedMessage.payload + ':' + payload.cause.message"/>
|
||||
|
||||
</beans:beans>
|
||||
</beans>
|
||||
|
||||
|
||||
@@ -1,27 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-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-2.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">
|
||||
|
||||
<!-- Client side -->
|
||||
<context:property-placeholder />
|
||||
|
||||
<!-- Client side -->
|
||||
<int:gateway id="gw"
|
||||
service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
|
||||
default-request-channel="input"/>
|
||||
service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
|
||||
default-request-channel="input"/>
|
||||
|
||||
<!-- Create a connection for the client gateway that uses the same Stx-Etx deserializer to turn the stream
|
||||
into the appropriate content (it looks for the Stx byte, extracts anything between it and the Etx byte). We
|
||||
don't specify the serializer (although we could) because the unit test explicitly shows how the content
|
||||
to be sent is wrapped by the Stx and Etx bytes. -->
|
||||
<!-- Create a connection for the client gateway that uses the same Stx-Etx deserializer to turn the stream
|
||||
into the appropriate content (it looks for the Stx byte, extracts anything between it and the Etx byte). We
|
||||
don't specify the serializer (although we could) because the unit test explicitly shows how the content
|
||||
to be sent is wrapped by the Stx and Etx bytes. -->
|
||||
<int-ip:tcp-connection-factory id="client"
|
||||
type="client"
|
||||
host="localhost"
|
||||
port="22222"
|
||||
port="${availableServerSocket}"
|
||||
single-use="true"
|
||||
so-timeout="10000"
|
||||
deserializer="connectionSerializeDeserialize"
|
||||
/>
|
||||
deserializer="connectionSerializeDeserialize"/>
|
||||
|
||||
<int:channel id="input" />
|
||||
|
||||
@@ -30,27 +35,26 @@
|
||||
reply-channel="clientBytes2StringChannel"
|
||||
connection-factory="client"
|
||||
request-timeout="10000"
|
||||
reply-timeout="10000"
|
||||
/>
|
||||
reply-timeout="10000"/>
|
||||
|
||||
<int:channel id="clientBytes2StringChannel"/>
|
||||
<int:channel id="clientBytes2StringChannel"/>
|
||||
|
||||
<int:transformer id="clientBytes2String"
|
||||
<int:transformer id="clientBytes2String"
|
||||
input-channel="clientBytes2StringChannel"
|
||||
expression="new String(payload)"/>
|
||||
|
||||
<!-- Server side -->
|
||||
<!-- When creating the socket factory on the server side, we specify both the serializer and deserializer
|
||||
which deals with both accepting a stream formatted with the Stx-Etx bytes as well as sending a stream
|
||||
formatted with the Stx-Etx bytes. -->
|
||||
<!-- Server side -->
|
||||
<!-- When creating the socket factory on the server side, we specify both the serializer and deserializer
|
||||
which deals with both accepting a stream formatted with the Stx-Etx bytes as well as sending a stream
|
||||
formatted with the Stx-Etx bytes. -->
|
||||
<int-ip:tcp-connection-factory id="serverConnectionFactory"
|
||||
type="server"
|
||||
port="22222"
|
||||
serializer="connectionSerializeDeserialize"
|
||||
deserializer="connectionSerializeDeserialize"/>
|
||||
port="${availableServerSocket}"
|
||||
serializer="connectionSerializeDeserialize"
|
||||
deserializer="connectionSerializeDeserialize"/>
|
||||
|
||||
|
||||
<bean id="connectionSerializeDeserialize" class="org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer"/>
|
||||
<bean id="connectionSerializeDeserialize" class="org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer"/>
|
||||
|
||||
|
||||
<int-ip:tcp-inbound-gateway id="gatewayCrLf"
|
||||
@@ -58,9 +62,9 @@
|
||||
request-channel="incomingServerChannel"
|
||||
error-channel="errorChannel"/>
|
||||
|
||||
<!-- We leave a message listener off of this channel on purpose because we hook
|
||||
one up before the test actually runs (see the unit test associated with this
|
||||
context file) -->
|
||||
<!-- We leave a message listener off of this channel on purpose because we hook
|
||||
one up before the test actually runs (see the unit test associated with this
|
||||
context file) -->
|
||||
<int:channel id="incomingServerChannel" />
|
||||
|
||||
</beans>
|
||||
@@ -1,34 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-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-2.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">
|
||||
|
||||
<context:property-placeholder />
|
||||
|
||||
<!-- Server side -->
|
||||
<!-- When creating the socket factory on the server side, we specify both the serializer and deserializer
|
||||
which deals with both accepting a stream formatted with the Stx-Etx bytes as well as sending a stream
|
||||
formatted with the Stx-Etx bytes. -->
|
||||
<!-- Server side -->
|
||||
<!-- When creating the socket factory on the server side, we specify both the serializer and deserializer
|
||||
which deals with both accepting a stream formatted with the Stx-Etx bytes as well as sending a stream
|
||||
formatted with the Stx-Etx bytes. -->
|
||||
<int-ip:tcp-connection-factory id="serverConnectionFactory"
|
||||
type="server"
|
||||
port="11111"
|
||||
single-use="true"
|
||||
so-linger="10000"
|
||||
serializer="connectionSerializeDeserialize"
|
||||
deserializer="connectionSerializeDeserialize"/>
|
||||
port="${availableServerSocket}"
|
||||
single-use="true"
|
||||
so-linger="10000"
|
||||
serializer="connectionSerializeDeserialize"
|
||||
deserializer="connectionSerializeDeserialize"/>
|
||||
|
||||
|
||||
<bean id="connectionSerializeDeserialize" class="org.springframework.integration.samples.tcpclientserver.CustomSerializerDeserializer"/>
|
||||
<bean id="connectionSerializeDeserialize" class="org.springframework.integration.samples.tcpclientserver.CustomSerializerDeserializer"/>
|
||||
|
||||
<int-ip:tcp-inbound-gateway id="gatewayCrLf"
|
||||
connection-factory="serverConnectionFactory"
|
||||
request-channel="incomingServerChannel"
|
||||
error-channel="errorChannel"/>
|
||||
|
||||
<!-- We leave a message listener off of this channel on purpose because we hook
|
||||
one up before the test actually runs (see the unit test associated with this
|
||||
context file) -->
|
||||
<!-- We leave a message listener off of this channel on purpose because we hook
|
||||
one up before the test actually runs (see the unit test associated with this
|
||||
context file) -->
|
||||
<int:channel id="incomingServerChannel" />
|
||||
|
||||
</beans>
|
||||
</beans>
|
||||
|
||||
@@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -32,17 +33,14 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* 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
|
||||
* The test uses 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")
|
||||
@ContextConfiguration(loader=CustomTestContextLoader.class, locations={"/META-INF/spring/integration/tcpClientServerDemo-context.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
public class TcpClientServerDemoTest {
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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 static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
/**
|
||||
* Demonstrates the use of a gateway as an entry point into the integration flow.
|
||||
* The message generated by the gateway is sent over tcp by the outbound gateway
|
||||
* 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.
|
||||
*
|
||||
* This version shows how the conversion service can be used
|
||||
* instead of explicit transformers to convert the byte array payloads to
|
||||
* Strings.
|
||||
*
|
||||
* @author Gary Russell
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration(loader=CustomTestContextLoader.class, locations={"/META-INF/spring/integration/tcpClientServerDemo-conversion-context.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
public class TcpClientServerDemoWithConversionServiceTest {
|
||||
|
||||
@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("");
|
||||
System.out.println(result);
|
||||
assertEquals("echo:", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFail() {
|
||||
String result = gw.send("FAIL");
|
||||
System.out.println(result);
|
||||
assertEquals("FAIL:Failure Demonstration", result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.core.SubscribableChannel;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
import org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer;
|
||||
import org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -43,7 +44,8 @@ import static org.junit.Assert.assertEquals;
|
||||
* @author: ceposta
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = {"/META-INF/spring/integration/tcpServerConnectionDeserialize-context.xml"})
|
||||
@ContextConfiguration(loader=CustomTestContextLoader.class,
|
||||
locations = {"/META-INF/spring/integration/tcpServerConnectionDeserialize-context.xml"})
|
||||
@DirtiesContext
|
||||
public class TcpServerConnectionDeserializeTest {
|
||||
|
||||
|
||||
@@ -30,10 +30,12 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.core.SubscribableChannel;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
import org.springframework.integration.samples.tcpclientserver.support.CustomTestContextLoader;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -47,7 +49,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* @author: ceposta
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = {"/META-INF/spring/integration/tcpServerCustomSerialize-context.xml"})
|
||||
@ContextConfiguration(loader=CustomTestContextLoader.class,
|
||||
locations = {"/META-INF/spring/integration/tcpServerCustomSerialize-context.xml"})
|
||||
@DirtiesContext
|
||||
public class TcpServerCustomSerializerTest {
|
||||
|
||||
@@ -55,6 +58,9 @@ public class TcpServerCustomSerializerTest {
|
||||
@Qualifier("incomingServerChannel")
|
||||
MessageChannel incomingServerChannel;
|
||||
|
||||
@Value("${availableServerSocket}")
|
||||
int availableServerSocket;
|
||||
|
||||
@Test
|
||||
public void testHappyPath() {
|
||||
|
||||
@@ -86,7 +92,7 @@ public class TcpServerCustomSerializerTest {
|
||||
Writer out = null;
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
socket = new Socket("localhost", 11111);
|
||||
socket = new Socket("localhost", availableServerSocket);
|
||||
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
out.write(sourceMessage);
|
||||
out.flush();
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.support;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.support.GenericXmlContextLoader;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
*
|
||||
*/
|
||||
public class CustomTestContextLoader extends GenericXmlContextLoader {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(CustomTestContextLoader.class);
|
||||
|
||||
@Override
|
||||
protected void loadBeanDefinitions(GenericApplicationContext context,
|
||||
MergedContextConfiguration mergedConfig) {
|
||||
|
||||
int availableServerSocket = SocketUtils.findAvailableServerSocket(5678);
|
||||
|
||||
final Map<String, Object> sockets = new HashMap<String, Object>();
|
||||
sockets.put("availableServerSocket", availableServerSocket);
|
||||
|
||||
if (LOGGER.isInfoEnabled()) {
|
||||
LOGGER.info("Available Server Socket 1: " + availableServerSocket);
|
||||
}
|
||||
|
||||
final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);
|
||||
|
||||
context.getEnvironment().getPropertySources().addLast(propertySource);
|
||||
super.loadBeanDefinitions(context, mergedConfig);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user