Spring TCP Socket Integration实现不传递gateway.send方法

时间:2017-09-12 18:22:55

标签: sockets tcp spring-integration

所以我正在包装Spring Integration TCP客户端,为我的应用程序提供API。有关此问题的先前问题可以在herehere找到。这个问题是gateway.send()根本没有结束,API响应永远不会回来。

这是我的ServerConnection.java文件:

package com.abc.xyz.serverconnection;
import org.springframework.context.support.GenericXmlApplicationContext;   
public class ServerConnections {    
    private SimpleGateway gateway;    
    public ServerConnections() {
        final GenericXmlApplicationContext context = setupContext();
        this.setGateway(context.getBean(SimpleGateway.class));
    }    
    public static GenericXmlApplicationContext setupContext() {
        final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
        context.load("classpath:META-INF/spring/integration/tcpClientServerDemo-context.xml");
        context.registerShutdownHook();
        context.refresh();
        return context;
    }    
    public SimpleGateway getGateway() {
        return gateway;
    }    
    public void setGateway(SimpleGateway gateway) {
        this.gateway = gateway;
    }    
    public boolean sendData(String input) {
        this.gateway.send(input);
        return true;
    }    
    public void recieveData(String output) {
        System.out.println("Data from server:" + output);
    }    
}

在我的控制器中,我做了类似的事情:

@RequestMapping(value = "/logon", method = RequestMethod.GET)
@ResponseBody
public String logon() {
    // logics go here and the result is stored like below and sent
    String message = "0000005401F40000C1E3E304010000000020000000000000000000000000000000000000000000004040404040404040C1E3E300C1C2C3C4C5C6C7C8E8C5E2C8E6C1D540F1F7F24BF0F1F64BF0F0F34BF0F5F200";
    if (serverConnections.sendData(message)) {
        return "Data sent successfully!";
    } else {
        return "Data not sent!";
    }
}

以下是我的配置的样子:

    <context:property-placeholder />
    <int:channel id="input" />
    <int:channel id="toSA" />
    <int:service-activator input-channel="toSA"
                           ref="echoService"
                           method="recieveData"/>
    <bean id="echoService" class="com.abc.xyz.serverconnection.ServerConnection" />
    <bean id="CustomSerializerDeserializer" class="com.abc.xyz.serverconnection.CustomSerializerDeserializer" />
    <int:object-to-string-transformer id="serverBytes2String"
                                      input-channel="serverBytes2StringChannel"
                                      output-channel="toSA"/>
    <int:gateway id="gw"
                 service-interface="com.abc.xyz.serverconnection.SimpleGateway"
                 default-request-channel="input"/>
    <int-ip:tcp-connection-factory id="client"
                                   type="client"
                                   host="<ip>"
                                   serializer="CustomSerializerDeserializer"
                                   deserializer="CustomSerializerDeserializer"
                                   port="6100"
                                   single-use="false" />
    <int-ip:tcp-outbound-gateway id="outGateway"
                                 request-channel="input"
                                 reply-channel="serverBytes2StringChannel"
                                 connection-factory="client" />

编辑:无法获取应用程序的DEBUG日志,因为我在Maven项目中使用TCP客户端实现作为Maven模块。另一个模块使用它作为依赖项,这是REST API端点所在的位置。

1 个答案:

答案 0 :(得分:3)

我认为你的

 <int:service-activator input-channel="toSA"
                       ref="echoService"
                       method="recieveData"/>

不会将任何结果发送到replyChannel标头,同时您的gateway.send()不是void方法。这就是它等待永远不会回复的回复。