Backend Development 8 min read

Netty Overall Process: Service Startup, Connection Establishment, Read/Write, and Shutdown

This article provides a detailed English overview of Netty's complete server lifecycle, covering service startup, connection establishment, data read/write handling, business logic processing, and the procedures for closing connections and shutting down the service.

Architect
Architect
Architect
Netty Overall Process: Service Startup, Connection Establishment, Read/Write, and Shutdown

Netty is a high‑performance asynchronous networking framework for Java. This article explains the full lifecycle of a Netty server, from startup through connection handling, data processing, and eventual shutdown.

Overall Process

The Netty service flow can be divided into service startup, connection establishment, data reading, business processing, data sending, connection closure, and service shutdown. The diagram below (not shown) illustrates these steps.

Service Startup

Using the EchoServer example, the startup sequence involves creating a selector, initializing a server socket channel, registering it with a boss NioEventLoop , binding the address, and registering OP_READ events.

EchoServer#new NioEventLoopGroup(1) -> NioEventLoop#provider.openSelector() : create selector.

EchoServer#b.bind(PORT).sync -> AbstractBootStrap#doBind() -> initAndRegister() -> channelFactory.newChannel() / init(channel) : create and initialize ServerSocketChannel .

... -> config().group().register(channel) : register the channel with a boss NioEventLoop .

... -> AbstractChannel#register0(promise) -> AbstractNioChannel#javaChannel().register(eventLoop().unwrappedSelector(), 0, this) : register the server socket channel with the selector.

... -> AbstractChannel#doBind(localAddress) -> NioServerSocketChannel#javaChannel().bind(localAddress, config.getBacklog()) : bind address and start listening.

... -> AbstractChannel#pipeline.fireChannelActive() -> AbstractNioChannel#selectionKey.interestOps(interestOps|readInterestOp) : register OP_READ event.

Connection Establishment

After the server starts, the boss NioEventLoop accepts incoming connections, creates a socket channel, registers it with a worker NioEventLoop , and registers OP_ACCEPT events.

NioEventLoop#run() -> processSelectedKey() : selector polls for OP_ACCEPT.

... -> NioServerSocketChannel#doReadMessages() -> SocketUtil#accept(serverSocketChannel) : create socket channel.

... -> childGroup.register(child) : register socket channel with a worker event loop.

... -> AbstractChannel#register0(promise) -> AbstractNioChannel#javaChannel().register(eventLoop().unwrappedSelector(), 0, this) : register socket channel with selector.

... -> AbstractChannel#pipeline.fireChannelActive() -> AbstractNioChannel#selectionKey.interestOps(interestOps | readInterestOp) : register OP_ACCEPT.

Read/Write and Business Processing

When a connection is established, the worker NioEventLoop handles read events, forwards the data to the pipeline, and invokes the user‑defined handler (e.g., EchoServerHandler ) for business logic. The handler then writes and flushes the response back to the client.

NioEventLoop#run() -> processSelectedKey() : selector polls for OP_READ.

... -> AbstractNioByteChannel#read() : read data from the socket.

... -> pipeline.fireChannelRead(byteBuf) : forward data to business handler.

AbstractNioByteChannel#pipeline.fireChannelRead -> EchoServerHandler#channelRead : execute handler logic.

EchoServerHandler#write -> ChannelOutboundBuffer#addMessage : enqueue write.

EchoServerHandler#flush -> ChannelOutboundBuffer#addFlush : prepare flush.

EchoServerHandler#flush -> NioSocketChannel#doWrite : send data.

Connection Closure

When a channel reads a byte count less than zero, Netty initiates a graceful shutdown of the socket channel, cancels the selection key, and clears pending messages.

NioEventLoop#run() -> processSelectedKey() : selector detects read event.

... -> AbstractNioByteChannel#read() -> closeOnRead(pipeline) : trigger close when bytes < 0.

... -> AbstractChannel#close -> AbstractNioChannel#doClose() : close socket channel.

... -> outboundBuffer.failFlushed/close : fail pending writes.

... -> fireChannelInactiveAndDeregister -> AbstractNioChannel#doDeregister -> eventLoop().cancel(selectionKey()) : cancel selector key.

Service Shutdown

Shutting down the entire Netty server involves closing all channels, cancelling scheduled tasks, and finally closing the selector.

NioEventLoop#run -> closeAll() -> selectionKey.cancel / channel.close : close channels and cancel keys.

NioEventLoop#run -> confirmShutdown -> cancelScheduledTasks : cancel timers.

NioEventLoop#cleanup -> selector.close() : close selector.

With these steps, the Netty service lifecycle is complete.

backendjavaNettyNetworkingEventLoopAsyncIO
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.