title: Configure Dynamic TCP Server to send message to the client connected Spring Integration using Java DSL
body: <p>I am trying to create a Integration flow : </p>
  
  <p><code>Connection A :  [ ActiveMq (queue1) ---&gt; TCP Server (1111) ](spring boot Application)  ---&gt; [ExternalApplication (client connected to Server running on port (1111))] (Application on different Technology (VB))</code></p>
  
  <p><code>Connection B :  [ ActiveMq (queue2) ---&gt; TCP Server (2222) ](spring boot Application)  ---&gt; [ExternalApplication (client connected to Server running on port (2222))] (Application on different Technology (VB))</code></p>
  
  <p>So Above flow depicts following scenario:</p>
  
  <ol>
  <li><p>I have a spring boot application (say springUtility) which connect with external application(say externalutility) on some other technology.</p></li>
  <li><p>The connection between springutility and externalUtility is through TCP protocol.</p></li>
  <li><p>There will be more than one connection between both the utilities as in above flow diagram connection A and connection B.</p></li>
  <li><p>The flow of message will be from springUtilty to externalutility (One way)</p></li>
  <li><p>Now, the externalutility can run in client Mode or server mode. so, if the externalUtility run in server Mode then my springutility will be in client mode (Done) also to create more than one connection I run this below code in for loop code : </p></li>
  </ol>
  
  <p>Code:</p>
  
  <pre><code>flow = IntegrationFlows
              .from(Jms.inboundAdapter(ConnectionFactory())
                      .destination("rec"))
                      .channel(directChannel()).handle(Tcp.outboundAdapter(Tcp.nioClient("172.18.11.22",5555)
                               .serializer(customSerializer)
                               .deserializer(customSerializer)
                               .id(hostConnection.getConnectionNumber()).soTimeout(10000)))
                              .get();
  
      flowRegistration = this.flowContext.registration(flow).id("in.flow").register();
  </code></pre>
  
  <ol start="6">
  <li><p>Issue : When the externalutility run in client mode then spring utility should able to create a server on which the external utility will get connected and then it start receiving messsages from spring utility:
  which I tried to implement in a wrong way.</p></li>
  <li><p>So, finally I have to Implement the above connection as a test case to validate the flow,
  means spring utility has opened server port (1111 and 2222) the external application connect with these two port and start receiving the message from spring utility from queue1 and queue2 repectively. Both the connection will have there seperate flow (Once a single flow will be created then we can use a for loop to create no of connection we need).</p></li>
  </ol>
  
  <p>Code:</p>
  
  <pre><code>flow = IntegrationFlows
              .from(Jms.inboundAdapter(ConnectionFactory())
                      .destination("rec"))
                      .channel(directChannel()).handle(Tcp.outboundAdapter(Tcp.netServer(5555)
                               .serializer(customSerializer)
                               .deserializer(customSerializer)
                               .id(hostConnection.getConnectionNumber()).soTimeout(10000)))
                              .get();
  
      flowRegistration = this.flowContext.registration(flow).id("in.flow").register();
  </code></pre>
  
  <p>`
  code:</p>
  
  <pre><code>2018-05-30 17:39:39.171  INFO 15776 --- [nio-8080-exec-1] o.s.i.endpoint.EventDrivenConsumer       : Adding {jms:outbound-channel-adapter} as a subscriber to the 'Conn1311out.flow.channel#0' channel
  2018-05-30 17:39:39.172  INFO 15776 --- [nio-8080-exec-1] o.s.integration.channel.DirectChannel    : Channel 'application.Conn1311out.flow.channel#0' has 1 subscriber(s).
  2018-05-30 17:39:39.172  INFO 15776 --- [nio-8080-exec-1] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#3
  2018-05-30 17:39:41.965  INFO 15776 --- [nio-8080-exec-1] o.s.i.endpoint.EventDrivenConsumer       : Adding {ip:tcp-outbound-channel-adapter} as a subscriber to the 'Conn1311in.flow.channel#0' channel
  2018-05-30 17:39:41.965  INFO 15776 --- [nio-8080-exec-1] o.s.integration.channel.DirectChannel    : Channel 'application.Conn1311in.flow.channel#0' has 1 subscriber(s).
  2018-05-30 17:39:41.966  INFO 15776 --- [nio-8080-exec-1] .s.i.i.t.c.TcpNetServerConnectionFactory : started Conn1311, port=3333
  2018-05-30 17:39:41.966  INFO 15776 --- [nio-8080-exec-1] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#4
  2018-05-30 17:39:41.966  INFO 15776 --- [pool-1-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : Conn1311, port=3333 No listener bound to server connection factory; will not read; exiting...
  2018-05-30 17:39:41.971  INFO 15776 --- [nio-8080-exec-1] o.s.i.e.SourcePollingChannelAdapter      : started org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean#0
  </code></pre>
  
  <p>It first create the server on the provided port but exit with following message</p>
  
  <p><code>No listener bound to server connection factory; will not read; exiting...</code></p>
  
  <p>When I am creating the client it is working fine but in case of creating server it is giving error</p>
  
  <p>EDIT:</p>
  
  <p>I have use the following approach to create dynamically different server and send message to connected client from its respective activemq client</p>
  
  <ol>
  <li><p>I have created an inbound adapter as below : </p>
  
  <pre><code>IntegrationFlow flow;
  CustomSerializer customSerializer = getCustomSerializer(String.valueOf(hostConnection.getMaxMessageLength()),
          hostConnection.getTerminatorChar());
  
  TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
  TcpNetServerConnectionFactory cf = new TcpNetServerConnectionFactory(1234));
  cf.setSerializer(customSerializer);
  cf.setDeserializer(customSerializer);
  handler.setConnectionFactory(cf);
  
       flow = IntegrationFlows
          .from(Tcp.inboundAdapter(cf).id("adapter")).handle(handler)
          .get();
  
       this.flowContext.registration(flow).id("inflow")
       .addBean(hostConnection.getConnectionNumber(),cf)
       .addBean(hostConnection.getConnectionNumber()+"handler",handler)
       .register();
  </code></pre></li>
  <li><p>I have created a Jmsflow in which the message from the queue is send to a router as below:</p>
  
  <pre><code>    flow = IntegrationFlows
          .from(Jms.inboundAdapter(activeMQConnectionFactory)
          .destination(hostConnection.getConnectionNumber() +"rec")) 
          .channel(directChannel()).route(new ServerRouter())
          .get(); 
  </code></pre></li>
  <li><p>when the client got connected to my server then I create a flow from the listener <code>handleTcpConnectionCloseEvent(TcpConnectionOpenEvent event)</code> and create a flow as:</p>
  
  <pre><code>void createServerFlow(TcpConnectionOpenEvent event) {
      String connectionNumber = event.getConnectionFactoryName();
      TcpNetConnection server = (TcpNetConnection) event.getSource();
      TcpSendingMessageHandler handler = (TcpSendingMessageHandler) ac.getBean(connectionNumber + "handler");
  
      IntegrationFlow flow = f -&gt; f.enrichHeaders(e -&gt; e.header(IpHeaders.CONNECTION_ID, event.getConnectionId()))
              .handle(handler);
      IntegrationFlowRegistration flowregister = this.flowContext.registration(flow).id("outclient").register();
  
      MessageChannel channel = flowregister.getInputChannel();
  
      this.subFlows.put(connectionNumber + "server", channel);
  }
  </code></pre></li>
  <li><p>My ServerRouter find the channel and send to the target channel</p>
  
  <pre><code>protected Collection&lt;MessageChannel&gt; determineTargetChannels(Message&lt;?&gt; message) {
  String serverChannel = (String) message.getHeaders().get("connectionnumber");
  MessageChannel channel = HostConnectionRepository.subFlows
          .get(serverChannel+"server");
  
  return Collections.singletonList(channel);
  </code></pre>
  
  <p>}</p></li>
  <li><p>when I got disconnected then I remove the flow from flowContext created in <code>step 3</code>.</p></li>
  </ol>
  
  <p><strong>Issue :</strong> 
  When I remove the flow the bean associated with the flow also get removed as its written in the docs that related beans will also discard.</p>
  
  <p>This remove my handler bean which close the TCP adapter also the connectionfactory hence once it get disconnected it can't connect again</p>
  
  <p>How can i discard the outclientflow without discarding the handler bean?</p>