【问题标题】:spring integration callback on message received收到消息的spring集成回调
【发布时间】:2017-03-13 14:11:59
【问题描述】:

对 spring 和 spring 集成还是新手,所以请多多包涵。 =)

我已经设置了一个客户端以使用 TCP 连接到远程服务器。一旦建立连接,服务器就会发送一条消息。使用 ngrep 我已验证连接已建立并且消息是从服务器发送的。

通过使用网关接口“gw”,我可以成功接收到消息。但是我想做的是在收到消息时触发 com.example.Client.onMessage 方法。我的理解是,这应该可以使用如下所示的 ServiceActivator 来实现。这是真的还是我必须使用自己的专用线程进行阻塞接收?谢谢。

配置

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

  <int-ip:tcp-connection-factory id="client"
    type="client" host="localhost" port="12000"
    single-use="false" so-timeout="10000" so-keep-alive="true" deserializer="javaDeserializer"
    serializer="javaSerializer"/>

  <int:gateway id="gw" service-interface="com.example.Interface"
    default-request-channel="input" default-reply-channel="replies" />

  <int:channel id="input" />

  <int:channel id="replies">
    <int:queue />
  </int:channel>

  <int-ip:tcp-outbound-channel-adapter
    id="outboundClient" channel="input" connection-factory="client" />

  <int-ip:tcp-inbound-channel-adapter
    id="inboundClient" channel="replies" connection-factory="client"
    client-mode="true" retry-interval="10000" auto-startup="true" />

  <int:service-activator input-channel="input" output-channel="replies" ref="com.example.Client" method="onMessage" />

ServiceActivator

@EnableIntegration
@IntegrationComponentScan
@MessageEndpoint
public class Client {

    @ServiceActivator
    public void onMessage(byte[] received) {
        //Not called
    }
}

【问题讨论】:

    标签: java spring spring-integration


    【解决方案1】:
    1. @EnableIntegration@IntegrationComponentScan 必须在 @Configuration 上配置。一般的Spring注解配置原则:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-java。尽管有 XML 配置,但您根本不需要这些注释。

    2. 如果你想从TCP接收消息到&lt;service-activator&gt;,你必须把它的input-channel配置成replies

    3. 现在你的配置很乱:input 频道的多个订阅者。在这种情况下,他们通过循环方式接收传入的消息。

    4. 如果我理解正确,您应该删除所有 &lt;int:gateway&gt; 员工,然后执行第 2 步。虽然尚不清楚您将如何向input 频道发送消息...

    【讨论】:

    • 是的,一团糟。 =) 删除了 并添加了一个轮询器 。现在按我的预期调用 onMessage 。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多