Network - Application development the easy way
https://github.com/netty/netty
@netty_project
Netty is a NIO client server framework which enable quick and easy development of network applications such as protocol servers and clients...
@!# WTF, why should I care ?
© http://www.flickr.com/photos/56223083@N06/5514161603/
As waiting is b0ring....
But synchronous / blocking IO served me well! Again, why should I care ?
Usually a Thread takes memory from 256kb to 1mb for the stack space!
Ouch....
© http://www.flickr.com/photos/djjasoncook/8608405432/
© http://www.flickr.com/photos/brizzlebornandbred/6136653327/
Channel channel = ...
ChannelFuture cf = channel.writeAndFlush(data);
cf.addListener(
new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future)
throws Exception {
if(!future.isSuccess() {
future.cause().printStacktrace();
...
}
...
}
});
...
// or block and wait for completion if you really need!
cf.sync();
And more to come ....
ByteBuf == ByteBuffer on steroids!
© http://www.flickr.com/photos/lightmash/3183278318/
ByteBuf buf = ...;
buf.writeInt(1).writeBytes(data).writeBoolean(true)...
© https://blog.twitter.com/2013/netty-4-at-twitter-reduced-gc-overhead
Pooling or not pooling....
Users building blocks!
© http://www.flickr.com/photos/bdesham/2432400623/
Your business logic needs a place, and this is where it belongs too
We have a lot of them!
Makes your life easier
@Sharable
public class Echo4ServerHandler extends
ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx,
Object msg) {
ctx.writeAndFlush(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx,
Throwable cause) {
cause.printStacktrace();
ctx.close();
}
}
© http://www.flickr.com/photos/rickz/2113212191/
$ echo "Netty is shit...." | sed -e 's/is /is the /' Netty is the shit....
You see, everything is adjustable!
Channel ch = ...;
ChannelPipeline p = ch.pipeline();
EventExecutor e1 = new DefaultEventExecutor(16);
EventExecutor e2 = new DefaultEventExecutor(8);
// Executed in EventLoop Thread
p.addLast(new MyProtocolCodec());
// Executed in one of the Threads of e1
p.addLast(e1, new MyDatabaseAccessingHandler());
// Executed in one of the Threads of e1
p.addLast(e2, new MyHardDiskAccessingHandler());
© http://www.flickr.com/photos/stavos52093/9645884201/
All Tasks executed in Event-Loop!
No need to worry about synchronization
© http://www.flickr.com/photos/vegaseddie/5700609302/
© http://www.flickr.com/photos/disowned/1159135004/
I promise to never block the Event-Loop!
All the ScheduleExecutorService goodies for free!
public class MyHandler extends
ChannelOutboundHandlerAdapter {
@Override
public void write(ChannelHandlerContext ctx,
Object msg, ChannelPromise promise) {
ctx.write(msg, promise);
if (!promise.isDone() {
// schedule task for in 30 seconds
ctx.executor().schedule(new MyWriteTimeoutTask(
promise), 30, TimeUnit.SECONDS);
}
// Run an arbitrary task from an I/O thread.
ctx.executor().execute(new Runnable() { ... });
}
}
It's time to accept some real traffic
© http://www.flickr.com/photos/postbear/4500894615/
ServerBootstrap b = new ServerBootstrap();
EventLoopGroup group = new NioEventLoopGroup(numThreads);
try {
b.group(group, group))
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new YourChannelHandler());
}
});
ChannelFuture f = b.bind().sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
Bootstrap b = new Bootstrap();
EventLoopGroup group = new NioEventLoopGroup(numThreads);
try {
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress("10.0.0.1", 25))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new YourChannelHandler());
}
});
ChannelFuture f = b.connect().sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
And many more...
And again many more....
© http://www.flickr.com/photos/legofenris/4499417549/
You may like Vert.x which is based on Netty!
You may want to buy my book...
http://www.manning.com/maurer/
© http://www.flickr.com/photos/photomequickbooth/3531951972/