DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Build a REST API With Just 2 Classes in Java and Quarkus
  • Dropwizard vs. Micronaut: Unpacking the Best Framework for Microservices
  • How Stalactite ORM Implements Its Fluent DSL

Trending

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • A Modern Stack for Building Scalable Systems
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • Unlocking AI Coding Assistants Part 4: Generate Spring Boot Application
  1. DZone
  2. Coding
  3. Frameworks
  4. Building a Performant Application Using Netty Framework in Java

Building a Performant Application Using Netty Framework in Java

Netty is an asynchronous, event-driven networking framework for building high-performance applications. Dive deep into Netty's various components.

By 
Prasanna J user avatar
Prasanna J
·
Mar. 19, 24 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
11.2K Views

Join the DZone community and get the full member experience.

Join For Free

Netty is a powerful, asynchronous, event-driven networking framework for building high-performance, scalable applications. It simplifies the development of network applications by providing an easy-to-use API and robust abstractions for handling various networking protocols and data formats.

Key features and benefits of Netty include:

  1. Asynchronous and event-driven: Netty utilizes a non-blocking, event-driven architecture that allows it to handle thousands of concurrent connections with low latency. This makes it ideal for building high-performance applications that require scalability.
  2. Modular and extensible: Netty is designed with a modular architecture that allows developers to customize and extend its functionality easily. It provides a rich set of components, known as "handlers," that can be combined to build complex networking pipelines tailored to specific use cases.
  3. Protocol agnostic: Netty abstracts away the complexities of various networking protocols, making it easy to develop applications that communicate using protocols such as HTTP, HTTPS, WebSocket, TCP, UDP, and more.
  4. Byte buffers and zero-copy: Netty uses efficient byte buffer abstractions and supports zero-copy mechanisms. Zero-copy is a feature currently available only with NIO and Epoll transport. It allows you to quickly and efficiently move data from a file system to the network without copying from kernel space to user space, which can significantly improve performance in protocols such as FTP or HTTP. Specifically, it is not usable with file systems that implement data encryption or compression util file system already has encrypted data.
  5. Support for SSL/TLS: Netty provides built-in support for SSL/TLS encryption, allowing developers to secure their network communications with ease.

Components of Netty

1. ChannelInitializer

ChannelInitializer is an abstract class in Netty used for initializing a new Channel. When a new connection is accepted by the server, Netty creates a new Channel, and the ChannelInitializer is invoked to set up the initial configuration for this Channel. This typically involves adding handlers to the ChannelPipeline. childHandler() method in the ServerBootstrap class is used to specify the ChannelInitializer for the child channels.

2. ChannelPipeline

ChannelPipeline represents a sequence of channel handlers that process inbound and outbound data for a Channel. When a message (such as a byte buffer) travels through a Channel, it passes through the pipeline, where each handler can process or modify the message as needed. The output of one handler becomes input to the next handler. The pipeline can be dynamically changed in the handler by acquiring handlerContext. 

3. ChannelHandler

ChannelHandler is an interface in Netty that defines the behavior of components that can be added to a ChannelPipeline to process inbound and outbound data. Handlers can perform various tasks such as encoding/decoding data, handling protocol-specific logic, performing business logic, and managing the lifecycle of the Channel. There are different types of ChannelHandler interfaces in Netty, such as ChannelInboundHandler for handling inbound data, ChannelOutboundHandler for handling outbound data, and ChannelDuplexHandler for handling both inbound and outbound data. Netty Provides many abstract handlers to hook up to one of the events. 

ChannelHandler

4. ChannelHandlerContext

ChannelHandlerContext represents the context in which a ChannelHandler is invoked within a ChannelPipeline. It provides access to the Channel, the ChannelHandler itself, and various operations for interacting with the pipeline. ChannelHandlerContext allows handlers to send messages downstream in the pipeline, forward events to other handlers, modify the pipeline dynamically (e.g., add/remove handlers), and manage the lifecycle of the Channel. When a ChannelHandler is added to a ChannelPipeline, it’s assigned a ChannelHandlerContext, which represents the binding between a ChannelHandler and the ChannelPipeline. Although this object can be used to obtain the underlying Channel, it’s mostly utilized to write outbound data. There are two ways of sending messages in Netty. You can write directly to the Channel or write to a ChannelHandlerContext object associated with a ChannelHandler. The former approach causes the message to start from the tail of the ChannelPipeline, the latter causes the message to start from the next handler in the ChannelPipeline.

ChannelPipeline

5. EventLoopGroup

A group of event loops used by Netty to handle I/O operations such as accepting incoming connections, reading from sockets, and writing to sockets. It manages a pool of event loops, each of which runs on a separate thread and processes I/O events for one or more channels. Provides methods to create, manage, and shut down event loops. There are two distinct EventLoopGroup instances used for different purposes in a server:

  1. Boss EventLoopGroup: This group is responsible for accepting incoming connections on the server's listening socket. Less number of threads should be sufficient for this group usually 1, as all it does is accept a connection and hand it over to the group.
  2. Worker EventLoopGroup: This group handles the actual processing of accepted connections. More number of thread needs to be allocated to this group, depending on processor capacity.

Some of the implementations are NioEventLoopGroup for non-blocking I/O using NIO, OioEventLoopGroup for blocking I/O using old I/O, and epollEventLoopGroup specific to Linux.

6. EventLoop

EventLoop is the heart of Netty's asynchronous event-driven architecture. It represents a single-threaded event loop that processes I/O events for one or more channels. Each EventLoop runs on its own thread and executes tasks in a loop, such as accepting connections, reading data from sockets, writing data to sockets, and executing user-defined tasks. One EventLoop is used throughout lifecycle of the Channel, avoiding the need for synchronization. Handles the lifecycle of channels, including registration, deregistration, and closing. Manages a queue of tasks (also known as the task queue) and executes them one by one in the order they were submitted.

7. Channel

The Channel abstraction hides the underlying complexities of different transport protocols (e.g., TCP, UDP) and network sockets. It provides a unified interface for performing I/O operations regardless of the transport protocol being used. NioServerSocketChannel is a specific implementation of the Channel interface provided by Netty, used for server-side TCP socket communication based on the Java NIO (New I/O) framework. It uses a selectable channel, meaning it can be registered with a selector to receive notification of I/O events such as incoming connection requests. This allows the server to efficiently manage multiple channels using a single thread. Provides various configuration options for setting up the socket, such as setting the receive buffer size, send buffer size, and socket timeout.

In addition to this, Netty provides various other channel implementations for different types of communication protocols and use cases. 

Some of the commonly used channel implementations include

  1. NioSocketChannel: Represents a client-side TCP socket channel based on the Java NIO framework. It is used for establishing outgoing connections to remote servers.
  2. LocalServerChannel: Represents a server-side local (in-process) communication channel. It is used for handling local connections within the same JVM process.
  3. LocalChannel: Represents a client-side local (in-process) communication channel. It is used for establishing outgoing local connections within the same JVM process.
  4. EpollServerSocketChannel: Represents a server-side TCP socket channel optimized for Linux systems using the epoll mechanism for I/O event multiplexing.
  5. EpollSocketChannel: Represents a client-side TCP socket channel optimized for Linux systems using the epoll mechanism.
  6. KQueueServerSocketChannel: Represents a server-side TCP socket channel optimized for BSD-based systems (such as macOS) using the kqueue mechanism for I/O event multiplexing.
  7. KQueueSocketChannel: Represents a client-side TCP socket channel optimized for BSD-based systems using the kqueue mechanism.
  8. DatagramChannel: Represents a UDP (User Datagram Protocol) channel for both server-side and client-side communication.

These are just a few examples of channel implementations provided by Netty. Netty offers a wide range of channel implementations to support various communication protocols, performance optimizations, and platform-specific features. Developers can choose the appropriate channel implementation based on their specific requirements and deployment environments.

Let us write a simple Server that echos back the received message.

Java
 
@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
 @Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) {
 ByteBuf in = (ByteBuf) msg;
 ctx.write(in);
 }
 @Override
 public void channelReadComplete(ChannelHandlerContext ctx) {
 ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
 .addListener(ChannelFutureListener.CLOSE);
 }
 @Override
 public void exceptionCaught(ChannelHandlerContext ctx,
 Throwable cause) {
 cause.printStackTrace();
 ctx.close();
 }
}


Server Bootstrap :

Java
 
public class EchoServer {

 public static void main(String[] args) throws Exception {
 new EchoServer().start();
 }
 public void start() throws Exception {
 final EchoServerHandler serverHandler = new EchoServerHandler();
 EventLoopGroup bossGroup = new NioEventLoopGroup(1); // Typically fewer threads for boss
 EventLoopGroup workerGroup = new NioEventLoopGroup(4);
 try {
 ServerBootstrap b = new ServerBootstrap();
 b.group(bossGroup, workerGroup)
 .channel(NioServerSocketChannel.class)
 .localAddress(new InetSocketAddress(8089))
 .childHandler(new ChannelInitializer<SocketChannel>(){
 @Override
 public void initChannel(SocketChannel ch)
 throws Exception {
 ch.pipeline().addLast(serverHandler);
 }
 });
 ChannelFuture f = b.bind().sync();
 f.channel().closeFuture().sync();
 } finally {
 bossGroup.shutdownGracefully().sync();
 workerGroup.shutdownGracefully().sync();
 }
 }
}


Writing an Echo Client which prints received messages to the console.

Java
 
@Sharable
public class EchoClientHandler extends
 SimpleChannelInboundHandler<ByteBuf> {
 @Override
 public void channelActive(ChannelHandlerContext ctx) {
 ctx.writeAndFlush(Unpooled.copiedBuffer("Netty Client!",
 CharsetUtil.UTF_8);
 }
 @Override
 public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) {
 System.out.println(
 "Client received: " + in.toString(CharsetUtil.UTF_8));
 }
 @Override
 public void exceptionCaught(ChannelHandlerContext ctx,
 Throwable cause) {
 cause.printStrackTrace();
 ctx.close();
 }
}


Client Bootstrap :

Java
 
public class EchoClient {
 private final String host;
 private final int port;
 public EchoClient(String host, int port) {
 this.host = host;
 this.port = port;
 }
 public void start() throws Exception {
 EventLoopGroup group = new NioEventLoopGroup();
 try {
 Bootstrap b = new Bootstrap();
 b.group(group)
 .channel(NioSocketChannel.class)
 .remoteAddress(new InetSocketAddress(host, port))
 .handler(new ChannelInitializer<SocketChannel>() {
 @Override
 public void initChannel(SocketChannel ch)
 throws Exception {
 ch.pipeline().addLast(
 new EchoClientHandler());
 }
 });
 ChannelFuture f = b.connect().sync();
 f.channel().closeFuture().sync();
 } finally {
 group.shutdownGracefully().sync();
 }
 }


All the operations in Netty are async, which returns ChannelFuture or ChannelPromise. ChannelFuture can be supplied with a callback method, which will be executed once the async operation is completed.

Resource Management

Whenever you act on data by calling ChannelInboundHandler.channelRead() or ChannelOutboundHandler.write(), you need to ensure that there are no resource leaks. Netty uses reference counting to handle pooled ByteBufs. So it’s important to adjust the reference count after you have finished using a ByteBuf. Netty ByteBuf is different from NIO ByteBuffer. In Netty read and write operations have 2 different pointers, particular pointers move forward corresponding to specific operations. This eliminates the need to call flip() ByteBuff while reading data.

Netty provides class ResourceLeakDetector, which will sample about 1% of your application’s buffer allocations to check for memory leaks. Set the below property to enable sampling.

java -Dio.netty.leakDetectionLevel=ADVANCED

To summarize, Netty empowers developers to build high-performance and scalable networked applications with ease, leveraging its rich feature set, flexible architecture, and extensive ecosystem of libraries and tools.

Framework Java (programming language) Netty (software)

Opinions expressed by DZone contributors are their own.

Related

  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Build a REST API With Just 2 Classes in Java and Quarkus
  • Dropwizard vs. Micronaut: Unpacking the Best Framework for Microservices
  • How Stalactite ORM Implements Its Fluent DSL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!