Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 360
user3037845 Avatar asked Dec 01 '15 21:12

user3037845


1 Answers

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.

like image 63
Paul Samsotha Avatar answered Oct 13 '22 22:10

Paul Samsotha