Created
February 8, 2017 14:12
-
-
Save errandir/af45f3559421e3d12ace556af3e4999b to your computer and use it in GitHub Desktop.
Netty 4.1.8.Final @sharable CombinedChannelDuplexHandler usage problem demonstration
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
import io.netty.bootstrap.Bootstrap; | |
import io.netty.bootstrap.ServerBootstrap; | |
import io.netty.buffer.ByteBuf; | |
import io.netty.channel.*; | |
import io.netty.channel.ChannelHandler.Sharable; | |
import io.netty.channel.nio.NioEventLoopGroup; | |
import io.netty.channel.socket.SocketChannel; | |
import io.netty.channel.socket.nio.NioServerSocketChannel; | |
import io.netty.channel.socket.nio.NioSocketChannel; | |
import io.netty.handler.codec.ByteToMessageCodec; | |
import java.util.List; | |
import static java.lang.Integer.parseInt; | |
public class NettySharableCombinedChannelDuplexHandlerProblemDemonstration { | |
public static void main(String[] args) throws InterruptedException { | |
int port = args.length > 0 ? parseInt(args[0]) : 63889; | |
initServer(port); | |
Channel client1 = newClientChannel(port); | |
client1.writeAndFlush("[1-1]").syncUninterruptibly(); | |
Channel client2 = newClientChannel(port); | |
client2.writeAndFlush("[2-1]").syncUninterruptibly(); | |
client1.writeAndFlush("[1-2]").syncUninterruptibly(); | |
client2.writeAndFlush("[2-2]").syncUninterruptibly(); | |
client1.writeAndFlush("[1-3]").syncUninterruptibly(); | |
client2.writeAndFlush("[2-3]").syncUninterruptibly(); | |
client1.writeAndFlush("[1-4]").syncUninterruptibly(); | |
client2.writeAndFlush("[2-4]").syncUninterruptibly(); | |
Thread.sleep(100); | |
System.exit(0); | |
} | |
static void initServer(int port) { | |
ProblematicHandler problematicHandler = new ProblematicHandler(); | |
new ServerBootstrap() | |
.group(new NioEventLoopGroup(), new NioEventLoopGroup()) | |
.channel(NioServerSocketChannel.class) | |
.childHandler(new ChannelInitializer<SocketChannel>() { | |
protected void initChannel(SocketChannel ch) { | |
ch.pipeline() | |
.addLast("codec", new StringCodec()) | |
.addLast("problematic", problematicHandler); | |
} | |
}) | |
.bind(port) | |
.syncUninterruptibly(); | |
} | |
static Channel newClientChannel(int port) { | |
return new Bootstrap() | |
.group(new NioEventLoopGroup()) | |
.channel(NioSocketChannel.class) | |
.handler(new StringCodec()) | |
.connect("localhost", port) | |
.syncUninterruptibly() | |
.channel(); | |
} | |
} | |
class StringCodec extends ByteToMessageCodec<String> { | |
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { | |
byte[] bytes = new byte[in.readableBytes()]; | |
in.readBytes(bytes); | |
out.add(new String(bytes)); | |
} | |
protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) { | |
out.writeBytes(msg.getBytes()); | |
} | |
} | |
@Sharable | |
class ProblematicHandler extends CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> { | |
ProblematicHandler() { | |
super( | |
new SimpleChannelInboundHandler<String>() { | |
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { | |
System.out.println(ctx + ": " + msg); | |
} | |
}, | |
new ChannelOutboundHandlerAdapter()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment