1. 程式人生 > >spring integration學習入門之tcp通訊

spring integration學習入門之tcp通訊

第一次編寫csdn的部落格,只怪stackoverflow死活登不上,沒辦法了

最近工作需要需要使用springintegration,以前沒有使用過,不得已從頭開始學習,資料也甚是不全,只能自己到處找英文原版看,也是辛苦,弄了好幾天,總算跑通了helloworld和tcp的通訊了。

閒言少敘,直接上乾貨。

環境搭建

Maven 獲取專案所需原始碼

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bocom.bbip</groupId>
<artifactId>bbip-integration-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>3.2.1.RELEASE</spring.version>
<spring.integration.version>4.1.8.RELEASE</spring.integration.version>
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>




<dependencies>
<!-- Spring Integration -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>${spring.integration.version}</version>
</dependency>


<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ip</artifactId>
<version>${spring.integration.version}</version>
</dependency>
</dependencies>




</project>

通過配置

maven獲取到所有spring integration所需要使用的程式碼,為了方便除錯,我這邊也拉下來了所有的原始碼。


接下來是具體程式碼實現的講解。

服務端的設計

服務端配置如下: <beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns="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/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:component-scan base-package="messageHeader" />
<!-- below is tcp test -->
<beans:bean id="tcpIntegration" class="tcp.TcpIntegrationTest" />
<beans:bean id="javaSerializer"
class="org.springframework.core.serializer.DefaultSerializer" />
<beans:bean id="javaDeserializer"
class="org.springframework.core.serializer.DefaultDeserializer" />
<beans:bean id="toBytes" class="tcp.ToBytes" />
<int-ip:tcp-connection-factory id="server"
type="server" port="1234" deserializer="javaDeserializer" serializer="javaSerializer"
single-use="true" />
<!-- <int-ip:tcp-connection-factory id="client" type="client" host="localhost" 
port="1234" single-use="true" so-timeout="10000" deserializer="javaDeserializer" 
serializer="javaSerializer" /> -->


<channel id="inputChannel" />


<channel id="outputChannel" />
<!-- <gateway id="tcpGateWay" default-request-channel="inputChannel" default-reply-channel="inputChannel" 
service-interface="tcp.TcpGateWay" /> -->


<service-activator input-channel="inputChannel" ref="tcpIntegration"
method="show" output-channel="outputChannel"/>


<int-ip:tcp-inbound-channel-adapter
id="inboundServer" channel="inputChannel" connection-factory="server" />


<int-ip:tcp-outbound-channel-adapter
id="outboundServer" channel="outputChannel" connection-factory="server" />






</beans:beans>

首先是服務端的設計,spring中tcp服務端,先要有tcp-connection-factory並設定起對應解碼編碼的bean,然後是tcp-inbound-channel-adapter的設計,作為服務端用於監聽客戶端的連結,並將客戶端的連結傳送到對應通道上面。

客戶端傳送連結到服務端,通過服務端的tcp-inbound-channel-adapter將訊息適配到inputChannel


然後通過tcp-inbound-channel-adapter將服務端接收到的訊息做處理並顯示出來,併發送到outputchannel。


服務端接收到客戶端的訊息以後,響應訊息被髮送到outputchannel,通過tcp-outbound-channel-adapter將響應訊息進行外連結到客戶端。

客戶端設計

客戶端配置如下 <beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns="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/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:component-scan base-package="messageHeader" />
<!-- below is tcp test -->
<beans:bean id="tcpIntegration" class="tcp.TcpIntegrationTest" />
<beans:bean id="gatewaySendMessage" class="gateway.SendMessage" />
<beans:bean id="replyShow" class="tcp.ReplyShow" />
<beans:bean id="javaSerializer"
class="org.springframework.core.serializer.DefaultSerializer" />
<beans:bean id="javaDeserializer"
class="org.springframework.core.serializer.DefaultDeserializer" />
<!-- 
<int-ip:tcp-connection-factory id="server"
type="server" port="1234" deserializer="javaDeserializer" serializer="javaSerializer"
using-nio="true" single-use="true" /> -->
<int-ip:tcp-connection-factory id="client"
type="client" host="localhost" port="1234" single-use="true"
so-timeout="10000" deserializer="javaDeserializer" serializer="javaSerializer" />
<channel id="inputChannel" />
<channel id="replyChannel" />
<!-- <gateway id="tcpGateWay" default-request-channel="inputChannel" default-reply-channel="replieChannel" 
service-interface="tcp.TcpGateWay" /> -->
<!-- <service-activator input-channel="inputChannel" output-channel="replieChannel" 
ref="tcpIntegration" method="show" /> -->
<!-- <int-ip:tcp-inbound-channel-adapter id="inboundServer" channel="inputChannel" 
connection-factory="server"/> -->
<int-ip:tcp-outbound-channel-adapter
id="outboundServer" channel="inputChannel" connection-factory="client" />
<int-ip:tcp-inbound-channel-adapter
id="inboundServer" channel="replyChannel" connection-factory="client" />
<!-- 客戶端拿到相應訊息後的處理 -->
<service-activator input-channel="replyChannel" ref="replyShow"
method="replyShow" />
<logging-channel-adapter channel="replyChannel" />
</beans:beans>

同樣客戶端設計首先是connectionfactory,包括客戶端設計

如上配置可看出,本地設計一個客戶端和服務端,繫結埠為1234.


客戶端定義好outboundchanneladapter實現將客戶端通道inoutchannel連線到clientconnectionfactory,所以此時客戶端只需向inputchannel生產訊息,則訊息會從client傳送到對應tcp服務端。實現了客戶端訊息的發出設計。

同樣的,如上章節所述,服務端接收到客戶端請求後,返回相應訊息,所以此時客戶端也亞由inbound-channel-adapter用於接收服務端的相應,並通過service-activator將獲取到的訊息顯示出來。

客戶端傳送程式設計如下

public class GateWayTest {
    public static void main(String[] args){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("integration/tcp-client-intergation.xml");
        SendMessage sendMessage=applicationContext.getBean("gatewaySendMessage",SendMessage.class);
        sendMessage.sendMessage("GateWayTest");
     
    }
    }

public class SendMessage {


    @Autowired
    private MessageChannel inputChannel;


   
    public void sendMessage(String message) {
        Message<String> sendmessage=MessageBuilder.withPayload(message).build();
        inputChannel.send(sendmessage);
    }


}

 

測試

如上配置完成以後,既可以進行簡單測試,首先啟動服務端



package tcp;


import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Server {


    public static void main(String[] args) {
        @SuppressWarnings("unused")
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("integration/tcp-server-intergation.xml");
        
    }




    
}


服務端啟動以後,監聽localhost的1234埠

 

客戶端傳送測試程式


客戶端傳送完成以後,服務端接收到訊息並打印出來,


此時服務端返回響應訊息給客戶端,客戶端接收到也顯示出來


一個簡單的tcp例項即完成了。