0

I'm using

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

Given this code to generate dynamically TCP connections. I define the ReveiverAdapter and 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();

When I try to reroute to the correct channel the first time I run it I get "fromTcp" instead of the sender adapter, when I recover the channel from IntegrationFlow.getInputChannel()

Reyfren
  • 51
  • 1
  • 6

1 Answers1

1

I think you have somewhere one more subscriber for that bme.fromTcp(), so in the flow you get one more with the .enrichHeaders() and I guess that fromTcp is a DirectChannel which is based on the RoundRobinLoadBalancingStrategy by default, so incoming messages are balanced between your subscribers.

Not sure what is your intention, but consider do not inject that bme.fromTcp() into the TcpReceivingChannelAdapter and Java DSL will take care about implicit channel injection in betweed from(adapter) and .enrichHeaders().

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • I don't understand what you mean when you say that with .enrichHeaders() I get more suscribers. What I want to do with .enrichHeaders() is to add a header called "connectionId" when I send a message through of the directChannel (fromTcp). Could you give me any advice how to do that or do it differently? – Reyfren Dec 20 '18 at 08:44