Search in sources :

Example 11 with ReadTimeoutHandler

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);
}
Also used : ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) ClientChannelHandler(org.apache.camel.component.netty4.handlers.ClientChannelHandler) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) ClientChannelHandler(org.apache.camel.component.netty4.handlers.ClientChannelHandler)

Example 12 with ReadTimeoutHandler

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();
}
Also used : GetReferencesRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetReferencesRequestEncoder) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) GetSegmentRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetSegmentRequestEncoder) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) StringEncoder(io.netty.handler.codec.string.StringEncoder) ResponseDecoder(org.apache.jackrabbit.oak.segment.standby.codec.ResponseDecoder) GetBlobRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetBlobRequestEncoder) GetHeadRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetHeadRequestEncoder) Bootstrap(io.netty.bootstrap.Bootstrap) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) SnappyFramedDecoder(io.netty.handler.codec.compression.SnappyFramedDecoder) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) SslContext(io.netty.handler.ssl.SslContext)

Example 13 with ReadTimeoutHandler

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));
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpClientChannelHandler(org.apache.camel.component.netty4.http.handlers.HttpClientChannelHandler) ChannelHandlerFactory(org.apache.camel.component.netty4.ChannelHandlerFactory) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) HttpClientChannelHandler(org.apache.camel.component.netty4.http.handlers.HttpClientChannelHandler) ChannelHandler(io.netty.channel.ChannelHandler) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 14 with ReadTimeoutHandler

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);
            }
        });
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) PingHandler(com.github.jvm.io.protocol.c2d.heart.PingHandler) InetSocketAddress(java.net.InetSocketAddress) C2DHessianMsgDecoder(com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgDecoder) C2DHessianMsgEncoder(com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgEncoder) LoginAuthReqHandler(com.github.jvm.io.protocol.c2d.auth.LoginAuthReqHandler) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 15 with ReadTimeoutHandler

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");
}
Also used : ProtobufVarint32LengthFieldPrepender(io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) ProtobufVarint32FrameDecoder(io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder)

Aggregations

ReadTimeoutHandler (io.netty.handler.timeout.ReadTimeoutHandler)26 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)7 ChannelHandler (io.netty.channel.ChannelHandler)6 ChannelPipeline (io.netty.channel.ChannelPipeline)6 LoggingHandler (io.netty.handler.logging.LoggingHandler)6 TimeUnit (java.util.concurrent.TimeUnit)6 Bootstrap (io.netty.bootstrap.Bootstrap)5 SocketChannel (io.netty.channel.socket.SocketChannel)5 Channel (io.netty.channel.Channel)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)4 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)4 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Connection (reactor.netty.Connection)4 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)3 ByteBuf (io.netty.buffer.ByteBuf)3 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)3 SslContext (io.netty.handler.ssl.SslContext)3 SSLException (javax.net.ssl.SSLException)3