2016-03-21 53 views
1

https://github.com/spring-projects/spring-integration-samples/tree/master/intermediate/tcp-client-server-multiplex 我看到了這個例子。所以,我改變了建築。轉移如何將消息傳遞給網關?

  • 發送模式 enter image description here
  • 接收模式 enter image description here 我有一個問題。 我不明白轉移在發送模型中的角色。 我明白聚合傳送訊息傳送。 但是,我不明白如何將消息傳遞給網關。 如何提供?

這是我的彈簧集成配置。

它是將應用程式設定,

<!-- 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

我一個不知道什麼是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標題。

+0

謝謝。我嘗試這種方式。但是,這是行不通的。 –

+1

我不知道你的體系結構,但是如果你遵循'multiplex'示例,你真的必須使用與'tcp:outbound-channel-adapter'相同的'publish-subscribe-channel',以及'aggregator'。但根據你的最後一個代碼,你使用完全不同的通道 - 「batchSendChannel」。請小心代碼,我不知道我們應該爲你做這件事... –

+0

對不起。batchSendChannel只是輸入錯誤。我將改爲確切的配置。 –