- Netty / Vert.x / All things NIO
- Author of Netty in Action
- @normanmaurer
- github.com/normanmaurer
Netty is a NIO client server framework which enable quick and easy development of network applications such as protocol servers and clients…
Netty Website
Don’t call us, we’ll call you.
Hollywood principle
Usually a Thread takes memory from 256kb to 1mb for the stack space! Also a lot of wasted resources… |
hhttp://www.flickr.com/photos/bibi/5204647994/
Channel channel = ...
ChannelFuture cf = channel.writeAndFlush(data); (1)
cf.addListener(new ChannelFutureListener() { (2)
@Override
public void operationComplete(ChannelFuture future) {
if(!future.isSuccess() {
future.cause().printStacktrace();
} else { ... }
}
});
1 | Write to the Channel and flush |
2 | Add ChannelFutureListener to be notified once operations completes |
ByteBuf buf = ...;
buf.writeInt(1).writeBytes(data).writeBoolean(true)...
Pooling pays off for direct and heap buffers! |
https://blog.twitter.com/2013/netty-4-at-twitter-reduced-gc-overhead
@Sharable
public class EchoHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) { (1)
ctx.writeAndFlush(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { (2)
cause.printStacktrace();
ctx.close();
}
}
1 | Intercept received message and write it back to the remote peer |
2 | React on Throwable and close the connection |
Kind of a unix-pipe-like thing…
$ echo "Netty is shit...." | sed -e 's/is /is the /'
Netty is the shit....
You see, everything is adjustable! |
No need to worry about synchronization |
http://www.flickr.com/photos/za3tooor/65911648/
public class WriteTimeOutHandler extends ChannelOutboundHandlerAdapter {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
ctx.write(msg, promise);
if (!promise.isDone() {
ctx.executor().schedule(new WriteTimeoutTask(promise), 30, TimeUnit.SECONDS); (1)
}
}
}
1 | Schedule task for in 30 seconds |
A case study for using Netty based on Vert.x!
Used in Vert.x as callback for outbound operations
public interface Handler<E> {
void handle(E event);
}
Used in Netty as callback for outbound operations.
public interface ChannelFutureListener {
void operationComplete(ChannelFuture future) throws Exception;
}
public interface ChannelOutboundHandler extends ChannelHandler {
void write(ChannelHandlerContext ctx, Object msg,
ChannelPromise promise) throws Exception;
...
}
public class HandlerAdapter<AsyncResult<T>> implements ChannelFutureListener {
public HandlerAdapter(Handler<AsyncResult<T>> handler, T okResult) { ... }
@Override
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
handler.handle(new DefaultFutureResult<T>(okResult));
} else {
handler.handle(new DefaultFutureResult<T>(future.cause()));
}
}
}
public DefaultNetSocket implements NetSocket {
@Override
public void sendFile(String name, Handler<AsyncResult<NetSocket> callback) {
channel.writeAndFlush(createFileRegion(name)).addListener(
new HandlerAdapter(callback, this));
}
}
Used in Vert.x as callback for inbound events
public interface Handler<E> {
void handle(E event);
}
Used in Netty as callback for inbound events.
public interface ChannelInboundHandler extends ChannelHandler {
void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception;
void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception;
...
public class InboundHandlerAdapter<T> extends ChannelInboundHandlerAdapter {
public InboundHandlerAdapter(Handler<T> dataHandler,
Handler<Throwable> exceptionHandler) { ... }
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
handler.handle((T) msg);
}
@Override
public void exceptionCaught(ChannelHandlerCtx ctx, Throwable cause) {
exceptionHandler.handle(cause);
}
}
… and many more … |
http://www.flickr.com/photos/legofenris/4499417549/
Buy my book Netty in Action and make me RICH. |
http://www.manning.com/maurer
$ KA-CHING $
Netty - http://netty.io |
Slides generated with Asciidoctor and DZSlides backend |
Original slide template - Dan Allen & Sarah White |
All pictures licensed with Creative Commons Attribution orCreative Commons Attribution-Share Alike |