【问题标题】:having spring integration tcpserver to manage clients and send them messages有 spring 集成 tcpserver 来管理客户端并向他们发送消息
【发布时间】:2015-06-08 15:26:15
【问题描述】:

我已经创建了一个简单的带有 spring 集成的 tcp 服务器,它使连接保持活动状态并在连接期间响应每个请求。

在那个 requestMethod 中,我还可以读取 MessageHeder 来获取 connectionId。

现在我想从服务器向客户端发送消息。

据我了解,我需要将 connectionid 放在 MessageHeader 中,然后发送消息。但我不知道如何做后一个。我已准备好消息,但如何发送/推出?

这是我的 xml 配置:

<bean id="lfSerializer" class="org.springframework.integration.ip.tcp.serializer.ByteArrayLfSerializer"/>

<int-ip:tcp-connection-factory
    id="socketserver"
    type="server"
    port="30124"
    using-nio="true"
    deserializer="lfSerializer"
    serializer="lfSerializer"
    single-use="false"/>

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

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

<int:channel id="inputChannel">
    <int:interceptors>
        <int:wire-tap channel="logger"/>
    </int:interceptors>
</int:channel>

<int:channel id="outputChannel">
    <int:interceptors>
        <int:wire-tap channel="logger"/>
    </int:interceptors>
</int:channel>

<int:logging-channel-adapter id="logger" level="DEBUG" log-full-message="true"/>

<int:service-activator input-channel="inputChannel"
                   output-channel="outputChannel"
                   ref="echoService"
                   method="test"/>

<bean id="echoService"
    class="com.examples.EchoService" />

我还尝试创建一个 bean 和另一个 serviceactivator 用于输出,然后自动装配该 bean 并将其称为“发送”方法,但我不知道在该发送方法中实现什么来发送消息。

【问题讨论】:

    标签: java tcp spring-integration


    【解决方案1】:

    如果是简单的请求/响应场景,请使用入站网关而不是通道适配器,框架会为您处理相关性。这用于the sample app。只需让您的 POJO 方法返回回复负载即可。

    如果您想向客户端发送任意消息(即不是请求/回复,而是说 in、out、out、in、out、out、out 等),那么,是的,您需要自己构建消息,插入 ip_connectionId 标头。

    要发送它们,有几个选项:

    outputChannel 注入您的代码

    @Autowired
    private MessageChannel outputChannel;
    

    使用MessagingTemplate 发送到通道(或直接调用其send(Message&lt;?&gt; message) 方法)。

    或者

    使用带有 void 返回方法的 MessagingGateway 并将网关注入到您的代码中。

    编辑:

    注意,如果你想在收到任何东西之前开始发送消息,你可以通过connection opened event获取连接ID。

    【讨论】:

    • 自动连接频道是我错过的。谢谢。 :)
    猜你喜欢
    • 2021-03-17
    • 2017-05-03
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 2020-07-10
    • 2016-05-25
    • 1970-01-01
    相关资源
    最近更新 更多