在Jersey StreamingOutput上调用flush()无效 [英] calling flush() on Jersey StreamingOutput has no effect

查看:95
本文介绍了在Jersey StreamingOutput上调用flush()无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用工作正常的Jersey StreamingOutput,直到我们升级到Jersey 2.16.这是东西.在某些情况下,我的StreamingOuput产生的输出非常缓慢.我确实定期写数据,但是我写得很慢,一次只写一小部分.每次写任何字节时,我都会在传递给StreamingOutput.write()OutputStream上调用flush(),但是flush()似乎无效.在将8K写入OutputStream之前,不会通过电线发送任何内容.不幸的是,在某些情况下,到写入8K时,客户端已超时.

I am using a Jersey StreamingOutput that was working just fine until we upgraded to Jersey 2.16. Here's the thing. My StreamingOuput produces output very slowly in some circumstances. I do write data regularly, but I write it pretty slowly and just a little of it at a time. I call flush() on the OutputStream passed to StreamingOutput.write() every time I write any bytes, but the flush() appears to have no effect. Nothing is sent over the wire until 8K has been written to the OutputStream. Unfortunately, in some circumstances, by the time 8K has been written, the client has timed out.

我下载了一些球衣源代码,并通过一些调试,发现传递给write()OutputStream是包裹了CommittingOutputStreamUnCloseableOutputStream.

I downloaded some of the jersey source and through some debugging, I see that the OutputStream passed to write() is an UnCloseableOutputStream which wraps a CommittingOutputStream.

CommittingOutputStream启用了缓冲,因此在提交响应(完成)之前,flush()本质上是无操作的.

The CommittingOutputStream has buffering enabled, and therefore the flush() is essentially a no-op until the response is committed (complete).

所以,我在泡菜中.如何使用StreamingOutput(或直接写到输出流)并强制其在整个响应完成之前通过电线发送字节?泽西岛还有其他方法可以做到这一点吗?我找不到ResponseBuilder上的任何方法来执行此操作.我找不到关闭缓冲的任何方法.

So, I am in a pickle. How can I use a StreamingOutput (or otherwise write directly to an output stream) and force it to send bytes over the wire before the entire response is complete? Is there some other way to do this with Jersey? I can't find any methods on the ResponseBuilder to do this. I can't find any way to turn off buffering.

推荐答案

有Jersey属性

There is the Jersey property ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER to set the size of the buffer, but changing it has implications on the Content-Length header (if that matters to you at all). You should read the docs on the property.

一个整数值,该值定义用于缓冲服务器端响应实体以确定其大小并设置HTTP"Content-Length"标头值的缓冲区大小.

An integer value that defines the buffer size used to buffer server-side response entity in order to determine its size and set the value of HTTP "Content-Length" header.

如果实体大小超过配置的缓冲区大小,则缓冲将被取消,并且将无法确定实体大小.小于或等于零的值将完全禁用实体的缓冲.

If the entity size exceeds the configured buffer size, the buffering would be cancelled and the entity size would not be determined. Value less or equal to zero disable the buffering of the entity at all.

可以在服务器端使用此属性来覆盖出站消息缓冲区大小值-默认值或使用"jersey.config.contentLength.buffer"全局属性设置的全局自定义值.

This property can be used on the server side to override the outbound message buffer size value - default or the global custom value set using the "jersey.config.contentLength.buffer" global property.

默认值为8192.

这篇关于在Jersey StreamingOutput上调用flush()无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆