use of io.netty.handler.timeout.ReadTimeoutHandler in project camel by apache.
the class DefaultClientInitializerFactory method initChannel.
protected void initChannel(Channel ch) throws Exception {
// create a new pipeline
ChannelPipeline channelPipeline = ch.pipeline();
SslHandler sslHandler = configureClientSSLOnDemand();
if (sslHandler != null) {
//TODO must close on SSL exception
//sslHandler.setCloseOnSSLException(true);
LOG.debug("Client SSL handler configured and added to the ChannelPipeline: {}", sslHandler);
addToPipeline("ssl", channelPipeline, sslHandler);
}
List<ChannelHandler> decoders = producer.getConfiguration().getDecoders();
for (int x = 0; x < decoders.size(); x++) {
ChannelHandler decoder = decoders.get(x);
if (decoder instanceof ChannelHandlerFactory) {
// use the factory to create a new instance of the channel as it may not be shareable
decoder = ((ChannelHandlerFactory) decoder).newChannelHandler();
}
addToPipeline("decoder-" + x, channelPipeline, decoder);
}
List<ChannelHandler> encoders = producer.getConfiguration().getEncoders();
for (int x = 0; x < encoders.size(); x++) {
ChannelHandler encoder = encoders.get(x);
if (encoder instanceof ChannelHandlerFactory) {
// use the factory to create a new instance of the channel as it may not be shareable
encoder = ((ChannelHandlerFactory) encoder).newChannelHandler();
}
addToPipeline("encoder-" + x, channelPipeline, encoder);
}
// do we use request timeout?
if (producer.getConfiguration().getRequestTimeout() > 0) {
if (LOG.isTraceEnabled()) {
LOG.trace("Using request timeout {} millis", producer.getConfiguration().getRequestTimeout());
}
ChannelHandler timeout = new ReadTimeoutHandler(producer.getConfiguration().getRequestTimeout(), TimeUnit.MILLISECONDS);
addToPipeline("timeout", channelPipeline, timeout);
}
// our handler must be added last
addToPipeline("handler", channelPipeline, new ClientChannelHandler(producer));
LOG.trace("Created ChannelPipeline: {}", channelPipeline);
}
use of io.netty.handler.timeout.ReadTimeoutHandler in project jackrabbit-oak by apache.
the class StandbyClient method connect.
void connect(String host, int port) throws Exception {
final SslContext sslContext;
if (secure) {
sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
} else {
sslContext = null;
}
Bootstrap b = new Bootstrap().group(group).channel(NioSocketChannel.class).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, readTimeoutMs).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (sslContext != null) {
p.addLast(sslContext.newHandler(ch.alloc()));
}
p.addLast(new ReadTimeoutHandler(readTimeoutMs, TimeUnit.MILLISECONDS));
// Decoders
p.addLast(new SnappyFramedDecoder(true));
// Such a big max frame length is needed because blob
// values are sent in one big message. In future
// versions of the protocol, sending binaries in chunks
// should be considered instead.
p.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
p.addLast(new ResponseDecoder());
// Encoders
p.addLast(new StringEncoder(CharsetUtil.UTF_8));
p.addLast(new GetHeadRequestEncoder());
p.addLast(new GetSegmentRequestEncoder());
p.addLast(new GetBlobRequestEncoder());
p.addLast(new GetReferencesRequestEncoder());
// Handlers
p.addLast(new GetHeadResponseHandler(headQueue));
p.addLast(new GetSegmentResponseHandler(segmentQueue));
p.addLast(new GetBlobResponseHandler(blobQueue));
p.addLast(new GetReferencesResponseHandler(referencesQueue));
// Exception handler
p.addLast(new ExceptionHandler(clientId));
}
});
channel = b.connect(host, port).sync().channel();
}
use of io.netty.handler.timeout.ReadTimeoutHandler in project camel by apache.
the class HttpClientInitializerFactory method initChannel.
@Override
protected void initChannel(Channel ch) throws Exception {
// create a new pipeline
ChannelPipeline pipeline = ch.pipeline();
SslHandler sslHandler = configureClientSSLOnDemand();
if (sslHandler != null) {
//TODO must close on SSL exception
//sslHandler.setCloseOnSSLException(true);
LOG.debug("Client SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
pipeline.addLast("ssl", sslHandler);
}
pipeline.addLast("http", new HttpClientCodec());
List<ChannelHandler> encoders = producer.getConfiguration().getEncoders();
for (int x = 0; x < encoders.size(); x++) {
ChannelHandler encoder = encoders.get(x);
if (encoder instanceof ChannelHandlerFactory) {
// use the factory to create a new instance of the channel as it may not be shareable
encoder = ((ChannelHandlerFactory) encoder).newChannelHandler();
}
pipeline.addLast("encoder-" + x, encoder);
}
List<ChannelHandler> decoders = producer.getConfiguration().getDecoders();
for (int x = 0; x < decoders.size(); x++) {
ChannelHandler decoder = decoders.get(x);
if (decoder instanceof ChannelHandlerFactory) {
// use the factory to create a new instance of the channel as it may not be shareable
decoder = ((ChannelHandlerFactory) decoder).newChannelHandler();
}
pipeline.addLast("decoder-" + x, decoder);
}
pipeline.addLast("aggregator", new HttpObjectAggregator(configuration.getChunkedMaxContentLength()));
if (producer.getConfiguration().getRequestTimeout() > 0) {
if (LOG.isTraceEnabled()) {
LOG.trace("Using request timeout {} millis", producer.getConfiguration().getRequestTimeout());
}
ChannelHandler timeout = new ReadTimeoutHandler(producer.getConfiguration().getRequestTimeout(), TimeUnit.MILLISECONDS);
pipeline.addLast("timeout", timeout);
}
// handler to route Camel messages
pipeline.addLast("handler", new HttpClientChannelHandler(producer));
}
use of io.netty.handler.timeout.ReadTimeoutHandler in project jdepth by Crab2died.
the class C2DClient method connect.
public void connect(String remoteHost, int remotePort, String localeHost, int localPort) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, Boolean.TRUE).handler(new LoggingHandler(LogLevel.WARN)).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new C2DHessianMsgDecoder(1024 * 1024, 4, 4, -8, 0)).addLast("MessageEncoder", new C2DHessianMsgEncoder()).addLast("ReadTimeoutHandler", new ReadTimeoutHandler(TIME_OUT)).addLast("LoginAuthReq", new LoginAuthReqHandler()).addLast("Ping", new PingHandler());
}
});
// 发起异步连接操作
ChannelFuture future = bootstrap.connect(new InetSocketAddress(remoteHost, remotePort), new InetSocketAddress(localeHost, localPort)).sync();
// 当对应的channel关闭的时候,就会返回对应的channel。
future.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
// 所有资源释放完成之后,清空资源,再次发起重连操作
executor.execute(() -> {
try {
TimeUnit.SECONDS.sleep(5);
try {
// 发起重连操作
connect(REMOTE_HOST, REMOTE_PORT, LOCAL_HOST, LOCAL_PORT);
} catch (Exception e) {
logger.error("reconnect error", e);
}
} catch (InterruptedException e) {
logger.error("InterruptedException", e);
}
});
}
}
use of io.netty.handler.timeout.ReadTimeoutHandler in project java-tron by tronprotocol.
the class Channel method init.
public void init(ChannelPipeline pipeline, String remoteId, boolean discoveryMode, ChannelManager channelManager, PeerConnectionDelegate peerDel) {
this.channelManager = channelManager;
this.remoteId = remoteId;
isActive = remoteId != null && !remoteId.isEmpty();
// TODO: use config here
pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(60, TimeUnit.SECONDS));
pipeline.addLast(stats.tcp);
pipeline.addLast("protoPender", new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast("lengthDecode", new ProtobufVarint32FrameDecoder());
// handshake first
pipeline.addLast("handshakeHandler", handshakeHandler);
this.discoveryMode = discoveryMode;
this.peerDel = peerDel;
messageCodec.setChannel(this);
msgQueue.setChannel(this);
handshakeHandler.setChannel(this, remoteId);
p2pHandler.setChannel(this);
tronHandler.setChannel(this);
p2pHandler.setMsgQueue(msgQueue);
tronHandler.setMsgQueue(msgQueue);
tronHandler.setPeerDel(peerDel);
logger.info("Channel init finished");
}
Aggregations