Example usage for io.netty.handler.timeout IdleStateHandler IdleStateHandler

List of usage examples for io.netty.handler.timeout IdleStateHandler IdleStateHandler

Introduction

In this page you can find the example usage for io.netty.handler.timeout IdleStateHandler IdleStateHandler.

Prototype

public IdleStateHandler(long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit) 

Source Link

Usage

From source file:alluxio.network.netty.NettyClient.java

License:Apache License

/**
 * Creates and returns a new Netty client bootstrap for clients to connect to remote servers.
 *
 * @param address the socket address/* ww w  .  j av a2 s.com*/
 * @return the new client {@link Bootstrap}
 */
public static Bootstrap createClientBootstrap(SocketAddress address) {
    final Bootstrap boot = new Bootstrap();

    boot.group(WORKER_GROUP).channel(
            NettyUtils.getClientChannelClass(NettyUtils.CHANNEL_TYPE, !(address instanceof InetSocketAddress)));
    boot.option(ChannelOption.SO_KEEPALIVE, true);
    boot.option(ChannelOption.TCP_NODELAY, true);
    boot.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    if (NettyUtils.CHANNEL_TYPE == ChannelType.EPOLL) {
        boot.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    }

    // After 10 missed heartbeat attempts and no write activity, the server will close the channel.
    final long timeoutMs = Configuration.getMs(PropertyKey.NETWORK_NETTY_HEARTBEAT_TIMEOUT_MS);
    final long heartbeatPeriodMs = Math.max(timeoutMs / 10, 1);
    boot.handler(new ChannelInitializer<Channel>() {
        @Override
        public void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();

            pipeline.addLast(RPCMessage.createFrameDecoder());
            pipeline.addLast(ENCODER);
            pipeline.addLast(DECODER);
            pipeline.addLast(new IdleStateHandler(0, heartbeatPeriodMs, 0, TimeUnit.MILLISECONDS));
            pipeline.addLast(new IdleWriteHandler());
        }
    });

    return boot;
}

From source file:barry.ServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast("logging", new LoggingHandler());

    // ???//from   w w w .  ja  v a2  s  .c  om
    //      pipelinene.addLast("decoder", new MsgDecoder());
    // ????byte
    //      pipeline.addLast("encoder", new MsgEncoder());

    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());

    // 
    pipeline.addLast(new IdleStateHandler(0, 0, 90, TimeUnit.SECONDS));

    // 
    pipeline.addLast("handler", serverHandler);
}

From source file:cn.savor.small.netty.NettyClient.java

License:Open Source License

public void start() {
    try {/*w  ww  .  j  a  v  a 2s .  com*/
        bootstrap = new Bootstrap();
        bootstrap.group(workGroup).channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel ch) throws Exception {
                        System.out.println("client SocketChannel.....................................");
                        ch.pipeline().addLast("ping", new IdleStateHandler(60, 60, 80, TimeUnit.SECONDS));
                        //POJO? ?
                        ch.pipeline().addLast(new ObjectDecoder(1024 * 5,
                                ClassResolvers.cacheDisabled(this.getClass().getClassLoader())));
                        //????
                        ch.pipeline().addLast(new ObjectEncoder());

                        ch.pipeline().addLast(new NettyClientHandler(NettyClient.this, callback, mContext));
                    }
                });
        connect();

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.code.server.gate.bootstarp.SocketServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();//from w  w w .jav  a  2 s .  c om
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }
    //        p.addLast(new LoggingHandler(LogLevel.INFO));
    p.addLast("encoder", new GameCharsEncoder());
    p.addLast("decoder", new GameCharsDecoder());
    p.addLast("timeout", new IdleStateHandler(15, 0, 0, TimeUnit.SECONDS));
    //        p.addLast(new SocketHandler());
    p.addLast("gameHandler", new GameMsgHandler());
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueEndpoint.java

License:Apache License

@Override
protected void customEndpointHandlers(final ChannelPipeline pipeline) {
    if (environment().keepAliveInterval() > 0) {
        pipeline.addLast(new IdleStateHandler(environment().keepAliveInterval(), 0, 0, TimeUnit.MILLISECONDS));
    }/*w  ww .j a  va2  s.  c  o m*/
    pipeline.addLast(new BinaryMemcacheClientCodec())
            .addLast(new BinaryMemcacheObjectAggregator(Integer.MAX_VALUE))
            .addLast(new KeyValueAuthHandler(bucket(), password()))
            .addLast(new KeyValueFeatureHandler(environment()))
            .addLast(new KeyValueHandler(this, responseBuffer(), false));
}

From source file:com.couchbase.client.core.endpoint.query.QueryEndpoint.java

License:Apache License

@Override
protected void customEndpointHandlers(final ChannelPipeline pipeline) {
    if (environment().keepAliveInterval() > 0) {
        pipeline.addLast(new IdleStateHandler(environment().keepAliveInterval(), 0, 0, TimeUnit.MILLISECONDS));
    }/*from w  w w  . ja  v  a 2 s.c o m*/
    pipeline.addLast(new HttpClientCodec()).addLast(new QueryHandler(this, responseBuffer(), false));
}

From source file:com.couchbase.client.core.endpoint.search.SearchEndpoint.java

License:Apache License

@Override
protected void customEndpointHandlers(final ChannelPipeline pipeline) {
    if (environment().keepAliveInterval() > 0) {
        pipeline.addLast(new IdleStateHandler(environment().keepAliveInterval(), 0, 0, TimeUnit.MILLISECONDS));
    }/*www  .ja  v  a 2s  .  co m*/
    pipeline.addLast(new HttpClientCodec()).addLast(new SearchHandler(this, responseBuffer(), false));
}

From source file:com.couchbase.client.core.endpoint.view.ViewEndpoint.java

License:Apache License

@Override
protected void customEndpointHandlers(final ChannelPipeline pipeline) {
    if (environment().keepAliveInterval() > 0) {
        pipeline.addLast(new IdleStateHandler(environment().keepAliveInterval(), 0, 0, TimeUnit.MILLISECONDS));
    }/*from w  w w.  ja  v  a  2 s  .  c o  m*/
    pipeline.addLast(new HttpClientCodec()).addLast(new ViewHandler(this, responseBuffer(), false));
}

From source file:com.eucalyptus.ws.IoHandlers.java

License:Open Source License

public static Map<String, ChannelHandler> channelMonitors(final TimeUnit unit, final long timeout) {
    final Map<String, ChannelHandler> monitors = Maps.newHashMap();
    monitors.put("idlehandler", new IdleStateHandler(0L, 0L, timeout, unit));
    monitors.put("idlecloser", new ChannelInboundHandlerAdapter() {
        @Override//from   www . j  av  a  2 s  .c o  m
        public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception {
            if (evt instanceof IdleStateEvent) {
                IdleStateEvent e = (IdleStateEvent) evt;
                if (e.state() == IdleState.ALL_IDLE) {
                    ctx.channel().close();
                }
            }
        }
    });
    monitors.putAll(extraMonitors);
    return monitors;
}

From source file:com.github.nettybook.ch7.junit.TelnetServerInitializerV3.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    // Add the text line codec combination first,
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    // the encoder and decoder are static as these are sharable
    pipeline.addLast(DECODER);//from   w  ww .j  a v  a  2  s .  c  o m
    pipeline.addLast(ENCODER);
    pipeline.addLast(new IdleStateHandler(0, 0, 10, TimeUnit.SECONDS));

    // and then business logic.
    pipeline.addLast(SERVER_HANDLER);
}