Netty Fundamentals
Netty is an asynchronous, event driven networking framework that we can use to develop high performance protocol clients and servers. In this post, I’m going to explain some basic netty concepts so that it will be easy for you to understand netty terms when we start to use netty framework to build our very own http web server and a client.
Channel
Think of the “Channel” as a connection to a socket/file/hardware device, basically a connection to anything that can handle I/O operations such as read and write.
ChannelFuture
All the I/O operations are asynchronous in netty, that is, we don’t have to wait for the I/O operations to complete, as it immediately returns a “ChannelFuture” object, which we could later use to register listeners to get notified of the status of I/O operations.
ChannelFuture channelFuture = bootstrap.connect();
ChannelFutureListener
Now that we have a “ChannelFuture” object we can register one or more “ChannelFutureListener” instances on it, whose methods we can use to determine whether the operation is successful or not.
channelFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (future.isSuccess() && future.isDone()) {
...
} else {
future.cause().printStackTrace();
...
}
}
});
ChannelPipeline & Netty Handlers
There are two types of data flows, inbound and outbound. When you read data from socket that’s an inbound event and when you write data to a socket that’s an outbound event. So in netty, handlers get executed in response to these inbound and outbound events and we can define our data processing logic in these handlers.
ChannelPipeline is the one that holds these handlers and each channel is assigned it’s own ChannelPipeline, when a channel is created.
These are some vary basic netty concepts to get you familiar with the terms before we get our hands dirty with coding.