java - Netty ClosedChannelException 异常

标签 java io netty

我是 Netty 新手,我遵循了这个 example使用netty编写静态文件服务器。但是每当服务器提供一个大的js文件时。它遇到 ClosedChannelException。

以下是我的代码,我将 chunkedFile 写入作为 http 响应。 当提供大型 js 文件时,我收到 closeChannelException 并且 raf 文件也被关闭。

你能帮我弄清楚我在这里做错了什么吗?另外,是否有一个简单的教程可以帮助我了解 netty 的基本控制流程?

     // Write the content.
      ChannelFuture writeFuture = null;
    try
    {
        long fileLength = raf.length();
        HttpResponse response = new DefaultHttpResponse(
            HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, fileLength);
        Channel c = ctx.getChannel();
        // Write the initial line and the header.
        c.write(response);

        writeFuture = c.write(new ChunkedFile(raf, 0, fileLength, 8192));
    }
    finally
    {
        raf.close();
        if (writeFuture != null)
            writeFuture.addListener(ChannelFutureListener.CLOSE);
    }
}

<

最佳答案

在finally block 中调用raf.close()是错误的,因为它可能还没有写入。事实上,netty 会在写入完成后关闭它。

关于java - Netty ClosedChannelException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18042923/

相关文章:

netty - 在 Netty channel 上保持状态

java - 来自 http 调用和表单的 Meteor 动态模板

java - Java中使用正则表达式扫描有什么缺点

java - 来自另一个类的多个 Activity 的 setContentView

java - JPA 与同一实体的两个单向@OneToMany 关系导致重复条目

java - java Scanner 类中的 nextLine()、hasNextLine() 和 NoSuchElementException

cocoa - 如何检查 NSFileHandle 是否有可用数据?

python - 禁用标准。和 Python 沙箱实现中的文件 I/O

httpclient - webClient 问题 - ReactorClientHttpConnector 和 httpClient 之间

java - Netty - 关闭客户端的套接字是否会关闭服务器的 channel