Error handling netty不同的channelpool和异常过程

Error handling netty不同的channelpool和异常过程,error-handling,netty,channel,Error Handling,Netty,Channel,我是netty新手,使用netty程序时遇到问题 在initConnection方法中,我想为每个组创建一个不同的channelpool 当用户组A进入我的sendToMessage时,我想创建channelPool A 像这样,用户组B进入我的sendToMessage我想创建channelPool B,下次如果用户组A再次进入,我将返回channelPool A 这样做对吗?可能吗 FixedChannelPool错误处理 告诉我如何FixedChannelPool错误处理?我是否可

我是
netty
新手,使用
netty
程序时遇到问题

  • initConnection
    方法中,我想为每个组创建一个不同的
    channelpool


    当用户组A进入我的
    sendToMessage
    时,我想创建
    channelPool A
    像这样,用户组B进入我的
    sendToMessage
    我想创建
    channelPool B
    ,下次如果用户组A再次进入,我将返回
    channelPool A

    • 这样做对吗?可能吗

  • FixedChannelPool
    错误处理

    • 告诉我如何
      FixedChannelPool
      错误处理?我是否可以在
      时间内使用
      acquireTimeoutMillis
      。如何使用
  • 这是我的密码

    @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,String userGroup) {
    
    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
    }
    
    @服务
    公共类NettyPool服务{
    公共静态最终属性key FUTURE=属性key.valueOf(“未来”);
    私有静态最终StringDecoder StringDecoder=新的StringDecoder(CharsetUtil.UTF_8);
    私有静态最终StringEncoder StringEncoder=新StringEncoder(CharsetUtil.UTF_8);
    专用静态信道池信道池;
    私有静态EventLoopGroup EventLoopGroup;
    @值(“${host}”)
    私有字符串主机;
    @值(“${port}”)
    专用字符串端口;
    @值(“${connection.count}”)
    私有字符串连接;
    @值(${thread.count}”)
    私有字符串numberOfThreads;
    私有同步void initConnection(字符串主机、int端口、int numberOfThreads、int numberOfConnections、字符串用户组){
    if((channelPool!=null)&&(eventLoopGroup!=null)){
    返回;
    }
    3.系统。输出。系统。输出。印刷LN(系统。系统。系统。输出。系统。输出。印印印(“(“(“联合35)))系统。系统。系统。系统。系统。系统。系统。系统。系统。输出。系统。系统。输出。系统。输出。系统。印印民民民民民方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方##################;
    System.out.println(“初始化连接启动”);
    eventLoopGroup=新的NioEventLoopGroup(线程数);
    Bootstrap Bootstrap=新的Bootstrap();
    option(ChannelOption.ALLOCATOR,pooledbytebuffallocator.DEFAULT);
    bootstrap.option(ChannelOption.SO_KEEPALIVE,true);
    //bootstrap.option(ChannelOption.WRITE\u BUFFER\u HIGH\u WATER\u MARK,32*1024);
    //bootstrap.option(ChannelOption.WRITE\u BUFFER\u LOW\u WATER\u MARK,8*1024);
    //选项(ChannelOption.TCP_NODELAY,true);
    bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).remoteAddress(主机、端口);
    int acquireTimeoutMillis=10000;
    int maxPendingAcquires=整数.MAX_值;
    channelPool=新的FixedChannelPool(引导,
    新的AbstractChannelPoolHandler(){
    创建的公用无效通道(通道ch)引发异常{
    ChannelPipeline=通道管道();
    //解码器
    pipeline.addLast(“framer”,新的DelimiterBasedFrameDecoder(8192,Delimiters.lineDelimiter());
    pipeline.addLast(“stringDecoder”,stringDecoder);
    //编码器
    pipeline.addLast(“stringEncoder”,stringEncoder);
    //业务逻辑处理程序
    addLast(“clientHandler”,新的ClientPoolHandler(channelPool));
    }
    }, 
    ChannelHealthChecker.ACTIVE,//eventloop
    AcquireTimeoutAction.NEW,//超时
    获得超时毫秒//
    连接数//
    maxPendingAcquires)//
    System.out.println(“初始连接端”);
    3.系统。输出。系统。输出。印刷LN(系统。系统。系统。输出。系统。输出。印印印(“(“(“联合35)))系统。系统。系统。系统。系统。系统。系统。系统。系统。输出。系统。系统。输出。系统。输出。系统。印印民民民民民方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方##################;
    }//初始化连接
    public void sendToMessage(字符串消息、字符串组ID){
    System.out.println(“=======================================================:”+GroupId);
    if(channelPool==null){
    initConnection(主机,Integer.parseInt(port.trim()),Integer.parseInt(numberOfThreads.trim()),Integer.parseInt(numberOfConnections.trim());
    }
    最终CompletableFuture=新的CompletableFuture();
    Future channelFuture=channelPool.acquire();
    System.out.println(“======================channelFuture.get()============================:“+channelFuture.toString());
    channelFuture.addListener(新的FutureStener(){
    公共无效操作完成(未来f){
    如果(f.isSuccess()){
    Channel=f.getNow();
    channel.attr(NettyPoolClientService.FUTURE).set(FUTURE);
    channel.writeAndFlush(消息,channel.voidPromise());
    }
    }
    });
    channelFuture.syncunterruptibly();
    }//sendToBnp
    }
    
    这里的代码有点长。我建议你把问题分成两部分。毫不犹豫地创建2个新的并关闭此一个。请提前阅读和阅读。谢谢您的建议。这里的代码有点长。我建议你把问题分成两部分。毫不犹豫地创建2个新的并关闭此一个。请事先阅读并确认。谢谢你的建议。