From 27f86a78b4431093ce4fdcc40dd48eeeb68b959d Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Fri, 1 Oct 2010 16:49:04 -0400 Subject: [PATCH] INTSAMPLES-4 Add Simple TCP Client/Server Sample --- basic/tcp-client-server/.classpath | 10 +++ basic/tcp-client-server/.gitignore | 1 + basic/tcp-client-server/.project | 35 +++++++++ .../com.springsource.sts.config.flow.prefs | 4 ++ ...ringframework.ide.eclipse.beans.core.prefs | 23 ++++++ basic/tcp-client-server/.springBeans | 14 ++++ basic/tcp-client-server/pom.xml | 72 +++++++++++++++++++ basic/tcp-client-server/readme.txt | 8 +++ .../samples/tcpclientserver/EchoService.java | 31 ++++++++ .../tcpclientserver/SimpleGateway.java | 26 +++++++ .../tcpClientServerDemo-context.xml | 53 ++++++++++++++ .../TcpClientServerDemoTest.java | 51 +++++++++++++ .../src/test/resources/log4j.xml | 28 ++++++++ 13 files changed, 356 insertions(+) create mode 100644 basic/tcp-client-server/.classpath create mode 100644 basic/tcp-client-server/.gitignore create mode 100644 basic/tcp-client-server/.project create mode 100644 basic/tcp-client-server/.settings/com.springsource.sts.config.flow.prefs create mode 100644 basic/tcp-client-server/.settings/org.springframework.ide.eclipse.beans.core.prefs create mode 100644 basic/tcp-client-server/.springBeans create mode 100644 basic/tcp-client-server/pom.xml create mode 100644 basic/tcp-client-server/readme.txt create mode 100644 basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/EchoService.java create mode 100644 basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/SimpleGateway.java create mode 100644 basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-context.xml create mode 100644 basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java create mode 100644 basic/tcp-client-server/src/test/resources/log4j.xml diff --git a/basic/tcp-client-server/.classpath b/basic/tcp-client-server/.classpath new file mode 100644 index 00000000..dde3100b --- /dev/null +++ b/basic/tcp-client-server/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/basic/tcp-client-server/.gitignore b/basic/tcp-client-server/.gitignore new file mode 100644 index 00000000..ea8c4bf7 --- /dev/null +++ b/basic/tcp-client-server/.gitignore @@ -0,0 +1 @@ +/target diff --git a/basic/tcp-client-server/.project b/basic/tcp-client-server/.project new file mode 100644 index 00000000..cc6ea292 --- /dev/null +++ b/basic/tcp-client-server/.project @@ -0,0 +1,35 @@ + + + tcp-client-server + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + org.maven.ide.eclipse.maven2Builder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.maven.ide.eclipse.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/basic/tcp-client-server/.settings/com.springsource.sts.config.flow.prefs b/basic/tcp-client-server/.settings/com.springsource.sts.config.flow.prefs new file mode 100644 index 00000000..13af1492 --- /dev/null +++ b/basic/tcp-client-server/.settings/com.springsource.sts.config.flow.prefs @@ -0,0 +1,4 @@ +#Fri Oct 01 16:39:52 EDT 2010 +//com.springsource.sts.config.flow.coordinates\:http\://www.springframework.org/schema/integration\:/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-context.xml=\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n +//com.springsource.sts.config.flow.coordinates\:http\://www.springframework.org/schema/integration\:/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo.xml=\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n +eclipse.preferences.version=1 diff --git a/basic/tcp-client-server/.settings/org.springframework.ide.eclipse.beans.core.prefs b/basic/tcp-client-server/.settings/org.springframework.ide.eclipse.beans.core.prefs new file mode 100644 index 00000000..2c9ba628 --- /dev/null +++ b/basic/tcp-client-server/.settings/org.springframework.ide.eclipse.beans.core.prefs @@ -0,0 +1,23 @@ +#Fri Oct 01 15:50:57 EDT 2010 +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/aop= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/beans= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/context= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/integration= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/jee= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/lang= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/task= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/tx= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/util= +//org.springframework.ide.eclipse.beans.core.prefix.http\://www.springframework.org/schema/aop=aop +//org.springframework.ide.eclipse.beans.core.prefix.http\://www.springframework.org/schema/beans=beans +//org.springframework.ide.eclipse.beans.core.prefix.http\://www.springframework.org/schema/context=context +//org.springframework.ide.eclipse.beans.core.prefix.http\://www.springframework.org/schema/integration=int +//org.springframework.ide.eclipse.beans.core.prefix.http\://www.springframework.org/schema/jee=jee +//org.springframework.ide.eclipse.beans.core.prefix.http\://www.springframework.org/schema/lang=lang +//org.springframework.ide.eclipse.beans.core.prefix.http\://www.springframework.org/schema/task=task +//org.springframework.ide.eclipse.beans.core.prefix.http\://www.springframework.org/schema/tx=tx +//org.springframework.ide.eclipse.beans.core.prefix.http\://www.springframework.org/schema/util=util +eclipse.preferences.version=1 +org.springframework.ide.eclipse.beans.core.default.version.check.classpath=true +org.springframework.ide.eclipse.beans.core.enable.project.preferences=true +org.springframework.ide.eclipse.beans.core.loadNamespaceHandlerFromClasspath=true diff --git a/basic/tcp-client-server/.springBeans b/basic/tcp-client-server/.springBeans new file mode 100644 index 00000000..c107e968 --- /dev/null +++ b/basic/tcp-client-server/.springBeans @@ -0,0 +1,14 @@ + + + 1 + + + + + + + src/main/resources/META-INF/spring/integration/tcpClientServerDemo-context.xml + + + + diff --git a/basic/tcp-client-server/pom.xml b/basic/tcp-client-server/pom.xml new file mode 100644 index 00000000..9eff63b4 --- /dev/null +++ b/basic/tcp-client-server/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + org.springframework.integration.samples + tcp-client-server + 2.0.0 + Spring Integration TCP Client Server Sample + jar + + 3.0.3.RELEASE + 2.0.0.M7 + 1.2.16 + 4.7 + + + + org.springframework.integration + spring-integration-ip + ${spring.integration.version} + + + log4j + log4j + ${log4j.version} + + + + junit + junit + ${junit.version} + + + org.springframework + spring-test + ${spring.version} + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + -Xlint:all + true + false + + + + + + + repository.springframework.maven.release + Spring Framework Maven Release Repository + http://maven.springframework.org/release + + + repository.springframework.maven.milestone + Spring Framework Maven Milestone Repository + http://maven.springframework.org/milestone + + + repository.springframework.maven.snapshot + Spring Framework Maven Snapshot Repository + http://maven.springframework.org/snapshot + + + \ No newline at end of file diff --git a/basic/tcp-client-server/readme.txt b/basic/tcp-client-server/readme.txt new file mode 100644 index 00000000..d14179d1 --- /dev/null +++ b/basic/tcp-client-server/readme.txt @@ -0,0 +1,8 @@ +This is a place to get started with tcp. It demonstrates a simple message flow represented by the diagram below: + +Gateway -> Channel -> TcpOutboundGateway -> <===Socket===> -> TcpInboundGateway -> Channel -> ServiceActivator + +The service returns a response which the inbound gateway sends back over the socket to the outbound gateway +and the result is returned to the client that invoked the original SimpleGateway method. + +To run sample simply execute a test case in org.springframework.integration.samples.tcpclientservice package \ No newline at end of file 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 new file mode 100644 index 00000000..8cf27748 --- /dev/null +++ b/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/EchoService.java @@ -0,0 +1,31 @@ +/* + * Copyright 2002-2010 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; + +/** + * Simple service that receives data in a byte array, + * converts it to a String and appends it to 'echo:'. + * + * @author Gary Russell + * + */ +public class EchoService { + + public String test(byte[] bytes) { + return "echo:" + new String(bytes); + } + +} \ 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 new file mode 100644 index 00000000..e5ee0b47 --- /dev/null +++ b/basic/tcp-client-server/src/main/java/org/springframework/integration/samples/tcpclientserver/SimpleGateway.java @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2010 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; + +/** + * @author Gary Russell + * + */ +public interface SimpleGateway { + + public byte[] 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 new file mode 100644 index 00000000..dc2abdf5 --- /dev/null +++ b/basic/tcp-client-server/src/main/resources/META-INF/spring/integration/tcpClientServerDemo-context.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + 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 new file mode 100644 index 00000000..504441cf --- /dev/null +++ b/basic/tcp-client-server/src/test/java/org/springframework/integration/samples/tcpclientserver/TcpClientServerDemoTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2002-2010 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.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. + * + * @author Gary Russell + * + */ +@ContextConfiguration("/META-INF/spring/integration/tcpClientServerDemo-context.xml") +@RunWith(SpringJUnit4ClassRunner.class) +public class TcpClientServerDemoTest { + + @Autowired + SimpleGateway gw; + + @Test + public void test() { + byte[] echo = gw.send("Hello world!"); + String result = new String(echo); + System.out.println(result); + assertEquals("echo:Hello world!", result); + } +} diff --git a/basic/tcp-client-server/src/test/resources/log4j.xml b/basic/tcp-client-server/src/test/resources/log4j.xml new file mode 100644 index 00000000..f391e2ed --- /dev/null +++ b/basic/tcp-client-server/src/test/resources/log4j.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file