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

查看:12
本文介绍了在 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.

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.

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.

The default value is 8192.

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

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