Java Code Examples for io.netty.channel.socket.nio.NioSocketChannel#pipeline()

The following examples show how to use io.netty.channel.socket.nio.NioSocketChannel#pipeline() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BackPoolHandler.java    From api-gateway-core with Apache License 2.0 6 votes vote down vote up
@Override
    public void channelCreated(Channel ch) throws Exception {
        logger.debug("channelCreated. Channel ID: {}", ch.id());
        NioSocketChannel channel = (NioSocketChannel) ch;
        channel.config().setKeepAlive(true);
        channel.config().setTcpNoDelay(true);
        ChannelPipeline pipeline = channel.pipeline();
        if (sslCtx != null) {
            pipeline.addLast(sslCtx.newHandler(ch.alloc()));
        }
        pipeline.addLast(new HttpClientCodec());
        pipeline.addLast(new HttpContentDecompressor());
        pipeline.addLast(new HttpObjectAggregator(1024 * 1024 * 64));
        pipeline.addLast(new ChunkedWriteHandler());
//        pipeline.addLast(new ReadTimeoutHandler(requestHolder.route.getTimeoutInMilliseconds(), TimeUnit.MILLISECONDS));
        pipeline.addLast(new BackHandler());
    }
 
Example 2
Source File: HttpClientChannelPoolHandler.java    From protools with Apache License 2.0 6 votes vote down vote up
@Override
public void channelCreated(Channel channel) {

    NioSocketChannel nioSocketChannel = (NioSocketChannel) channel;
    nioSocketChannel.config().setTcpNoDelay(true).setKeepAlive(true);

    final ChannelPipeline p = nioSocketChannel.pipeline();

    //HTTPS
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(channel.alloc()));
    }

    p.addLast(new HttpClientCodec(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE));

    p.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
}
 
Example 3
Source File: GpbTcpServer.java    From Okra with Apache License 2.0 6 votes vote down vote up
@Override
    protected ChannelHandler newChannelInitializer() {
        return new ChannelInitializer<NioSocketChannel>() {
            @Override
            protected void initChannel(NioSocketChannel ch) throws Exception {
                ChannelPipeline cp = ch.pipeline();
                cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
                cp.addLast("prepender", FRAME_PREPENDER);
                cp.addLast("decoder", GPB_DECODER_HANDLER);
                cp.addLast("encoder", GPB_ENCODER_HANDLER);
                // handler
                cp.addLast("handler", serverHandler);
//                cp.addLast("handler", new ServerHandler());
            }
        };
    }
 
Example 4
Source File: PlacementServer.java    From Okra with Apache License 2.0 5 votes vote down vote up
@Override
protected ChannelHandler newChannelInitializer() {
    return new ChannelInitializer<NioSocketChannel>() {
        @Override
        protected void initChannel(NioSocketChannel ch) throws Exception {
            ChannelPipeline cp = ch.pipeline();
            cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
            cp.addLast("prepender", new LengthFieldPrepender(2, false));
            // Any other useful handler
            cp.addLast("strDecoder", STRING_DECODER);
            cp.addLast("strEncoder", STRING_ENCODER);
            cp.addLast("handler", new DisruptorAdapterBy41xHandler<JsonRequest>() {
                protected Executor newExecutor(Session session, JsonRequest msg) {
                    return new Executor() {
                        @Override
                        @SuppressWarnings("unchecked")
                        public void onExecute() {
                            try {
                                Command command = serviceManager.getCommand(msg.getApi());
                                if (command == null) {
                                    LOG.warn("Command is not exist. API : ", msg.getApi());
                                    return;
                                }
                                command.execute(session, msg);
                            } catch (Exception e) {
                                LOG.error("Error occured by execute command.", e);
                            }
                        }

                        @Override
                        public void release() {

                        }
                    };
                }
            });
        }
    };
}
 
Example 5
Source File: TcpServer.java    From Okra with Apache License 2.0 5 votes vote down vote up
@Override
protected ChannelHandler newChannelInitializer() {
    return new ChannelInitializer<NioSocketChannel>() {
        @Override
        protected void initChannel(NioSocketChannel ch) throws Exception {
            ChannelPipeline cp = ch.pipeline();
            cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
            cp.addLast("prepender", FRAME_PREPENDER);
            // Any other useful handler
            cp.addLast("handler", new ExampleSocketHandler());
        }
    };
}
 
Example 6
Source File: BenchmarkClient.java    From Okra with Apache License 2.0 5 votes vote down vote up
@Override
protected ChannelHandler newChannelInitializer() {
    return new ChannelInitializer<NioSocketChannel>() {
        @Override
        protected void initChannel(NioSocketChannel ch) throws Exception {
            ChannelPipeline cp = ch.pipeline();
            cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
            cp.addLast("prepender", FRAME_PREPENDER);
            // Any other useful handler
            cp.addLast("strDecoder", STRING_DECODER);
            cp.addLast("strEncoder", STRING_ENCODER);
            cp.addLast("handler", new SimpleChannelInboundHandler<String>() {
                @Override
                protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
                    COUNT.getAndIncrement();
                    ChannelPromise voidPromise = ctx.voidPromise();
                    if (ctx.channel().isWritable()) {
                        ctx.writeAndFlush(msg, voidPromise);
                    } else {
                        ctx.channel().eventLoop().schedule(() -> {
                            ctx.writeAndFlush(msg, voidPromise);
                        }, 1L, TimeUnit.SECONDS);
                    }
                }
            });
        }
    };
}
 
Example 7
Source File: BenchmarkServer.java    From Okra with Apache License 2.0 5 votes vote down vote up
@Override
protected ChannelHandler newChannelInitializer() {
    return new ChannelInitializer<NioSocketChannel>() {
            @Override
            protected void initChannel(NioSocketChannel ch) throws Exception {
                ChannelPipeline cp = ch.pipeline();
                cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
                cp.addLast("prepender", FRAME_PREPENDER);
                // Any other useful handler
                cp.addLast("strDecoder", STRING_DECODER);
                cp.addLast("strEncoder", STRING_ENCODER);
                cp.addLast("handler", HANDLER);
            }
    };
}
 
Example 8
Source File: HttpServer.java    From Okra with Apache License 2.0 5 votes vote down vote up
@Override
protected ChannelHandler newChannelInitializer() {
    return new ChannelInitializer<NioSocketChannel>() {
        @Override
        protected void initChannel(NioSocketChannel ch) throws Exception {
            ChannelPipeline cp = ch.pipeline();
            cp.addLast("decoder", new HttpRequestDecoder());
            cp.addLast("encoder", new HttpResponseEncoder());
            cp.addLast("aggregator", new HttpObjectAggregator(1048576));
            cp.addLast("handler", new ExampleApiHandler());
        }
    };
}