Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty closeFuture().sync().channel(); blocks rest api

I am in the middle of learning Netty an started some tutorials using spring boot. My goal is to create an application which set up a tcp port for receiving messages and to present them over a rest api.

Most of the tutorials are saying that I should add something like this

serverChannel = serverBootstrap.bind(tcpPort).sync().channel().closeFuture().sync().channel();

to start netty. When I do that, the rest services which I implemented are not working. Now when I use the following code snippet to start the application:

serverChannel = serverBootstrap.bind(tcpPort).sync().channel();

everything seems to be working just fine. Could someone explain me what might cause this issue?

Thanks

like image 980
SBK Avatar asked Jan 06 '17 12:01

SBK


People also ask

What is Channel in Netty?

Netty Channel Channel is a component providing users a ways to process I/O operations, such as read and write. A ChannelPipeline encapsulates a series of ChannelHandler instances as two-way linked list. Inbound and outbound events that flow through a channel can be intercepted by the ChannelPipeline.

Why do we use Netty?

Netty allows you to write your own network protocol tailored to your specific requirements, optimizing the traffic flow for your specific situation, without the unnecessary overhead of something like HTTP or FTP.

How do I disable Netty server?

when you use netty as client, use f.channel(). closeFuture(). sync() ,but when you use it as server, you should use f.channel(). close().

How does Netty works?

Netty uses an event-driven application paradigm, so the pipeline of the data processing is a chain of events going through handlers. Events and handlers can be related to the inbound and outbound data flow. Inbound events can be the following: Channel activation and deactivation.


1 Answers

The first part start the server, 1) binding it on a TCP port, 2) wait for the server to be ready (socket is listening) 3) and return the associated channel.

serverBootstrap.bind(tcpPort).sync().channel();
                 (1)           (2)       (3)

The second part is to wait for the main channel (listening socket) to shutdown (closeFuture().sync()) where closeFuture gives you the "future" on "close" operation (meaning shutdown of the server socket), and sync waiting for this future to be done. channel() gives you back the very same channel than first time, except it is now closed.

So you might find this code in various example because generally you start the server (bind) in the main thread or so, and then if you don't wait for something, the main thread will end up, giving your JVM finishing, and therefore your server to stop immediately after starting.

So in general, what we do is:

  • start the server
  • add in the pipeline the necessary handlers to handle your business logic (and the network protocol of course)
  • then finish your main by waiting on closeFuture, such that, once in your business logic you get the order to shutdown, you close the main channel, and therefore your main thread is closing too.

See for instance Shutdown netty programmatically

like image 150
Frederic Brégier Avatar answered Oct 10 '22 11:10

Frederic Brégier