spring integration tcp server向tcp客户端发送消息

时间:2016-12-18 18:47:52

标签: spring tcp server client spring-integration

我想写一个完美的客户端 - 服务器应用程序是Spring Integration。我有一个服务器接收消息并向客户端发送响应的部分。

我想不时向客户发送包含一些信息的消息。我在TcpConnectionEvent中设置了一个带有connectionId的头,但没有任何事情发生。下面有我的代码。我几天就堆积了这个问题。谢谢你的任何挫折!

<!-- 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!");
}

2 个答案:

答案 0 :(得分:0)

发送任意数据时,您无法使用网关,严格来说,这些数据是请求/回复消息。

无论如何,您发送的是完全不同的连接。

您需要入站通道适配器和出站通道适配器(共享服务器连接工厂),而不是入站网关。

如果要发送任意数据(不是请求/回复的一部分),请将消息发送到oubound通道适配器,并正确设置连接ID头。

答案 1 :(得分: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"/>