Shutdown netty programmatically

One way is to make something like:

// once having an event in your handler (EchoServerHandler)
// Close the current channel
ctx.channel().close();
// Then close the parent channel (the one attached to the bind)
ctx.channel().parent().close();

Doing this way will end up the following:

// Wait until the server socket is closed. Thread gets blocked.
f.channel().closeFuture().sync();

No need for an extra thread on main part. Now the question is: what kind of event? It's up to you... Might be a message in the echo handler as "shutdown" that will be taken as an order of shutdown and not only "quit" which will turn as closing only the client channel. Might be something else...

If you do not handle the shutdown from a child channel (so through your handler) but through another process (for instance looking for a stop file existing), then you need an extra thread that will wait for this event and then directly make a channel.close() where channel will be the parent one (from f.channel()) for instance...

Many other solutions exist.