【问题标题】:Returning Response from TCP Inbound Gateway从 TCP 入站网关返回响应
【发布时间】:2019-05-02 20:44:50
【问题描述】:

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

当我运行测试时,消息会成功发送到服务激活器。我知道这一点是因为服务激活器会在返回消息负载之前打印它。我也知道服务激活器将消息放在正确的通道上,因为我在该通道上有一个拦截器,它也打印消息。

问题似乎是网关没有读取该通道,即使我将其设置为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}]

【问题讨论】:

  • 这不是TCP客户端的问题吗?所以,你在那里使用TcpSendingMessageHandler,而不是TcpOutboundGateway。我们有没有机会从你那里得到一个简单的项目来玩和复制?
  • 啊哈,你是对的。我以为问题出在服务器上,但实际上是在客户端上。服务端网关成功发回消息,但客户端没有 TcpListener 来接收。

标签: spring-integration


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-01-24
    • 2021-06-06
    • 1970-01-01
    • 2022-10-25
    • 2012-11-12
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 2014-04-13
    相关资源
    最近更新 更多