转移如何向Gateway发送消息?

时间:2016-03-21 16:44:08

标签: spring spring-integration

https://github.com/spring-projects/spring-integration-samples/tree/master/intermediate/tcp-client-server-multiplex 我看到了这个例子。所以,我改变了架构。

  • 发送模型 enter image description here
  • 收到模特 enter image description here 我有一个问题。 我不明白转移在发送模型中的作用。 我理解聚合器传递消息到转移。 但是,我不明白如何向网关传递消息。 怎么送货?

那是我的spring integration config。

发送应用设置,

  <!-- telegram bean -->
  <bean id="byteArrayRawSerializer" class="org.springframework.integration.ip.tcp.serializer.ByteArrayRawSerializer">
    <property name="maxMessageSize" value="2000"/>
  </bean>  
  <bean id="myCorrelationStrategy" class="com.test.util.MyCorrelationStrategy"/>
  <!-- online connection factory -->
  <int-ip:tcp-connection-factory id="server" type="server" port="20001" single-use="false" lookup-host="false" so-timeout="60000" serializer="byteArrayRawSerializer" deserializer="byteArrayRawSerializer" />
  <int-ip:tcp-connection-factory id="client" type="client" host="localhost" port="10001" single-use="false" so-timeout="60000" serializer="byteArrayRawSerializer" deserializer="byteArrayRawSerializer" />

  <!--  Send -->
  <int:gateway id="sendGateway" 
        service-interface="com.test.MySendGateway"
        default-reply-timeout="20000"
        default-request-channel="sendChannel"/>

  <int:publish-subscribe-channel id="sendChannel" />

  <int-ip:tcp-outbound-channel-adapter id="sendAdapter" order="2" channel="sendChannel" connection-factory="client" /> 

  <int:bridge input-channel="batchSendChannel" output-channel="toSendAggregator" order="1"/> 

  <int:channel id="toSendAggregator" datatype="byte[]"/>

  <int:aggregator input-channel="toSendAggregator"
        output-channel="toSendTransformer"
        expire-groups-upon-completion="true"
        correlation-strategy="myCorrelationStrategy"
        correlation-strategy-method="getCorrelationKey"
        release-strategy-expression="size() == 2" />

  <int:transformer input-channel="toSendTransformer"  expression="payload.get(1)"/>

  <int-ip:tcp-inbound-channel-adapter id="sendReplyAdapter" channel="toSendAggregator" connection-factory="client" />  

  <!--  Receive -->  
  <int-ip:tcp-inbound-channel-adapter id="inboundAdapter" channel="batchInboundChannel" connection-factory="batchReceiveServer" />  
  <int-ip:tcp-outbound-channel-adapter id="outboundAdapter" channel="batchOutboundChannel" connection-factory="batchReceiveServer"/>

  <int:channel id="batchInboundChannel" />

  <int:channel id="batchOutboundChannel"/>

  <int:service-activator id="myReceiveServiceActivator" method="receive" input-channel="batchInboundChannel">
    <bean class="com.test.endpoint.MyReceiveServiceActivator" />
  </int:service-activator>

并且,它是交付应用程序设置(图片的中心),

  <!-- Bean load -->
  <bean id="byteArrayRawSerializer" class="org.springframework.integration.ip.tcp.serializer.ByteArrayRawSerializer">
    <property name="maxMessageSize" value="2000"/>
  </bean>  

  <!-- Connection factory -->
  <int-ip:tcp-connection-factory id="server" type="server" port="30001" using-nio="true" single-use="false" lookup-host="false" so-timeout="10000" serializer="byteArrayRawSerializer"  deserializer="byteArrayRawSerializer" />  
  <int-ip:tcp-connection-factory id="client" type="client" host="${RemoteTarget.ip}" port="${RemoteTarget.port}" using-nio="true" single-use="false" so-timeout="10000" serializer="byteArrayRawSerializer"  deserializer="byteArrayRawSerializer" />

  <!-- Connection factory -->
  <int-ip:tcp-connection-factory id="inboundServer" type="server" port="10001" using-nio="true"  single-use="false" lookup-host="false" so-timeout="${server.outbound.connectionTimeout}" serializer="byteArrayRawSerializer"  deserializer="byteArrayRawSerializer" />  
  <int-ip:tcp-connection-factory id="inboardClient" type="client" host="localhost" port="20001" using-nio="true" single-use="false" so-timeout="10000" serializer="byteArrayRawSerializer"  deserializer="byteArrayRawSerializer" />

  <int-ip:tcp-inbound-channel-adapter id="receiveAdapter" channel="receiveChannel" connection-factory="server" auto-startup="true"/>
  <int-ip:tcp-inbound-channel-adapter id="sendReplyAdapter" channel="sendReplyChannel" connection-factory="client" auto-startup="true"/>

  <int:channel id="receiveChannel"/>
  <int:channel id="sendReplyChannel"/>

  <int-ip:tcp-outbound-channel-adapter id="receiveAdapter.inboard" channel="receiveChannel" connection-factory="inboundServer" />
  <int-ip:tcp-outbound-channel-adapter id="sendReplyAdapter.inboard" channel="sendReplyChannel" connection-factory="inboardClient" />

  <int-ip:tcp-outbound-channel-adapter id="sendAdapter" channel="sendChannel" connection-factory="client" />  
  <int-ip:tcp-outbound-channel-adapter id="receiveReplyAdapter" channel="receiveReplyChannel" connection-factory="server" />

  <int:channel id="sendChannel" />

  <int:channel id="receiveReplyChannel"/>

  <int:router id="outRouter" input-channel="toRouter" method="route" auto-startup="true">
      <bean class="com.test.endpoint.MyRouter"/>
  </int:router>   

  <int:channel id="toRouter"/>

  <int-ip:tcp-inbound-channel-adapter id="sendAdapter.inboard" channel="toRouter" connection-factory="inboundServer" auto-startup="true"/>
  <int-ip:tcp-inbound-channel-adapter id="receiveReplyAdapter.inboard" channel="toRouter" connection-factory="inboardClient" auto-startup="true"/>

但是,当我调用gw.send(msg)时,它不起作用, (确切地说,消息在传递机器的'sendAdapter.inboard'处停止,直到超时并且路由器收到此消息....

1 个答案:

答案 0 :(得分:1)

我不确定你的ar3chitecture中的transfer是什么。

根据您展示的样本,我们有:

gateway -> outbound-channel-adapter
        |-> aggregator

inbound-channel-adapter->aggregator->transformer

gateway将请求发送到TCP出站通道适配器和聚合器,只是因为<publish-subscribe-channel id="input" />

我们知道gateway是一个请求/回复组件。当它发送请求时,由于合同,它等待回复:

public interface SimpleGateway {

    String send(String text);

}

在这种情况下,网关将TemporaryReplyChannel添加为replyChannel标头以等待回复。

当我们没有在最后一个下游流消费者上指定output-channel时,会产生回复。

在我们的情况下,它恰好是<transformer>之后的<aggregator>

<transformer input-channel="toTransformer.client"
    expression="payload.get(1)"/> <!-- The response is always second -->

我认为这是你正在寻找的技巧。它在消费者逻辑中称为replyChannel标题。