将ChunkedFile写入netty中的上下文永远不会调用'operationComplete'

时间:2014-05-02 07:20:16

标签: netty nio chunked

我正在尝试在netty中实现某种http代理,为此我需要逐个从磁盘发送几个文件。为此我实现了简单的ChannelFutureListener,在'operationComplete'上发送下一个文件:

public class FrontendArchive implements ChannelFutureListener {
    private final ChannelHandlerContext ctx;
    private final Archive archive;

    public void sendNext() {
        Entity entity = archive.nextEntity();
        if (entity == null) {
            //No more data
            // Write the end marker
            ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE);
            return;
        }

        try {
            ctx.writeAndFlush(entity.getData()).addListener(this);
        } catch (IOException e) {
            //We have nothing to do atm, but break the connection.
            log.error("Exception: {}", e.getMessage(), e);
            ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE);
        }
    }


    @Override
    public void operationComplete(ChannelProgressiveFuture channelProgressiveFuture) throws Exception {
        this.sendNext();
    }
}

getData非常简单:

public Object getData() throws IOException {
try(RandomAccessFile raf = new RandomAccessFile(new File(this.getLocation()), "r")) {
    long fileLength = raf.length();

    // Write the content.
    return new ChunkedFile(raf, 0, fileLength, SEND_BUFFER);
}
}

问题在于,由于某些原因,侦听器的'operationCompleted'仅针对前几个文件被调用,而实际数据从未在另一侧被接收。我做错了什么?

1 个答案:

答案 0 :(得分:0)

嗯,netty是聪明的,应该将写请求和它们自动排队。因此,上面的代码可以简化为:

    // Start sending data.
    Entity entity = archive.get().nextEntity();
    try {
        while (entity != null) {
            ctx.write(entity.getData());
            entity = archive.get().nextEntity();
        }
    } catch (IOException e) {
        log.error("Exception during zip serving: {}", e.getMessage(), e);
    } finally {
        ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE);
    }

您不必等待传输完成并允许一次排队所有传输。