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
}

-- 
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 [email protected].
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.

Reply via email to