Example usage for io.netty.util ReferenceCountUtil release

List of usage examples for io.netty.util ReferenceCountUtil release

Introduction

In this page you can find the example usage for io.netty.util ReferenceCountUtil release.

Prototype

public static boolean release(Object msg) 

Source Link

Document

Try to call ReferenceCounted#release() if the specified message implements ReferenceCounted .

Usage

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 w  w w. ja v a  2  s .com*/

        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

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

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

    if (queue == null) {

        return;//from   ww w  .j  av  a2 s  .  co m
    }

    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.example.discard.DiscardServerHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    log.info("message coming.");
    final ByteBuf byteBuf = (ByteBuf) msg;
    final EmitterProcessor<Byte> emitter = EmitterProcessor.create();

    // ??? ByteBuf.toString(StandardCharset.UTF_8) ????
    final Mono<String> messageMono = emitter.collectList().map(DiscardServerHandler::unBoxing)
            .map(bs -> new String(bs, StandardCharsets.UTF_8));
    messageMono.subscribe(message -> log.info("received message: {}", message));
    messageMono.doOnTerminate(() -> ReferenceCountUtil.release(byteBuf));

    while (byteBuf.isReadable()) {
        emitter.onNext(byteBuf.readByte());
    }//from  w  w w  .ja  v  a2 s .  c o  m
    emitter.onComplete();
}

From source file:com.fjn.helper.frameworkex.netty.v4.discardserver.DiscardServerHandler.java

License:Apache License

/**
 * ??//from www . j  a va2 s .  c o  m
 */
public void channelRead2(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;
    try {
        while (in.isReadable()) {
            System.out.print((char) in.readByte());
            System.out.flush();
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:com.fjn.helper.frameworkex.netty.v4.discardserver.DiscardServerHandler.java

License:Apache License

/**
 * ???/*from   w w  w. j  a va  2  s. c  om*/
 * @param ctx
 * @param msg
 * @throws Exception
 */
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;
    try {
        while (in.isReadable()) {
            char c = (char) in.readByte();
            content += c;
            if (c == '\n' || c == '\r') {
                if (!line.isEmpty()) {
                    System.out.println(line);
                }
                if ("quit".equals(line)) {
                    ByteBuf bf = ctx.alloc().buffer(content.getBytes().length);
                    bf.setBytes(0, content.getBytes());
                    final ChannelFuture f = ctx.writeAndFlush(bf); // (3)
                    f.addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws InterruptedException {
                            assert f == future;
                            ctx.close();
                        }
                    }); // (4)
                }
                line = "";
                continue;
            } else {
                line += c;
            }
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:com.flysoloing.learning.network.netty.redis.RedisClientHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    RedisMessage redisMessage = (RedisMessage) msg;
    printAggregatedRedisResponse(redisMessage);
    ReferenceCountUtil.release(redisMessage);
}

From source file:com.github.ambry.admin.AdminIntegrationTest.java

License:Open Source License

/**
 * Combines all the parts in {@code contents} into one {@link ByteBuffer}.
 * @param response the {@link HttpResponse} containing headers.
 * @param contents the content of the response.
 * @return a {@link ByteBuffer} that contains all the data in {@code contents}.
 *//*  w  ww .  ja v a  2s.com*/
private ByteBuffer getContent(HttpResponse response, Queue<HttpObject> contents) {
    long contentLength = HttpHeaders.getContentLength(response, -1);
    if (contentLength == -1) {
        contentLength = HttpHeaders.getIntHeader(response, RestUtils.Headers.BLOB_SIZE, 0);
    }
    ByteBuffer buffer = ByteBuffer.allocate((int) contentLength);
    for (HttpObject object : contents) {
        HttpContent content = (HttpContent) object;
        buffer.put(content.content().nioBuffer());
        ReferenceCountUtil.release(content);
    }
    return buffer;
}

From source file:com.github.ambry.admin.AdminIntegrationTest.java

License:Open Source License

/**
 * Discards all the content in {@code contents}.
 * @param contents the content to discard.
 * @param expectedDiscardCount the number of {@link HttpObject}s that are expected to discarded.
 *//*w  w  w  .jav a2 s .c om*/
private void discardContent(Queue<HttpObject> contents, int expectedDiscardCount) {
    assertEquals("Objects that will be discarded differ from expected", expectedDiscardCount, contents.size());
    boolean endMarkerFound = false;
    for (HttpObject object : contents) {
        assertFalse("There should have been only a single end marker", endMarkerFound);
        endMarkerFound = object instanceof LastHttpContent;
        ReferenceCountUtil.release(object);
    }
    assertTrue("There should have been an end marker", endMarkerFound);
}

From source file:com.github.ambry.frontend.FrontendIntegrationTest.java

License:Open Source License

/**
 * Combines all the parts in {@code contents} into one {@link ByteBuffer}.
 * @param response the {@link HttpResponse} containing headers.
 * @param contents the content of the response.
 * @return a {@link ByteBuffer} that contains all the data in {@code contents}.
 *///  w  w  w  .  j  a  va 2 s .c  o  m
private ByteBuffer getContent(HttpResponse response, Queue<HttpObject> contents) {
    long contentLength = HttpHeaders.getContentLength(response, -1);
    if (contentLength == -1) {
        contentLength = HttpHeaders.getIntHeader(response, RestUtils.Headers.BLOB_SIZE, 0);
    }
    ByteBuffer buffer = ByteBuffer.allocate((int) contentLength);
    boolean endMarkerFound = false;
    for (HttpObject object : contents) {
        assertFalse("There should have been no more data after the end marker was found", endMarkerFound);
        HttpContent content = (HttpContent) object;
        buffer.put(content.content().nioBuffer());
        endMarkerFound = object instanceof LastHttpContent;
        ReferenceCountUtil.release(content);
    }
    return buffer;
}

From source file:com.github.ambry.frontend.FrontendIntegrationTest.java

License:Open Source License

/**
 * Verifies that no content has been sent as part of the response or readable bytes is equivalent to 0
 * @param contents the content of the response.
 *//*from w ww.j a  v  a 2  s  .c  om*/
private void assertNoContent(Queue<HttpObject> contents) {
    boolean endMarkerFound = false;
    for (HttpObject object : contents) {
        assertFalse("There should have been no more data after the end marker was found", endMarkerFound);
        HttpContent content = (HttpContent) object;
        Assert.assertEquals("No content expected ", 0, content.content().readableBytes());
        endMarkerFound = object instanceof LastHttpContent;
        ReferenceCountUtil.release(content);
    }
}