Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

jax rs - calling flush() on Jersey StreamingOutput has no effect

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...