Example usage for io.netty.channel ChannelPipeline removeLast

List of usage examples for io.netty.channel ChannelPipeline removeLast

Introduction

In this page you can find the example usage for io.netty.channel ChannelPipeline removeLast.

Prototype

ChannelHandler removeLast();

Source Link

Document

Removes the last ChannelHandler in this pipeline.

Usage

From source file:com.addthis.hydra.query.loadbalance.NextQueryTask.java

License:Apache License

private static ChannelHandlerContext cleanPipelineAndGetLastContext(ChannelPipeline pipeline) {
    log.trace("pipeline before pruning {}", pipeline);
    ChannelHandlerContext lastContext = pipeline.lastContext();
    while ((lastContext != null) && !"encoder".equals(lastContext.name())) {
        pipeline.removeLast();
        lastContext = pipeline.lastContext();
    }//from  w  w w  .  j  a va 2  s  .  c o m
    log.trace("pipeline after pruning {}", pipeline);
    return lastContext;
}

From source file:net.NettyEngine4.ServerHandler.java

License:Apache License

/**
 * Calls {@link io.netty.channel.ChannelHandlerContext#fireChannelInactive()} to forward
 * to the next {@link io.netty.channel.Channel} in the {@link io.netty.channel.ChannelPipeline}.
 *
 * Sub-classes may override this method to change behavior.
 *
 * ??/*from w ww  . java 2  s  .  co m*/
 */
@Override
public void channelInactive(ChannelHandlerContext ctx) {
    try {
        ctx.channel().close();
        //clear and store data!  channel closed!
        // this.player.saveAndDestory();

        countConnection.decrementAndGet();// -1

        if (logger.isDebugEnabled())
            logger.debug(ctx.channel().remoteAddress() + " channelClosed , Concurrent  connection... "
                    + countConnection.get());

        ChannelPipeline channelPipeline = ctx.channel().pipeline();

        if (null == channelPipeline) { // if null ,will be returned
            return;
        }
        //remove pipeline object
        while (channelPipeline.last() != null) {
            channelPipeline.removeLast();
        }
        if (ctx.isRemoved()) {
            ctx.close();
            // Set to null so the GC can collect them directly
            channelPipeline = null;
            ctx = null;
            player = null;
        }
    } catch (NoSuchElementException ee) {
        //do nothing
    } catch (Exception e) {
        if (logger.isDebugEnabled())
            logger.error("", e);
        //do nothing
    }
}

From source file:org.apache.hadoop.hbase.io.asyncfs.FanOutOneBlockAsyncDFSOutputHelper.java

License:Apache License

private static void processWriteBlockResponse(Channel channel, final DatanodeInfo dnInfo,
        final Promise<Channel> promise, final int timeoutMs) {
    channel.pipeline().addLast(new IdleStateHandler(timeoutMs, 0, 0, TimeUnit.MILLISECONDS),
            new ProtobufVarint32FrameDecoder(), new ProtobufDecoder(BlockOpResponseProto.getDefaultInstance()),
            new SimpleChannelInboundHandler<BlockOpResponseProto>() {

                @Override//from   w  ww.  j  a v a2s  .  co m
                protected void channelRead0(ChannelHandlerContext ctx, BlockOpResponseProto resp)
                        throws Exception {
                    Status pipelineStatus = resp.getStatus();
                    if (PipelineAck.isRestartOOBStatus(pipelineStatus)) {
                        throw new IOException("datanode " + dnInfo + " is restarting");
                    }
                    String logInfo = "ack with firstBadLink as " + resp.getFirstBadLink();
                    if (resp.getStatus() != Status.SUCCESS) {
                        if (resp.getStatus() == Status.ERROR_ACCESS_TOKEN) {
                            throw new InvalidBlockTokenException("Got access token error" + ", status message "
                                    + resp.getMessage() + ", " + logInfo);
                        } else {
                            throw new IOException("Got error" + ", status=" + resp.getStatus().name()
                                    + ", status message " + resp.getMessage() + ", " + logInfo);
                        }
                    }
                    // success
                    ChannelPipeline p = ctx.pipeline();
                    for (ChannelHandler handler; (handler = p.removeLast()) != null;) {
                        // do not remove all handlers because we may have wrap or unwrap handlers at the header
                        // of pipeline.
                        if (handler instanceof IdleStateHandler) {
                            break;
                        }
                    }
                    // Disable auto read here. Enable it after we setup the streaming pipeline in
                    // FanOutOneBLockAsyncDFSOutput.
                    ctx.channel().config().setAutoRead(false);
                    promise.trySuccess(ctx.channel());
                }

                @Override
                public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                    promise.tryFailure(new IOException("connection to " + dnInfo + " is closed"));
                }

                @Override
                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                    if (evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state() == READER_IDLE) {
                        promise.tryFailure(
                                new IOException("Timeout(" + timeoutMs + "ms) waiting for response"));
                    } else {
                        super.userEventTriggered(ctx, evt);
                    }
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                    promise.tryFailure(cause);
                }
            });
}