[英]Best way to stream back HTTP-Response?
I have a REST service which may have to return more than 1000,000 rows from the database. 我有一个REST服务,它可能必须从数据库返回超过1000,000行。 I am doing it by opening an output stream. 我通过打开输出流来做到这一点。 Basically I am getting printwriter from HTTP-RESPONSE and streaming it back as I get it from database rather than storing all in the memory and then returning back. 基本上,我是从HTTP-RESPONSE获取printwriter的,然后从数据库中获取它的流式传输,而不是将其全部存储在内存中,然后返回。
sth like this:- 像这样:
@Override
public void processRow(ResultSet resultSet) throws SQLException {
String data = resultSet.getString("data");
printWriter.write(data);
printWriter.flush();
}
}
My question is do we get any performance gain by flushing in a bucket? 我的问题是,通过在存储桶中进行冲洗,我们是否可以获得任何性能提升? Something like storing 5000 rows in a stringbuffer and then flushing it out rather than flushing it per row. 类似于在字符串缓冲区中存储5000行,然后将其刷新而不是按行刷新。
Each flush will make system to 'flush' data through all buffers down to network layer or, it depends on libraries you are using, might even block till remote side will acknowledge that data has arrived. 每次刷新都会使系统通过所有缓冲区将数据“刷新”到网络层,或者取决于您使用的库,甚至可能阻塞,直到远程端确认数据已到达。 That means that it will be a lots of time spent in I/O blocking aka 'it will be really slow'. 这意味着在I / O阻止上将花费大量时间,也就是“这真的很慢”。
I would recommend to remove flush here, make a BufferedOutputStream with reasonably small (512-8192 bytes) and create PrintWriter over it and perform measurements with different buffer sizes. 我建议在此处删除刷新,使BufferedOutputStream的大小较小(512-8192字节),并在其上创建PrintWriter并使用不同的缓冲区大小执行测量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.