disposing of a tcp server

6 views
Skip to first unread message

Assaf Abulafia

unread,
Jun 4, 2018, 6:51:04 AM6/4/18
to reactor-framework
Hi,

I trying to stop a tcp server running on some port.
I'm calling the dispose method on the netty context,
which stop listening on the port,
but established connections are not killed when the server stops.

I can keep track of all contexts returned by handler,
and when the server stops dispose them as well.
Is this the proper way of doing this, or should dispose also shutdown existing connections?

I'm using reactor netty 0.7.7

thanks,
Assaf


// create the server
BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> serverHandler = (in, out) -> {
                in.receive()
                        .asString()
                        .subscribe(data -> processMessage(data));

                NettyContext context = out.context();
                openConnections.add(context);

                return out.neverComplete();
            };

            nettyServer = TcpServer.create(opts -> {
                opts.afterChannelInit(c -> c.pipeline()
                        .addBefore(
                                NettyPipeline.ReactiveBridge,
                                "codec",
                                new LineBasedFrameDecoder(maxMessageLength)))
                        .onChannelInit(new Predicate<Channel>() {
                            @Override
                            public boolean test(Channel channel) {
                                logger.info("new connection from {}", channel.remoteAddress());
                                return false;
                            }
                        })
                        .port(port);
            })
                    .newHandler(serverHandler)
                    .block();





// stop method
if (nettyServer != null && nettyServer.isDisposed()) {
  nettyServer.dispose();

  for (NettyContext context : openConnections) {
    context.dispose();
  }
  nettyServer = null;
}
Reply all
Reply to author
Forward
0 new messages