Example usage for io.netty.channel ChannelHandlerContext channel

List of usage examples for io.netty.channel ChannelHandlerContext channel

Introduction

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

Prototype

Channel channel();

Source Link

Document

Return the Channel which is bound to the ChannelHandlerContext .

Usage

From source file:com.ebay.jetstream.http.netty.server.HttpRequestHandler.java

License:MIT License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {

    if (LOGGER.isDebugEnabled()) {
        debug("Rcvr Session created to host - "
                + ((InetSocketAddress) ctx.channel().remoteAddress()).getHostName());
    }/*from ww  w  .j  a va  2s . c  om*/

    super.channelActive(ctx);
}

From source file:com.ebay.jetstream.http.netty.server.HttpRequestHandler.java

License:MIT License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {

    if (LOGGER.isDebugEnabled()) {
        debug("EventProducerSessionHandler -> session closed to host - "
                + ((InetSocketAddress) ctx.channel().remoteAddress()).getHostName());
    }/*from  www .  j a  va2s.  co m*/

    super.channelInactive(ctx);

}

From source file:com.ebay.jetstream.http.netty.server.HttpRequestHandler.java

License:MIT License

private void processHttpRequest(HttpRequest message, ChannelHandlerContext ctx) throws Exception {

    if (LOGGER.isDebugEnabled()) {
        debugHeadersAndCookies(message);
    }//from  www .j a v a  2s. c o m

    // Expect: 100-continue should be handled by HttpObjectAggregator.
    ByteBuf buf = ((FullHttpMessage) message).content();
    m_totalContentLength.addAndGet(buf.readableBytes());
    m_server.processHttpRequest(message, ctx.channel());
}

From source file:com.ebay.jetstream.messaging.transport.netty.autoflush.handler.NettyAutoFlushBatcher.java

License:MIT License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {

    if (!ctx.channel().isActive()) {

        ReferenceCountUtil.release(msg);

        // we might get here as channel is closed but upstream has not been notified yet. It could be still sending us events.
        // in such a case we will inform the future and send an exception upstream

        Throwable cause = new Exception("passed channel not active - "
                + ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress());

        promise.setFailure(cause);//from  ww w  . j  a  v  a 2s .c o  m

        ctx.fireExceptionCaught(cause);

        return;
    }

    AutoFlushWriterChannelQueue queue = m_channelQueue.get(ctx.channel());

    if (queue == null) {
        queue = new AutoFlushWriterChannelQueue(m_maxFlushBufferSz.get());
        queue.setChannelHandlerContext(ctx);
        m_channelQueue.put(ctx.channel(), queue);
    }

    MessageEvent e = new MessageEvent(msg, promise);

    queue.add(e);

    if (queue.isTimeToFlush()) {

        flush(ctx, queue);

    }

}

From source file:com.ebay.jetstream.messaging.transport.netty.autoflush.handler.NettyAutoFlushBatcher.java

License:MIT License

/**
 * @param ctx//from www.  ja  v  a2  s  .  c  o  m
 * @param queue
 */
public void flush(ChannelHandlerContext ctx, AutoFlushWriterChannelQueue queue) {

    if (queue == null)
        return;

    MessageEvent[] events = queue.get();

    if (events == null)
        return;

    ByteBuf[] msgs = new ByteBuf[events.length];

    for (int j = 0; j < events.length; j++) {
        msgs[j] = (ByteBuf) events[j].getMsg();
    }

    try {
        ByteBuf composite = Unpooled.wrappedBuffer(msgs);

        ChannelPromise promise = new ExtendedChannelPromise(ctx.channel());

        promise.addListener(new AutoFlushWriterChannelListener(events));

        queue.setLastFlushTime(System.currentTimeMillis());

        super.write(ctx, composite, promise);
        super.flush(ctx);

    } catch (Throwable t) {
        LOGGER.error("Error while Flushing : " + Arrays.toString(t.getStackTrace()));
    }

}

From source file:com.ebay.jetstream.messaging.transport.netty.autoflush.handler.NettyAutoFlushBatcher.java

License:MIT License

/**
 * Calls {@link ChannelHandlerContext#disconnect(ChannelPromise)} to forward
 * to the next {@link ChannelOutboundHandler} in the {@link ChannelPipeline}.
 *
 * Sub-classes may override this method to change behavior.
 *///w  ww.ja va  2 s .c o  m
@Override
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
    try {
        flush(ctx, m_channelQueue.get(ctx.channel()));
        m_channelQueue.remove(ctx.channel());
    } finally {
        ctx.disconnect(promise);
    }

}

From source file:com.ebay.jetstream.messaging.transport.netty.autoflush.handler.NettyAutoFlushBatcher.java

License:MIT License

@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {

    AutoFlushWriterChannelQueue queue = m_channelQueue.remove(ctx.channel());

    if (queue == null) {

        return;//from w  ww.j a v  a 2s .  com
    }

    MessageEvent[] events = queue.get();

    if (events == null) {
        ctx.close();
        return;
    }

    Throwable cause = new ClosedChannelException();

    for (int i = 0; i < events.length; i++) {

        MessageEvent ev = events[i];

        ReferenceCountUtil.release(ev.getMsg());

        ev.getPromise().setFailure(cause);

    }

    super.close(ctx, promise);

}

From source file:com.ebay.jetstream.messaging.transport.netty.autoflush.handler.NettyAutoFlushBatcher.java

License:MIT License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {

    AutoFlushWriterChannelQueue queue = m_channelQueue.remove(ctx.channel());

    if (queue == null) {
        ctx.fireChannelInactive();/*from w w w  . ja  v a 2 s .c  o  m*/
        return;
    }

    MessageEvent[] events = queue.get();

    if (events == null) {
        ctx.fireChannelInactive();
        return;
    }

    Throwable cause = new ClosedChannelException();

    for (int i = 0; i < events.length; i++) {

        MessageEvent ev = events[i];

        Promise promise = ev.getPromise();

        if (promise != null)
            promise.setFailure(cause);

        ((ByteBuf) ev.getMsg()).release();

    }

    if (queue != null) {
        queue.clear();

    }

    ctx.fireChannelInactive();
}

From source file:com.ebay.jetstream.messaging.transport.netty.autoflush.handler.NettyAutoFlushBatcher.java

License:MIT License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

    LOGGER.error(cause.getLocalizedMessage(), cause);

    AutoFlushWriterChannelQueue queue = m_channelQueue.remove(ctx.channel());

    if (queue == null)
        return;/* w  w w .jav  a2 s.c  om*/

    MessageEvent[] events = queue.get();

    if (events == null)
        return;

    for (int i = 0; i < events.length; i++) {

        MessageEvent ev = events[i];

        Promise promise = ev.getPromise();

        if (promise != null)
            promise.setFailure(cause);

        ((ByteBuf) ev.getMsg()).release();

    }

    if (queue != null) {
        queue.clear();

    }

    super.exceptionCaught(ctx, cause);
}

From source file:com.ebay.jetstream.messaging.transport.netty.compression.MessageCompressionHandler.java

License:MIT License

/**
 * Invoked when {@link Channel#write(Object)} is called.
 *//*w  ww .j  a v a 2  s . c o m*/
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {

    try {

        Attribute<Boolean> attr = ctx.channel().attr(EventProducer.m_eckey);

        Boolean enableCompression = attr.get();

        if ((enableCompression != null) && (enableCompression == true)) {

            ByteBuf chbuf = (ByteBuf) msg;

            int msglen = chbuf.readableBytes();
            ExtendedChannelPromise epromise = (ExtendedChannelPromise) promise;
            epromise.setRawBytes(msglen);

            byte[] compressed = Snappy.rawCompress(chbuf.readBytes(msglen).array(), msglen);

            epromise.setCompressedBytes(compressed.length + 4);
            chbuf.release(); // need to release the original buffer - do I need to check if this this a ref counted buffer

            ByteBuf sendbuf = ctx.alloc().buffer();

            sendbuf.writeInt(compressed.length);
            sendbuf.writeBytes(compressed);

            ctx.write(sendbuf, promise);

            m_totalMessagesCompressed.increment();

        } else {

            ctx.write(msg, promise);

        }

    } catch (Throwable t) {
        m_totalMessagesDropped.increment();
        LOGGER.debug("Failed to compress message - " + t.getLocalizedMessage());

    }

}