Skip to content

Instantly share code, notes, and snippets.

View trustin's full-sized avatar
🌄
▂▃▅▇█▓▒░۩۞۩░▒▓█▇▅▃▂

Trustin Lee trustin

🌄
▂▃▅▇█▓▒░۩۞۩░▒▓█▇▅▃▂
View GitHub Profile
public class MyHandler extends ChannelOutboundMessageHandlerAdapter {
...
public void flush(ChannelHandlerContext ctx, ChannelFuture f) {
...
ctx.flush(f);
// Schedule a write timeout.
ctx.executor().schedule(new MyWriteTimeoutTask(), 30, TimeUnit.SECONDS);
...
}
java.nio.channels.FileChannel myFile = ...;
java.nio.channels.SocketChannel mySocket = java.nio.channels.SocketChannel.open();
// Perform some blocking operation here.
...
// Netty takes over.
SocketChannel ch = new NioSocketChannel(mySocket);
EventLoopGroup group = ...;
group.register(ch);
public static void main(String[] args) throws Exception {
// Configure the server.
ServerBootstrap b = new ServerBootstrap();
try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(new NioServerSocketChannel())
.option(ChannelOption.SO_BACKLOG, 100)
.localAddress(8080)
.childOption(ChannelOption.TCP_NODELAY, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
ChannelConfig cfg = ...;
// Before:
cfg.setOption("tcpNoDelay", true);
cfg.setOption("tcpNoDelay", 0); // Runtime ClassCastException
cfg.setOption("tcpNoDelays", true); // Typo in the option name - ignored silently
// After:
cfg.setOption(ChannelOption.TCP_NODELAY, true);
cfg.setOption(ChannelOption.TCP_NODELAY, 0); // Compile error
private void sendNumbers() {
// Do not send more than 4096 numbers.
boolean finished = false;
MessageBuf<Object> out = ctx.nextOutboundMessageBuffer();
while (out.size() < 4096) {
if (i <= count) {
out.add(Integer.valueOf(i));
i ++;
} else {
finished = true;
public class MyHandler extends ChannelInboundMessageHandlerAdapter<MyMessage> {
private static final AttributeKey<MyState> STATE =
new AttributeKey<MyState>("MyHandler.state");
@Override
public void channelRegistered(ChannelHandlerContext ctx) {
ctx.attr(STATE).set(new MyState());
ctx.fireChannelRegistered();
}
Channel ch = ...;
ChannelPipeline p = ch.pipeline();
EventExecutor e1 = new DefaultEventExecutor(16);
EventExecutor e2 = new DefaultEventExecutor(8);
p.addLast(new MyProtocolCodec());
p.addLast(e1, new MyDatabaseAccessingHandler());
p.addLast(e2, new MyHardDiskAccessingHandler());
public void inboundBufferUpdated(ChannelHandlerContext ctx) {
Queue<MyMessage> in = ctx.inboundMessageBuffer();
Queue<MyNewMessage> out = ctx.nextInboundMessageBuffer();
for (;;) {
MyMessage m = in.poll();
if (m == null) {
break;
}
MyNewMessage decoded = decode(m);
out.add(decoded);
// No more dynamicBuffer() - use buffer().
ByteBuf buf = ByteBuf.buffer();
// Increase the capacity of the buffer.
buf.capacity(1024);
...
// Decrease the capacity of the buffer (the last 512 bytes are deleted.)
buf.capacity(512);
// Before:
ServerBootstrap b = new ServerBootstrap();
try {
b.eventLoop(new NioEventLoop(), new NioEventLoop()); // <-- HERE
...
} finally { b.shutdown(); }
// After:
ServerBootstrap b = new ServerBootstrap();
try {