从TCP入站网关返回响应

时间:2019-05-02 20:44:50

标签: spring-integration

我被困在一个看似简单的任务上,但是没有主意。我在TcpInboundGateway上附加了TcpNetServerConnectionFactory,将请求传递给服务激活器。服务激活器只是将消息放回网关的回复通道。我希望网关通过连接返回该消息。

运行测试时,该消息会将其成功发送到服务激活器。我知道这是因为Service Activator在返回消息有效载荷之前会先将其打印出来。我也知道Service Activator会将消息放在正确的通道上,因为我在该通道上有一个拦截器,该拦截器也可以打印消息。

问题似乎是,即使我在setReplyChannel()中进行了设置,网关也没有从该通道读取数据。我还可以在日志中看到这一点:

Adding {bridge:null} as a subscriber to the 'testResponseChannel' channel

这使我怀疑消息只是发送到空通道,而不是被我的网关接收。

这是配置:

    @Bean
    public TcpNetServerConnectionFactory testServerFactory() {
        TcpNetServerConnectionFactory testServerFactory = new TcpNetServerConnectionFactory(0);
        testServerFactory.setSerializer(TcpCodecs.lengthHeader2());
        testServerFactory.setDeserializer(TcpCodecs.lengthHeader2());
        return testServerFactory;
    }

    @Bean
    public DirectChannel testRequestChannel() {
        return new DirectChannel();
    }

    @Bean
    public DirectChannel testResponseChannel() {
        DirectChannel testResponseChannel = new DirectChannel();
        testResponseChannel.addInterceptor(channelInterceptor());
        return testResponseChannel;
    }

    @Bean
    public TcpInboundGateway gateway() {
        TcpInboundGateway gateway = new TcpInboundGateway();
        gateway.setConnectionFactory(testServerFactory());
        gateway.setRequestChannel(testRequestChannel());
        gateway.setReplyChannel(testResponseChannel());
        return gateway;
    }

    @Bean
    @ServiceActivator(inputChannel = "testRequestChannel", outputChannel = "testResponseChannel")
    public EchoHandler echoHandler() {
        return new EchoHandler();
    }

这是我的POJO服务激活器:

public class EchoHandler {

    public Message<String> echoMessage(Message<String> request) {
        System.out.println(request.getPayload());
        return request;
    }
}

这是错误,发生在消息通过拦截器之后:

Unexpected message - no endpoint registered with connection interceptor: localhost:6060:59848:3c6c3cff-c697-4fc9-b4e3-9ea14508cec7 - GenericMessage [payload=byte[3], headers={ip_tcp_remotePort=6060, ip_connectionId=localhost:6060:59848:3c6c3cff-c697-4fc9-b4e3-9ea14508cec7, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=93c75664-54db-c93e-ab3a-3e06b1e4b626, ip_hostname=localhost, timestamp=1556828832645}]

1 个答案:

答案 0 :(得分:1)

要对服务器的答复做出正确反应,您的客户端必须具有请求响应能力。为此,Spring Integration IP模块建议使用TcpOutboundGateway。这样,TcpListener将被注册在TcpConnection上,并准备解析和处理套接字上的回复消息。