2016-04-26 58 views
1

我試圖實現一個tcp客戶端,該客戶端將使用spring集成連接到現有服務器。對於這項技術仍然很陌生,我在https://github.com/spring-projects/spring-integration-samples上跟着春天的例子。通過Spring集成發送服務器到客戶端的請求

我已經設法實現了客戶端連接到服務器,然後發送請求,然後從服務器接收回應的簡單情況。但是,由於實現服務器的方式,我現在需要實現以下情況:客戶端必須連接到服務器並等待來自服務器的請求,然後響應響應數據。

我該如何實施?它看起來不像我可以在客戶端使用網關,因爲網關要求我在發送任何響應數據之前發送請求。然後我考慮使用tcp-outbound-gateway的回覆通道作爲其輸入通道來配置服務激活器。但是,服務實施沒有收到答覆。似乎只有在我發送來自客戶端的第一個請求後纔會打開該頻道。目前,我有以下幾點:

Spring配置:

<int-ip:tcp-connection-factory id="client" 
           type="client" 
           host="localhost" 
           port="12345" 
           single-use="false" 
           so-timeout="10000" 
           deserializer="javaSerializerDeserializer" 
           serializer="javaSerializerDeserializer" /> 
<bean id="javaSerializerDeserializer" 
     class="org.springframework.integration.ip.tcp.serializer.ByteArrayLfSerializer" /> 

<int-ip:tcp-outbound-gateway id="outGateway" 
          request-channel="input" 
          reply-channel="clientBytes2StringChannel" 
          connection-factory="client" 
          request-timeout="10000" 
          reply-timeout="10000" /> 

<int:object-to-string-transformer id="clientBytes2String" 
            input-channel="clientBytes2StringChannel" /> 

<int:service-activator input-channel="clientBytes2StringChannel" ref="thinclientService" /> 
<bean id="thinclientService" class="tcp.service.ThinClientService" /> 

ThinClientService:

@Component 
public class ThinClientService { 
    private static final Logger LOGGER = LoggerFactory.getLogger(ThinClientService.class); 

    public String receive(String recv) { 
     LOGGER.info("*****************Recv: {}", recv); 
     return "echo"; 
    } 
} 

主要方法:

@SpringBootApplication 
public class Application { 
    public static void main(String args[]) throws Exception { 
      GenericXmlApplicationContext context = new GenericXmlApplicationContext(); 
     context.load("classpath:META-INF/beans.xml"); 
     context.registerShutdownHook(); 
     context.refresh(); 

     System.out.println("Running test"); 

     SpringApplication.run(Application.class, args); 
    } 
} 

即使我不想發送數據,我如何強制此客戶端連接到我的服務器?

回答

1

自己搞清楚了。我必須將tcp-outbound-gateway更改爲client-mode =「true」的tcp-inbound-gateway。只有這樣服務激活器纔會收到服務器發送的消息。

更新豆配置:

<int-ip:tcp-connection-factory id="client" 
           type="client" 
           host="localhost" 
           port="12345" single-use="false" so-timeout="10000" deserializer="javaSerializerDeserializer" 
           serializer="javaSerializerDeserializer" /> 
<bean id="javaSerializerDeserializer" 
     class="org.springframework.integration.ip.tcp.serializer.ByteArrayLfSerializer" /> 

<int-ip:tcp-inbound-gateway id="outGateway" 
          request-channel="input" 
          connection-factory="client" 
          client-mode="true" 
          retry-interval="1000" 
          scheduler="reconnectScheduler" /> 

<bean id="reconnectScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler" /> 

<int:object-to-string-transformer id="clientBytes2String" 
            input-channel="input" output-channel="responseString" /> 

<int:service-activator input-channel="responseString" ref="thinclientService" /> 
<bean id="thinclientService" class="tcp.service.ThinClientService" /> 
+0

這是偉大的!所以,你甚至可以接受你自己的答案! –

+0

似乎接口讓我只能在兩天後接受我自己的答案。 – mdewit

相關問題