2016-05-12 79 views
2

我试图在Spring集成中实现TCP Client。我有一个远程TCP服务器,将数据泵入套接字。基于Spring的TCP客户端必须从该套接字接收数据。spring -integration TCP网关,连接和接收来自套接字的数据

作为客户端,我不会将任何数据从我身边发送到服务器,只需连接和接收数据即可。看着这http://forum.spring.io/forum/spring-projects/integration/94696-want-to-configure-simple-tcp-client-to-receive-data-from-java-based-tcp-server?view=thread,我明白这是不可能的。但是,收到的答案相当陈旧,现在有没有可用的配置?

如果您还有其他问题,请让我知道。

@updated与配置

<bean id="javaSerializer" class="org.springframework.core.serializer.DefaultSerializer" /> 
<bean id="javaDeserializer" class="org.springframework.core.serializer.DefaultDeserializer" /> 

<context:property-placeholder /> 

<!-- Client side --> 

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

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

<int:channel id="input" /> 

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

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

<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" /> 

这里是我的远程TCP客户端:

final GenericXmlApplicationContext context = new GenericXmlApplicationContext(); 
    context.load("classpath:config.xml"); 

    context.registerShutdownHook(); 
    context.refresh(); 

    final SimpleGateway gateway = context.getBean(SimpleGateway.class); 
    int i=0; 
    while(i++<10){ 
    String h = gateway.receive(); 
    System.out.println(System.currentTimeMillis()+h); 

我TCP服务器模拟:

while(true) { 
    try { 
     System.out.println("Waiting for client on port " + 
     serverSocket.getLocalPort() + "..."); 

     Socket server = serverSocket.accept(); 
     System.out.println("Just connected to " 
       + server.getRemoteSocketAddress()); 

     DataOutputStream out = 
      new DataOutputStream(server.getOutputStream()); 
     out.write("ACK\r\n".getBytes()); 

     out.flush(); 

     //server.close(); 

    } catch(SocketTimeoutException s) { 
     System.out.println("Socket timed out!"); 
     break; 
    } catch(IOException e) { 
     e.printStackTrace(); 
     break; 
    } 
    } 

我的网关类:

public interface SimpleGateway {  
    public String receive(); 
} 

回答

1

TcpReceivingChannelAdapter<ip:tcp-inbound-channel-adapter/>)通常在服务器模式下运行 - 它侦听套接字以传入到客户端的连接。

然而,一个clientModeclient-mode)布尔已被添加为精确此用例。它将连接到服务器并接收来自它的传入数据。如果连接丢失,它将重试连接(按配置时间表)。

参见the documentation

通常情况下,入站适配器使用类型=“服务器”连接工厂,它监听传入连接请求。在某些情况下,需要建立反向连接,从而入站适配器连接到外部服务器,然后等待该连接上的入站消息。

通过在入站适配器上使用client-mode="true"来支持此拓扑。在这种情况下,连接工厂必须是client,并且必须将single-use设置为false。

另外两个属性用于支持此机制:retry-interval指定(毫秒)框架在连接失败后尝试重新连接的频率。 scheduler用于提供用于调度连接尝试的TaskScheduler,并测试连接是否仍处于活动状态。

如果未提供调度程序,则使用默认的taskScheduler bean。

+0

谢谢您的快速回复。我在GIT上跟随你的例子。我会在上述配置上做,并且会返回结果。希望我能得到它! :) –

+0

我尝试了解释的配置。但是我看不到服务器的回应。我不确定我是否错过了任何东西或者可能不正确。我有一个用方法定义的接口网关:'public String receive(String text);'。它只是从TCP服务器接收数据。 xml配置如文档中所述。请在配置上方找到。在我的客户端'TCPClient'中,我调用'receive()',但没有收到数据。 –

+0

更新上面的配置后,我尝试使用自定义desializer,而不是java deserialilzer。好消息是,我看到数据进入'deserialize()'方法,而不是我的'TCPClient.receive()'。这是陷入在'中的run方法org.springframework.integration.ip.tcp.connection.TcpNetConnection ',因为okToRun是真实的。我正在详细写这篇文章,因为我知道你是这个班的作者。 –