将ChunkedFile写入netty中的上下文不会调用';操作完成';
我正在尝试在netty中实现某种http代理,为此,我需要从磁盘逐个发送几个文件。为此,我实现了简单的ChannelFutureListener,它在“operationComplete”上发送下一个文件:将ChunkedFile写入netty中的上下文不会调用';操作完成';,netty,nio,chunked,Netty,Nio,Chunked,我正在尝试在netty中实现某种http代理,为此,我需要从磁盘逐个发送几个文件。为此,我实现了简单的ChannelFutureListener,它在“operationComplete”上发送下一个文件: public class FrontendArchive implements ChannelFutureListener { private final ChannelHandlerContext ctx; private final Archive archive;
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”只针对前两个文件调用,而实际数据从未在另一端接收到。我做错了什么?好吧,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);
}
您不必等待传输完成,并且可以一次将所有传输排队