即使设置了ip_connectionId也无法找到出站套接字

时间:2019-07-12 14:29:59

标签: spring-integration spring-cloud-stream

我正在开发一个通过TCP接收请求并将其发布到事件队列的应用程序。然后,它从该事件队列接收响应,并通过正确的TCP连接将响应发送回去。该应用在运行时可以运行,但是我正在尝试编写一个测试以模拟响应流。

配置:

    @Autowired
    private TcpToQueueHandler tcpToQueueHandler;

    @Autowired
    private QueueToTcpHandler queueToTcpHandler;

    // Main config

    @Bean
    public IntegrationFlow tcpToQueueFlow() {
        return IntegrationFlows
                .from(Tcp.inboundAdapter(tcpServerFactory()))
                .handle(tcpToQueueHandler, "handleTcpIn")
                .route("headers['replyChannel']")
                .get();
    }

    @Bean
    public IntegrationFlow queueToTcpFlow() {
        return IntegrationFlows
                .from(Processor.INPUT)
                .handle(queueToTcpHandler, "handleQueueIn")
                .route("headers['replyChannel']")
                .get();
    }

    @Bean
    @ServiceActivator(inputChannel = "tcpOut")
    public TcpSendingMessageHandler tcpOutboundAdapter() {
        TcpSendingMessageHandler adapter = new TcpSendingMessageHandler();
        adapter.setConnectionFactory(tcpServerFactory());
        return adapter;
    }

    @Bean
    public TcpNetServerConnectionFactory tcpServerFactory() {
        TcpNetServerConnectionFactory serverFactory = new TcpNetServerConnectionFactory(6060);
        serverFactory.setSerializer(TcpCodecs.lengthHeader2());
        serverFactory.setDeserializer(TcpCodecs.lengthHeader2());
        return serverFactory;
    }

    // Test config

    @Bean
    public IntegrationFlow tcpResponseFlow() {
        return IntegrationFlows
                .from(Tcp.inboundAdapter(tcpClientFactory()))
                .channel(MessageChannels
                        .queue("responseChannel")
                        .get())
                .get();
    }

    @Bean
    TcpNetClientConnectionFactory tcpClientFactory() {
        TcpNetClientConnectionFactory tcpClientFactory = new TcpNetClientConnectionFactory("localhost", 6060);
        tcpClientFactory.setSerializer(TcpCodecs.lengthHeader2());
        tcpClientFactory.setDeserializer(TcpCodecs.lengthHeader2());
        return tcpClientFactory;
    }

TcpToQueueHandler:

    public Message<String> handleTcpIn(Message<String> message) {
        String payload = message.getPayload();
        String replyChannel;

        try {
            isValid(payload); // throws Exception if payload is invalid
            replyChannel = Processor.OUTPUT;
        } catch (Exception e) {
            System.out.println("Invalid payload, sending to nullChannel");
            replyChannel = "nullChannel";
        }

        return MessageBuilder
                .withPayload(payload)
                .setHeader("replyChannel", replyChannel)
                .build();
    }

QueueToTcpHandler:

    public Message<String> handleQueueIn(Message<String> messageIn) {
        String payload = messageIn.getPayload();
        String replyChannel;

        try {
            isValid(payload); // throws Exception if payload is invalid
            replyChannel = "tcpOut";
        } catch (Exception e) {
            System.out.println("Invalid payload, sending to nullChannel");
            replyChannel = "nullChannel";
        }

        return MessageBuilder
                .withPayload(payload)
                .setHeader("replyChannel", replyChannel)
                .build();
    }

测试:

    @Autowired
    private TcpNetClientConnectionFactory tcpClientFactory;

    @Autowired
    private Processor processor;

    @Autowired
    @Qualifier("responseChannel")
    private QueueChannel responseChannel;

    @Test
    @SuppressWarnings("unchecked")
    public void validPayloadSentToTcpConnection() throws Exception {
        Message<String> requestMessage = MessageBuilder
                .withPayload("This is a valid payload")
                .build();
        tcpClientFactory.start();
        tcpClientFactory.getConnection().send(requestMessage);

        Message<String> responseMessage = MessageBuilder
                .withPayload(requestMessage.getPayload())
                .setHeader(IpHeaders.CONNECTION_ID, tcpClientFactory.getConnection().getConnectionId())
                .build();

        processor.input().send(responseMessage);

        Message<String> received = (Message<String>) responseChannel.receive(2000);
        assertNotNull(received);
    }

消息正确路由到tcpOut通道,但是TcpSendingMessageHandler引发出站套接字错误。我很困惑,因为我正在设置ip_connectionId。我想念什么?

1 个答案:

答案 0 :(得分:2)

所以问题确实出在您的连接ID上。您设置的不正确。 TcpNetClientConnectionFactoryTcpNetServerConnectionFactory都有自己的可用连接列表。 但是,相同的连接将具有不同的ID,例如。 localhost:6060:55690:6855ca59-4c09-4f4c-9c3b-98415f60c276(客户端)和localhost:55690:6060:ae60f5a3-f797-4315-99f9-b35905faaaf9(服务器)。 您要求在客户端建立连接,在QueueToTcpHandler中到达的消息上设置ID,然后尝试使用服务器上不存在的连接通过TCP发送该消息。

我在存储库上创建了一个PR,因此您需要进行一些更改才能使其正常工作。

希望可以解决您遇到的问题! :)