pipeline.addLast("channelGroupHandler", new ChannelInboundHandlerAdapter() { public void channelActive(ChannelHandlerContext ctx) { ... } public void channelInactive(ChannelHandlerContext ctx) { ... } });
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {}
public void testCloseForcibly(ServerBootstrap sb, Bootstrap cb) throws Throwable { sb.handler(new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { SocketChannel childChannel = (SocketChannel) msg; childChannel.config().setSoLinger(0); childChannel.unsafe().closeForcibly(); } }).childHandler(new ChannelInboundHandlerAdapter()); cb.handler(new ChannelInboundHandlerAdapter()); Channel sc = sb.bind().sync().channel(); cb.connect(sc.localAddress()).channel().closeFuture().syncUninterruptibly(); sc.close().sync(); } }
private void testShutdownSoLingerNoAssertError0(Bootstrap cb, boolean output) throws Throwable { ServerSocket ss = new ServerSocket(); Socket s = null; ChannelFuture cf = null; try { ss.bind(newSocketAddress()); cf = cb.option(ChannelOption.SO_LINGER, 1).handler(new ChannelInboundHandlerAdapter()) .connect(ss.getLocalSocketAddress()).sync(); s = ss.accept(); cf.sync(); if (output) { ((SocketChannel) cf.channel()).shutdownOutput().sync(); } else { ((SocketChannel) cf.channel()).shutdown().sync(); } } finally { if (s != null) { s.close(); } if (cf != null) { cf.channel().close(); } ss.close(); } } private static void checkThrowable(Throwable cause) throws Throwable {
private static EmbeddedChannel createChannel() { return new EmbeddedChannel(new ChannelInboundHandlerAdapter()); }
Channel cc = null; try { sb.childHandler(new ChannelInboundHandlerAdapter()); sc = sb.bind().syncUninterruptibly().channel(); cb.handler(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception {
public void testMultipleConnect(ServerBootstrap sb, Bootstrap cb) throws Exception { Channel sc = null; Channel cc = null; try { sb.childHandler(new ChannelInboundHandlerAdapter()); sc = sb.bind(NetUtil.LOCALHOST, 0).syncUninterruptibly().channel(); cb.handler(new ChannelInboundHandlerAdapter()); cc = cb.register().syncUninterruptibly().channel(); cc.connect(sc.localAddress()).syncUninterruptibly(); ChannelFuture connectFuture2 = cc.connect(sc.localAddress()).await(); assertTrue(connectFuture2.cause() instanceof AlreadyConnectedException); } finally { if (cc != null) { cc.close(); } if (sc != null) { sc.close(); } } }
final Bootstrap bootstrap = new Bootstrap().handler(new ChannelInboundHandlerAdapter()); if (MetronClientBuilder.this.eventLoopGroup == null) { eventLoopGroup = new NioEventLoopGroup(1);
ch.pipeline() .addLast( new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) {
cb.group(serverGroup); cb.channel(clientChannel()); cb.handler(new ChannelInboundHandlerAdapter()); Channel clientChannel = cb.connect(serverChannel.localAddress()).syncUninterruptibly().channel(); ByteBuf buf = clientChannel.alloc().buffer(expectedBytes);
@Override protected void initChannel(Channel ch) { ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) {
public void testLocalAddressAfterConnect(ServerBootstrap sb, Bootstrap cb) throws Throwable { Channel serverChannel = null; Channel clientChannel = null; try { final Promise<InetSocketAddress> localAddressPromise = ImmediateEventExecutor.INSTANCE.newPromise(); serverChannel = sb.childHandler(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { localAddressPromise.setSuccess((InetSocketAddress) ctx.channel().localAddress()); } }).bind().syncUninterruptibly().channel(); clientChannel = cb.handler(new ChannelInboundHandlerAdapter()).register().syncUninterruptibly().channel(); assertNull(clientChannel.localAddress()); assertNull(clientChannel.remoteAddress()); clientChannel.connect(serverChannel.localAddress()).syncUninterruptibly().channel(); assertLocalAddress((InetSocketAddress) clientChannel.localAddress()); assertNotNull(clientChannel.remoteAddress()); assertLocalAddress(localAddressPromise.get()); } finally { if (clientChannel != null) { clientChannel.close().syncUninterruptibly(); } if (serverChannel != null) { serverChannel.close().syncUninterruptibly(); } } }
public void testShutdownNotYetConnected(Bootstrap cb) throws Throwable { SocketChannel ch = (SocketChannel) cb.handler(new ChannelInboundHandlerAdapter()) .bind(newSocketAddress()).syncUninterruptibly().channel(); try { try { ch.shutdownInput().syncUninterruptibly(); fail(); } catch (Throwable cause) { checkThrowable(cause); } try { ch.shutdownOutput().syncUninterruptibly(); fail(); } catch (Throwable cause) { checkThrowable(cause); } } finally { ch.close().syncUninterruptibly(); } }