is it possible to different channelpool?

33 views
Skip to first unread message

James Kim

unread,
Jun 13, 2017, 4:06:53 AM6/13/17
to Netty discussions

i am new to netty and i have a problem using my netty program


1. sendToMessage(String message,String GroupId)
when user group A come in my sendToMessage i want to create channelPool A
like this way  user group B come in my sendToMessage i want to create channelPool B
and next time if user group A come in again, i will return channelPool A
how can i solve this problem? am i righth?


2. is there Bootstrap option required?
Bootstrap bootstrap = new Bootstrap();
bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);

3.FixedChannelPool error handling
=> tell me how can i FixedChannelPool error handling?
ex)acquireTimeoutMillis over time.how?

@Service
public class NettyPoolService {
 
 public static final AttributeKey<CompletableFuture<String>> FUTURE = AttributeKey.valueOf("future");
 private static final StringDecoder stringDecoder = new StringDecoder(CharsetUtil.UTF_8);
    private static final StringEncoder stringEncoder = new StringEncoder(CharsetUtil.UTF_8);
 private static ChannelPool channelPool;
 private static EventLoopGroup eventLoopGroup;
 
 @Value("${host}")
 private String host;
 
 @Value("${port}")
 private String port;
 
 @Value("${connection.count}")
 private String numberOfConnections;
 
 @Value("${thread.count}")
 private String numberOfThreads;
 
 
 private synchronized void initConnection (String host, int port, int numberOfThreads, int numberOfConnections) {
  
  if ( (channelPool != null) && (eventLoopGroup != null) ) {
   return;
  }
  System.out.println("#############################################");
  System.out.println("initConnection start");
  
  eventLoopGroup = new NioEventLoopGroup(numberOfThreads);
  
  Bootstrap bootstrap = new Bootstrap();
        bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
        bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
        //bootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
        //bootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);
        //bootstrap.option(ChannelOption.TCP_NODELAY, true);
       
        bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).remoteAddress(host, port);
       
        int acquireTimeoutMillis = 10000;
        int maxPendingAcquires = Integer.MAX_VALUE;
       
        channelPool = new FixedChannelPool(bootstrap,  
          new AbstractChannelPoolHandler() {  
   
     public void channelCreated(Channel ch) throws Exception {
      ChannelPipeline pipeline = ch.pipeline();
                  // decoders
                  pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
                  pipeline.addLast("stringDecoder", stringDecoder);
  
                  // encoders
                  pipeline.addLast("stringEncoder", stringEncoder);
  
                  // business logic handler
                  pipeline.addLast("clientHandler", new ClientPoolHandler(channelPool));
     }
    },
          ChannelHealthChecker.ACTIVE,//eventloop
          AcquireTimeoutAction.NEW,  //timeout
          acquireTimeoutMillis,   //
          numberOfConnections,   //
          maxPendingAcquires);  //
       
        System.out.println("initConnection End");
        System.out.println("#############################################");
  
       
      
 }//initConnection
 
 
  
 public void sendToMessage(String message,String GroupId) {
  
  System.out.println("=============GroupId=============:"+GroupId);
  if (channelPool == null) {
   initConnection(host, Integer.parseInt(port.trim()), Integer.parseInt(numberOfThreads.trim()), Integer.parseInt(numberOfConnections.trim()) );
  }
  
  final CompletableFuture<String> future = new CompletableFuture<String>();
  Future<Channel> channelFuture = channelPool.acquire();
  
  
  System.out.println("=============channelFuture.get()=============:"+channelFuture.toString());
        channelFuture.addListener(new FutureListener<Channel>() {
            public void operationComplete(Future<Channel> f) {
                if (f.isSuccess()) {
                    Channel channel = f.getNow();
                    channel.attr(NettyPoolClientService.FUTURE).set(future);
                    channel.writeAndFlush(message, channel.voidPromise());
                }
            }
        });
       
      
        channelFuture.syncUninterruptibly();
             
       
 }//sendToBnp
}

Norman Maurer

unread,
Jun 13, 2017, 4:47:57 AM6/13/17
to ne...@googlegroups.com, James Kim
Sorry but I don’t understand what exactly you are asking for.

Can you please try to re-phrase ?
--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netty/546ab6b1-9277-4c8a-94ea-acaaccea3d42%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

James Kim

unread,
Jun 13, 2017, 5:24:31 AM6/13/17
to Netty discussions, kimch...@gmail.com

 private synchronized void initConnection (String host, int port, int numberOfThreads, int numberOfConnections,String userGroup) {

 if(userGroup=="A"){

  channelPoolA = new FixedChannelPool(.....);

 }else if(userGroup=="B"){

  channelPoolB = new FixedChannelPool(.....);

 }

}

i want to like this.but it is so awkward.so what is the best way to make a different channelpool.
and is it possible?



2017년 6월 13일 화요일 오후 5시 47분 57초 UTC+9, Norman Maurer 님의 말:
Reply all
Reply to author
Forward
0 new messages