2015-06-08 40 views
0

我已經創建了一個簡單的tcp服務器與彈簧集成,它保持連接活着並在連接期間響應每個請求。有彈簧集成tcpserver來管理客戶端併發送消息

在該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,並呼籲它「送」的方法,但我不知道是什麼在該發送方法中實現發送消息。

回答

2

如果這是一個簡單的請求/響應場景,請使用入站網關而不是通道適配器,並且框架將爲您處理相關性。這用於the sample app。只需讓你的POJO方法返回應答負載。如果你想發送任意消息到客戶端(即不要求/回覆,但是,例如,進出出,等等),那麼,是的,你需要自己構建消息,插入ip_connectionId標題。

送他們,有幾個選項:

進樣outputChannel到你的代碼

@Autowired 
private MessageChannel outputChannel; 

使用MessagingTemplate發送到信道(或乾脆直接調用它send(Message<?> message)方法)。

或者

使用一個返回值爲void方法MessagingGateway並注入網關到你的代碼。

編輯:

請注意,如果你想開始接收任何事情之前發送消息,您可以通過connection opened event獲取連接ID。

+0

自動裝配頻道是我錯過的。謝謝。 :) – Jens

相關問題