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

问题描述

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.

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.

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

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.

解决方案

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.

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

07-29 10:46
查看更多