下面列出了io.netty.channel.socket.nio.NioSocketChannel#pipeline ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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());
}
@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));
}
@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());
}
};
}
@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() {
}
};
}
});
}
};
}
@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());
}
};
}
@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);
}
}
});
}
};
}
@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);
}
};
}
@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());
}
};
}