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
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.
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.
when you use netty as client, use f.channel(). closeFuture(). sync() ,but when you use it as server, you should use f.channel(). close().
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.
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:
See for instance Shutdown netty programmatically
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With