简体   繁体   English

流回HTTP响应的最佳方法?

[英]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.

相关问题 Jetty中HTTP响应的状态码中的Unicode - Unicodes in statuscode of HTTP-Response in Jetty 解压缩GZIP http-response(使用jersey client api,java) - Uncompress GZIP http-response (using jersey client api, java) 如何在JAVA中创建发送图像的HTTP响应? - How to create a HTTP-response sending an image in JAVA? 处理巨大 HTTP JSON 响应的最佳方式 - Best way to process a huge HTTP JSON response 是否可以通过使用Citrus-Framwork将http响应的内容类型获取为String? - Is it possbile to get the content type of a http-response as String by using the Citrus-Framwork? HTTP响应变得模糊 - http response coming back obfuscated 通过TCP发送转换为字节数组的XML,然后将响应转换回可读XML的最佳方法是什么? - What is the best way to send XML converted to a byte array over TCP, then translate the response back to readable XML? 使用 Reactor WebClient 发出非阻塞 HTTP 请求并反序列化对 object 的响应的最佳方法是什么? - What is the best way to make a non-blocking HTTP request using Reactor WebClient and deserialize the response to an object? 参考HTTP响应流返回对象 - Return object with reference to HTTP response stream 是否可以发送输出流作为对 HTTP 请求的响应? - Is it possible to send an ouput stream as response to a HTTP request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM