Netty 4.1-检测完全连接导致的拒绝连接

Netty 4.1-检测完全连接导致的拒绝连接,netty,Netty,我已将Netty 4.1作为TCP服务器实现到客户端,但不知道如何检测服务器是否无法接收客户端导致的连接已满,因此应用程序通知管理员服务器已满,在传统的ServerSocket中,我们可以检测异常连接过多/打开的文件过多: public class TCPServer { private int port; public TCPServer (int port) { this.port = port; } public

我已将Netty 4.1作为TCP服务器实现到客户端,但不知道如何检测服务器是否无法接收客户端导致的连接已满,因此应用程序通知管理员服务器已满,在传统的ServerSocket中,我们可以检测异常连接过多/打开的文件过多:

public class TCPServer {
    
    private int port;
    
    public TCPServer (int port) {
        this.port = port;
    }
    
    public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .childHandler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel ch) throws Exception {
                     ch.pipeline().addLast(new FrameDecoder());
                     ch.pipeline().addLast(new ProcessingData());

                 }
             })
             .option(ChannelOption.SO_BACKLOG, 128)         
             .childOption(ChannelOption.SO_KEEPALIVE, true);
            ChannelFuture f = b.bind(port).sync();
    
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
    
    public static void main(String[] args) throws Exception {
        int port = 9812;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        }

        new TCPServer
(port).run();
    }
}

公共类TCPServer{
专用int端口;
公共TCPServer(int端口){
this.port=端口;
}
public void run()引发异常{
EventLoopGroup bossGroup=新的NioEventLoopGroup();
EventLoopGroup workerGroup=新的NioEventLoopGroup();
试一试{
ServerBootstrap b=新的ServerBootstrap();
b、 组(bossGroup、workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(新的通道初始值设定项(){
@凌驾
public void initChannel(SocketChannel ch)引发异常{
ch.pipeline().addLast(新帧解码器());
ch.pipeline().addLast(新处理数据());
}
})
.option(ChannelOption.SO_BACKLOG,128)
.childOption(ChannelOption.SO_KEEPALIVE,true);
ChannelFuture f=b.bind(port.sync();
f、 通道().closeFuture().sync();
}最后{
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
公共静态void main(字符串[]args)引发异常{
int端口=9812;
如果(args.length>0){
port=Integer.parseInt(args[0]);
}
新TCPServer
(port.run();
}
}
帧解码器也作为检测连接已满

public class FrameDecoder extends ByteToMessageDecoder {

    public FrameDecoder() {
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        try {
            ctx.fireExceptionCaught(cause);
            // i never get exception like too many open files here....
            //
        } catch (Exception ex) {
        }
    }

   @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
        if (buf.readableBytes() < 5) {
            return;
        }
        int length = 2 + 2; // head and tail

        if (buf.getByte(buf.readerIndex()) == 0x78 && buf.getByte(buf.readerIndex() + 1) == 0x78) {
            length += 1 + buf.getUnsignedByte(buf.readerIndex() + 2);
        } else{
            length += 2 + buf.getUnsignedShort(buf.readerIndex() + 2);
        }

        if (buf.readableBytes() >= length && buf.getUnsignedShort(buf.readerIndex() + length - 2) == 0x0d0a) {
            out.add(buf.readRetainedSlice(length));
            return;
        }
        int endIndex = buf.readerIndex() - 1;
        do {
            endIndex = buf.indexOf(endIndex + 1, buf.writerIndex(), (byte) 0x0d);
            if (endIndex > 0 && buf.writerIndex() > endIndex + 1 && buf.getByte(endIndex + 1) == 0x0a) {
                out.add(buf.readRetainedSlice(endIndex + 2 - buf.readerIndex()));
            }
        } while (endIndex > 0);
    }

}

公共类FrameDecoder扩展为ByteToMessageDecoder{
公共帧解码器(){
}
@凌驾
public void exceptionCaught(ChannelHandlerContext ctx,可丢弃原因)引发异常{
试一试{
ctx.fireExceptionCaught(原因);
//我从来没有像这里有太多打开的文件一样出现异常。。。。
//
}捕获(例外情况除外){
}
}
@凌驾
受保护的无效解码(ChannelHandlerContext ctx、ByteBuf、List out)引发异常{
if(buf.readableBytes()<5){
返回;
}
int length=2+2;//头和尾
if(buf.getByte(buf.readerIndex())==0x78&&buf.getByte(buf.readerIndex()+1)==0x78){
长度+=1+buf.getUnsignedByte(buf.readerIndex()+2);
}否则{
长度+=2+buf.getUnsignedShort(buf.readerIndex()+2);
}
if(buf.readableBytes()>=length&&buf.getUnsignedShort(buf.readerIndex()+length-2)==0x0d0a){
out.add(buf.readRetainedSlice(长度));
返回;
}
int endIndex=buf.readerIndex()-1;
做{
endIndex=buf.indexOf(endIndex+1,buf.writerIndex(),(字节)0x0d);
如果(endIndex>0&&buf.writerIndex()>endIndex+1&&buf.getByte(endIndex+1)==0x0a){
add(buf.readretainedlice(endIndex+2-buf.readerIndex());
}
}而(endIndex>0);
}
}

您应该能够通过
b.handler(…)
ServerChannel
添加
ChannelInboundHandler
childHandler(…)
为“接受的”
频道添加处理程序,而
handler(…)
为接受
频道的
ServerChannel
添加处理程序。当为
ServerChannel
引发异常时,您需要在那里而不是在子处理程序上处理它