Skip to content

Instantly share code, notes, and snippets.

@jchambers
Last active December 28, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchambers/7487099 to your computer and use it in GitHub Desktop.
Save jchambers/7487099 to your computer and use it in GitHub Desktop.
A simple Netty test app to see which handler methods are fired when a read is attempted.
public class ChannelReadTestApp {
private static final int PORT = 9852;
public static void main(String[] args) throws InterruptedException {
final WriteAndIdleServer server = new WriteAndIdleServer(PORT);
final ReadAndIdleClient client = new ReadAndIdleClient(PORT);
server.start();
client.start();
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
System.out.println("Requesting read.");
client.attemptRead();
}
System.out.println("Shutting down.");
client.shutdown();
server.shutdown();
}
}
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.util.Date;
public class ReadAndIdleClient {
private final int port;
private Bootstrap bootstrap;
private Channel channel;
private class ReadAndIdleHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf m = (ByteBuf) msg;
try {
System.out.println(String.format("Read timestamp: %s",
new Date(m.readLong())));
} finally {
m.release();
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
System.out.println("Read complete.");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
System.out.println(String.format("Caught an exception: %s", cause));
}
}
public ReadAndIdleClient(final int port) {
this.port = port;
}
public void start() throws InterruptedException {
this.bootstrap = new Bootstrap();
this.bootstrap.group(new NioEventLoopGroup());
this.bootstrap.channel(NioSocketChannel.class);
this.bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
this.bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ReadAndIdleHandler());
}
});
final ChannelFuture connectFuture = this.bootstrap.connect("localhost",
this.port);
connectFuture.sync();
this.channel = connectFuture.channel();
}
public void attemptRead() {
this.channel.read();
}
public void shutdown() throws InterruptedException {
this.channel.close().sync();
this.bootstrap.group().shutdownGracefully().sync();
}
}
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class WriteAndIdleServer {
private final int port;
private ServerBootstrap bootstrap;
private class WriteAndIdleHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(final ChannelHandlerContext ctx) {
final ByteBuf time = ctx.alloc().buffer(8);
time.writeLong(System.currentTimeMillis());
ctx.writeAndFlush(time);
}
}
public WriteAndIdleServer(final int port) {
this.port = port;
}
public void start() throws InterruptedException {
this.bootstrap = new ServerBootstrap();
this.bootstrap.group(new NioEventLoopGroup());
this.bootstrap.channel(NioServerSocketChannel.class);
this.bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new WriteAndIdleHandler());
}
});
this.bootstrap.option(ChannelOption.SO_BACKLOG, 128);
this.bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
this.bootstrap.bind(this.port).sync();
}
public void shutdown() throws InterruptedException {
this.bootstrap.group().shutdownGracefully().sync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment