Mr.Wang from Next Door
Mr.Wang from Next Door

Reputation: 14790

Netty 4 : reference count in wrapped buffer

Netty 4.1.2.Final

I am using Unpooled.wrappedBuffer method to wrap more than one ByteBufs, API doc is here.

public static ByteBuf wrappedBuffer(ByteBuf... buffers)

The doc says

Reference count ownership of all variables is transfered to this method.

I update the question so it is more clear.

I need send more than one ByteBufs to multiple connections at once. I can't use ChannelGroup because some of the clients works in long-polling mode which means the response has to be sent in next request whose channel is not ready at this moment.

Hence, I have the following data structure stores the connected channels

private static final ConcurrentHashMap<String, Channel> channels 
  = new ConcurrentHashMap<String, Channel>(); 

And I have a method to broadcast the ByteBuf to the channels

public static void broadcast(final long registrationID, final ByteBuf [] buffers){
  final ByteBuf wrappedBuf = Unpooled.wrappedBuffer(buffers);
  for( Map.Entry<String, Channel> entry : channels.entrySet()){
    final Channel channel = entry.getValue();
    if( channel != null ){
        wrappedBuf.retain();

        boolean isLongPooling = channel.attr(TYPE).get();
        if( isLongPooling )
          Reply.send( channel, wrappedBuf); // add into a queue which will be pulled by client in next request
        else 
          channel.writeAndFlush(wrappedBuf); // real time client , send in current channel
    }
  }
  wrappedBuf.release();  
}

The problem is that: If I have more than one real-time clients connected, only one of them receives the response even if all of then are written.

It seems it is a problem caused by reference count?

Thank you

Upvotes: 0

Views: 523

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23557

No... it means that the you need to call release() on the returned buffer.

Upvotes: 1

Related Questions