This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Before: | |
ServerBootstrap b = new ServerBootstrap(); | |
try { | |
b.eventLoop(new NioEventLoop(), new NioEventLoop()); // <-- HERE | |
... | |
} finally { b.shutdown(); } | |
// After: | |
ServerBootstrap b = new ServerBootstrap(); | |
try { |