Search in sources :

Example 36 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project transporter by wang4ever.

the class TransportChannel method configure.

/**
 * 建立连接并初始化Nio channel
 *
 * @return this
 * @throws InterruptedException
 */
public TransportChannel configure() {
    try {
        if (this.bootstrap != null) {
            this.config.getLogger().warn("Initialized bootloader.");
            return this;
        }
        // 1.0 启动引导程序
        this.bootstrap = new Bootstrap();
        // 通过nio方式来接收连接和处理连接
        this.workerGroup = new NioEventLoopGroup();
        // 设置nio类型的channel
        this.bootstrap.group(workerGroup);
        this.bootstrap.channel(NioSocketChannel.class);
        this.bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
        // this.bootstrap.option(ChannelOption.SO_TIMEOUT,
        // this.config.soTimeout());
        this.bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.config.getConnecTimeout() * 1000);
        // 有连接到达时会创建一个channel
        this.bootstrap.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                // pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务
                ChannelPipeline p = ch.pipeline();
                if (config.isLoggingEnable() && config.getLevel().getValue() >= Level.DEBUG.getValue())
                    p.addLast(new LoggingHandler(LogLevel.valueOf(config.getLevel().name())));
                p.addLast(new IdleStateHandler(config.getReadIdleSeconds(), config.getWriteIdleSeconds(), config.getAllIdleSeconds()));
                p.addLast("decoder", new TransportMessageDecoder());
                p.addLast("encoder", new TransportMessageEncoder());
                p.addLast("receiveTextHandler", config.getHandler().newInstance());
            }
        });
    // 线程在这里开始等待,除非有socket事件唤醒
    // f.channel().closeFuture().sync();
    // 2.0 反射强制设置默认JDK日志级别
    // try {
    // // Netty JdkLogger instance
    // Field nettyJdkLogField =
    // Bootstrap.class.getDeclaredField("logger");
    // nettyJdkLogField.setAccessible(true);
    // Object nettyJdkLogObj = nettyJdkLogField.get(null);
    // // JdkLogger instance
    // Field jdkLogField =
    // nettyJdkLogObj.getClass().getDeclaredField("logger");
    // jdkLogField.setAccessible(true);
    // java.util.logging.Logger jdkLog = (java.util.logging.Logger)
    // jdkLogField.get(nettyJdkLogObj);
    // jdkLog.setLevel(Level.parse(this.config.getJdkLogLevel()));
    // } catch (Exception e) {
    // this.config.getLogger().error("设置Netty debugger日志级别失败. " +
    // e.getMessage());
    // }
    } catch (Exception e) {
        throw new TransportException("Connection channel configuration error.", e);
    }
    return this;
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) TransportMessageDecoder(io.transport.sdk.protocol.codec.TransportMessageDecoder) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Bootstrap(io.netty.bootstrap.Bootstrap) TransportMessageEncoder(io.transport.sdk.protocol.codec.TransportMessageEncoder) TransportException(io.transport.sdk.exception.TransportException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) IOException(java.io.IOException) TransportException(io.transport.sdk.exception.TransportException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 37 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project transporter by wang4ever.

the class TransportConnector method configure.

/**
 * 建立连接并初始化Nio channel
 *
 * @return this
 * @throws InterruptedException
 */
public TransportConnector configure() {
    try {
        if (this.bootstrap != null) {
            this.config.getLogger().warn("Initialized bootloader.");
            return this;
        }
        // Bootstrap program.
        this.bootstrap = new Bootstrap();
        // Receiving connections and processing connections by Nio.
        this.workerGroup = new NioEventLoopGroup();
        this.bootstrap.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.config.getConnecTimeout() * 1000);
        // this.bootstrap.option(ChannelOption.SO_TIMEOUT,
        // this.config.soTimeout());
        // When a connection arrives, a channel will be created.
        this.bootstrap.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                // pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务
                ChannelPipeline p = ch.pipeline();
                if (config.isLoggingEnable() && config.getLevel().getValue() >= Level.DEBUG.getValue())
                    p.addLast(new LoggingHandler(LogLevel.valueOf(config.getLevel().name())));
                p.addLast(new IdleStateHandler(config.getReadIdleSeconds(), config.getWriteIdleSeconds(), config.getAllIdleSeconds()));
                p.addLast("decoder", new TransportMessageDecoder(config));
                p.addLast("encoder", new TransportMessageEncoder(config));
                p.addLast("receiveTextHandler", config.getHandler().newInstance().setClient(client));
            }
        });
    // The thread begins to wait here unless there is a socket event
    // wake-up.
    // f.channel().closeFuture().sync();
    } catch (Exception e) {
        throw new TransportException("Connection channel configuration error.", e);
    }
    return this;
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) TransportMessageDecoder(io.transport.sdk.protocol.codec.TransportMessageDecoder) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Bootstrap(io.netty.bootstrap.Bootstrap) TransportMessageEncoder(io.transport.sdk.protocol.codec.TransportMessageEncoder) TransportException(io.transport.sdk.exception.TransportException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) IOException(java.io.IOException) TransportException(io.transport.sdk.exception.TransportException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 38 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project dubbo by alibaba.

the class QosProcessHandler method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 1) {
        return;
    }
    // read one byte to guess protocol
    final int magic = in.getByte(in.readerIndex());
    ChannelPipeline p = ctx.pipeline();
    p.addLast(new LocalHostPermitHandler(acceptForeignIp));
    if (isHttp(magic)) {
        // no welcome output for http protocol
        if (welcomeFuture != null && welcomeFuture.isCancellable()) {
            welcomeFuture.cancel(false);
        }
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(1048576));
        p.addLast(new HttpProcessHandler());
        p.remove(this);
    } else {
        p.addLast(new LineBasedFrameDecoder(2048));
        p.addLast(new StringDecoder(CharsetUtil.UTF_8));
        p.addLast(new StringEncoder(CharsetUtil.UTF_8));
        p.addLast(new IdleStateHandler(0, 0, 5 * 60));
        p.addLast(new TelnetProcessHandler());
        p.remove(this);
    }
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 39 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project ratpack by ratpack.

the class ConnectionIdleTimeout method init.

private void init(Duration duration) {
    long millis = duration.toMillis();
    channelPipeline.addFirst(HANDLER_NAME, new IdleStateHandler(millis, millis, 0, TimeUnit.MILLISECONDS));
    handlerRegistered = true;
}
Also used : IdleStateHandler(io.netty.handler.timeout.IdleStateHandler)

Example 40 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project java by wavefrontHQ.

the class Ingester method addIdleTimeoutHandler.

/**
 * Adds an idle timeout handler to the given pipeline
 *
 * @param pipeline the pipeline to add the idle timeout handler
 */
protected void addIdleTimeoutHandler(final ChannelPipeline pipeline) {
    // Shared across all reports for proper batching
    pipeline.addLast("idleStateHandler", new IdleStateHandler(CHANNEL_IDLE_TIMEOUT_IN_SECS_DEFAULT, 0, 0));
    pipeline.addLast("idleChannelTerminator", new ChannelDuplexHandler() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (evt instanceof IdleStateEvent) {
                if (((IdleStateEvent) evt).state() == IdleState.READER_IDLE) {
                    logger.warning("terminating connection to graphite client due to inactivity after " + CHANNEL_IDLE_TIMEOUT_IN_SECS_DEFAULT + "s: " + ctx.channel());
                    ctx.close();
                }
            }
        }
    });
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

Aggregations

IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)70 ChannelPipeline (io.netty.channel.ChannelPipeline)35 SocketChannel (io.netty.channel.socket.SocketChannel)18 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)14 LoggingHandler (io.netty.handler.logging.LoggingHandler)14 SslHandler (io.netty.handler.ssl.SslHandler)14 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)12 ChannelFuture (io.netty.channel.ChannelFuture)10 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)10 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 EventLoopGroup (io.netty.channel.EventLoopGroup)8 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)7 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)7 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 Channel (io.netty.channel.Channel)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 SslContext (io.netty.handler.ssl.SslContext)6 InetSocketAddress (java.net.InetSocketAddress)6