春天当路由inputChannel第一通道总是错误的

时间:2018-12-19 15:02:23

标签: spring-integration spring-integration-dsl

我正在使用

  • spring-integration-java-dsl-1.2.3.RELEASE
  • spring-integration-ip-4.3.17.RELEASE
  • spring-integration-http-4.3.17.RELEASE

给出此代码以动态生成TCP连接。我定义了ReveiverAdapter和SenderAdapter。

IntegrationFlow ifr = existsConnection(connectionId);
    if (ifr == null) {
        TcpNetClientConnectionFactory cf = new TcpNetClientConnectionFactory(host, port);
        final ByteArrayLengthHeaderSerializer by = new ByteArrayLengthHeaderSerializer(headBytes);
        cf.setSingleUse(false);
        cf.setSoKeepAlive(true);
        cf.setSerializer(by);
        cf.setDeserializer(by);
        cf.setComponentName(connectionId);

        //Inbound Adapter 
        TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
        adapter.setConnectionFactory(cf);
        adapter.setClientMode(true);
        adapter.setErrorChannelName("errorChannel");
        adapter.setRetryInterval(retryInterval);
        adapter.setOutputChannel(bme.fromTcp());
        //OutBound Adapter
        TcpSendingMessageHandler sender = new TcpSendingMessageHandler();
        sender.setConnectionFactory(cf);

        ifr = IntegrationFlows.from(adapter)
                .enrichHeaders(h -> h.header("connectionId",connectionId))
                .handle(sender).get();

        this.flowContext.registration(ifr).id(connectionId+CONNECTION_SUFFIX).addBean(cf).register();

当我第一次尝试将其重新路由到正确的频道时,当我从IntegrationFlow.getInputChannel()恢复频道时,会收到“ fromTcp”而不是发送者适配器

1 个答案:

答案 0 :(得分:1)

我认为您为此bme.fromTcp()还有一个订户,因此在流程中您可以通过.enrichHeaders()获得一个订户,我想fromTcpDirectChannel默认情况下基于RoundRobinLoadBalancingStrategy,因此传入消息在订阅者之间得到平衡。

不确定您的意图是什么,但请考虑不要将bme.fromTcp()注入TcpReceivingChannelAdapter中,Java DSL将注意在from(adapter)和{{1}中隐式注入信道}。