2016-12-18 92 views
0

我想寫一個完美的客戶端 - 服務器應用程序就是Spring Integration。我有一部分服務器接收消息並向客戶端發送響應。春季集成tcp服務器發送消息給tcp客戶端

我想不時向客戶發送一些信息。我在TcpConnectionEvent中設置了一個名爲connectionId的頭,但沒有任何結果。有我的代碼在下面。我從幾天就解決了這個問題。感謝您的任何halp!

<!-- CLIENT SIDE --> 

<int:gateway id="gw" 
    service-interface="com.app.hos.service.client.Gateway" 
    default-request-channel="input"/> 

<int-ip:tcp-connection-factory id="client" 
    type="client" 
    host="localhost" 
    port="14020" 
    single-use="false" 
    so-timeout="10000" 
    /> 

<int:channel id="input" /> 

<int-ip:tcp-outbound-gateway id="outGateway" 
    request-channel="transformChannel" 
    reply-channel="reply" 
    connection-factory="client" 
    request-timeout="10000" 
    reply-timeout="10000" 
    /> 
<int:channel id="transformChannel" /> 

<int:channel id="reply" datatype="java.lang.String" /> 

<!-- TRANSFORMERS --> 
<int:transformer id="testTransformer" ref="testTransformerBean" input-channel="input" 
     method="transform" output-channel="transformChannel"/> 

<bean id="testTransformerBean" class="com.app.hos.service.integration.Transformer" /> 

<!-- SERVER SIDE --> 

<bean id="connectionSerializeDeserialize" class="com.app.hos.service.integration.ByteArrayToStringConverter"/> 

<int-ip:tcp-connection-factory id="hosServer" 
    type="server" 
    port="14020" 
    serializer="connectionSerializeDeserialize" 
    deserializer="connectionSerializeDeserialize" 
    using-nio="true"/> 

<int-ip:tcp-inbound-gateway id="inputHosGateway" 
    connection-factory="hosServer" 
    request-channel="toServerChannel" 
    error-channel="errorChannel"/> 


<int:channel id="toServerChannel"/> 

<int:channel id="errorChannel"/> 

<int:channel id="inputChannel" /> 

<int:service-activator input-channel="toServerChannel" 
    ref="server" 
    method="serverTest"/> 

<bean id="server" 
    class="com.app.hos.service.server.Server" /> 


<!-- TCP EVENTS --> 

<int:service-activator input-channel="eventChannel" 
    ref="event" 
    method="eventTest"/> 

<bean id="event" 
    class="com.app.hos.service.integration.Event" /> 

<int:channel id="eventChannel"/> 

<int-event:inbound-channel-adapter channel="eventChannel" 
       event-types="org.springframework.integration.ip.tcp.connection.TcpConnectionEvent"/> 

Transforemr這裏我設置的ConnectionId:

@Autowired 
public Event event; 

public Message<String> transform(Message<String> msg) { 

    Message<String> newMessage = MessageBuilder.withPayload(msg.getPayload()) 
      .setHeader(IpHeaders.CONNECTION_ID, event.getConncetionId()) 
      .copyHeadersIfAbsent(msg.getHeaders()) 
      .build(); 
    return newMessage; 
} 

MVC控制器,我試圖通過網關發送消息:

@Autowired 
public TestController(Gateway gateway) { 
    this.gateway = gateway; 
} 

@RequestMapping(value = "/showTest", method=RequestMethod.GET) 
public String showTestPage() { 
    return "test/sendMessageTest"; 
} 

@RequestMapping(value = "/sendMessage", method=RequestMethod.GET) 
public void sendMessage() { 
    gateway.send("Working!"); 
} 

回答

0

不能使用網關發送任意的數據時,他們嚴格要求/回覆消息。

無論如何,您正在發送到完全不同的連接。

而不是一個入站網關,您需要一個入站通道適配器和出站通道適配器(共享服務器連接工廠)。

當您想要發送任意數據(不是請求/應答的一部分)時,將消息發送到oubound通道適配器,連接標頭設置適當。

0

感謝您的幫助!這就是解決方案:

<int:channel id="input" /> 

<int-ip:tcp-outbound-channel-adapter id="outboundChannel" 
    channel="transformChannel" 
    connection-factory="hosServer" /> 

<int-ip:tcp-inbound-channel-adapter id="inboundChannel" 
    channel="toServerChannel" 
    connection-factory="hosServer" 
    /> 


<int:channel id="transformChannel" /> 

<int:channel id="reply" datatype="java.lang.String" /> 

<!-- TRANSFORMERS --> 
<int:transformer id="testTransformer" ref="testTransformerBean" input-channel="input" 
     method="transform" output-channel="transformChannel"/> 

<bean id="testTransformerBean" class="com.app.hos.service.integration.Transformer" /> 

<!-- SERVER SIDE --> 

<bean id="connectionSerializeDeserialize" class="com.app.hos.service.integration.ByteArrayToStringConverter"/> 

<int-ip:tcp-connection-factory id="hosServer" 
    type="server" 
    port="14020" 
    serializer="connectionSerializeDeserialize" 
    deserializer="connectionSerializeDeserialize" 
    using-nio="true"/> 


<int:channel id="toServerChannel"/> 

<int:channel id="errorChannel"/> 

<int:channel id="inputChannel" /> 

<int:service-activator input-channel="toServerChannel" 
    ref="server" 
    method="serverTest"/> 

<bean id="server" 
    class="com.app.hos.service.server.Server" /> 


<!-- TCP EVENTS --> 

<int:service-activator input-channel="eventChannel" 
    ref="event" 
    method="eventTest"/> 

<bean id="event" 
    class="com.app.hos.service.integration.Event" /> 

<int:channel id="eventChannel"/> 

<int-event:inbound-channel-adapter channel="eventChannel" 
       event-types="org.springframework.integration.ip.tcp.connection.TcpConnectionEvent"/> 

相關問題