netty:警告:触发了exceptionCaught()事件,它在管道的尾部到达

时间:2017-03-16 12:52:05

标签: java server netty nio

我为我的客户编写服务来处理多个客户端它工作正常但是最后几天客户端的负载已经增加,现在服务器代码产生了异常,我无法理解它发生了什么。

我的代码是:

    public void run() throws Exception {

    //System.setProperty("org.jboss.netty.epollBugWorkaround", "true");
    EventLoopGroup bossPool = new NioEventLoopGroup(BOSS_THREADS);
    EventLoopGroup workerPool = new NioEventLoopGroup(MAX_WORKER_THREADS);

    try {

        ServerBootstrap boot = new ServerBootstrap();
        boot.group(bossPool, workerPool);
        boot.channel(NioServerSocketChannel.class);
        boot.childHandler(new Pipeline());
        boot.option(ChannelOption.TCP_NODELAY, true);
        boot.option(ChannelOption.SO_KEEPALIVE, true);
        boot.option(ChannelOption.SO_REUSEADDR, true);
        boot.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 6000);
        boot.option(ChannelOption.SO_TIMEOUT, 6);
        boot.option(ChannelOption.SO_BACKLOG, 2048);

        //System.out.println("Server started listening at port : " + port);
        boot.bind(port).sync().channel().closeFuture().sync();

    } catch (InterruptedException e) {
    } finally {
        workerPool.shutdownGracefully();
        bossPool.shutdownGracefully();
    }
}

管道代码是:

    public class Pipeline extends ChannelInitializer<Channel> {
    @Override
    protected void initChannel(Channel ch) throws Exception {
        // ch.pipeline().addLast("joiner", new JoinRequest());
        ch.pipeline().addLast("decoder", new ByteArrayDecoder());
        ch.pipeline().addLast("encoder", new StringEncoder());
        ch.pipeline().addLast("myHelper", new RequestHandler());
       }
    }

这是我每天都会收到的详细错误日志:

1006465:54:21:10-16-03-2017Mar 16, 2017 10:21:54 AM         io.netty.channel.DefaultChannelPipeline$TailContext exceptionCaught
WARNING: An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.io.IOException: Too many open files
    at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
    at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422)
    at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250)
    at io.netty.channel.socket.nio.NioServerSocketChannel.doReadMessages(NioServerSocketChannel.java:135)
    at io.netty.channel.nio.AbstractNioMessageChannel$NioMessageUnsafe.read(AbstractNioMessageChannel.java:69)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:514)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:471)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:385)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:351)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
    at io.netty.util.internal.chmv8.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412)
    at io.netty.util.internal.chmv8.ForkJoinTask.doExec(ForkJoinTask.java:280)
    at io.netty.util.internal.chmv8.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:877)
    at io.netty.util.internal.chmv8.ForkJoinPool.scan(ForkJoinPool.java:1706)
    at io.netty.util.internal.chmv8.ForkJoinPool.runWorker(ForkJoinPool.java:1661)
    at io.netty.util.internal.chmv8.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:126)

com.idea.builders.RequestHandler oprationSelector
SEVERE: null
java.io.FileNotFoundException: config.dat (Too many open files)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)
    at com.idea.builders.RequestHandler.oprationSelector(RequestHandler.java:175)
    at com.idea.builders.RequestHandler.messageReceived(RequestHandler.java:97)
    at com.idea.builders.RequestHandler.messageReceived(RequestHandler.java:35)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
    at io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:84)
    at io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:153)
    at io.netty.channel.PausableChannelEventExecutor.invokeChannelRead(PausableChannelEventExecutor.java:86)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:389)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:84)
    at io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:153)
    at io.netty.channel.PausableChannelEventExecutor.invokeChannelRead(PausableChannelEventExecutor.java:86)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:389)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:956)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:127)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:514)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:471)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:385)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:351)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
    at io.netty.util.internal.chmv8.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412)
    at io.netty.util.internal.chmv8.ForkJoinTask.doExec(ForkJoinTask.java:280)
    at io.netty.util.internal.chmv8.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:877)
    at io.netty.util.internal.chmv8.ForkJoinPool.scan(ForkJoinPool.java:1706)
    at io.netty.util.internal.chmv8.ForkJoinPool.runWorker(ForkJoinPool.java:1661)
    at io.netty.util.internal.chmv8.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:126)

Mar 16, 2017 10:21:54 AM com.idea.builders.RequestHandler oprationSelector
SEVERE: null
java.io.FileNotFoundException: config.dat (Too many open files)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)
    at com.idea.builders.RequestHandler.oprationSelector(RequestHandler.java:175)
    at com.idea.builders.RequestHandler.messageReceived(RequestHandler.java:97)
    at com.idea.builders.RequestHandler.messageReceived(RequestHandler.java:35)

我该怎么办?我必须每天重启服务器?

1 个答案:

答案 0 :(得分:0)

您应该在其中一个处理程序中覆盖exceptionCaught方法。

该消息表示netty警告异常,但您没有在任何处理程序中处理它。异常只是到达最后一个管道处理程序并消失。您可能应该在请求处理程序中处理它。

并且在完成文件处理后关闭所有流。此外,您还应该在异常或未注册事件上释放套接字和其他资源。