Calling flush () on Jersey StreamingOutput does not affect

I use StreamingOutput Jersey, which worked very well until we got to Jersey 2.16. That's what. My StreamingOuput produces products very slowly in some cases. I write data regularly, but I write it rather slowly and a little out of it at a time. I call flush() on an OutputStream passed to StreamingOutput.write() every time I write any bytes, but flush () has no effect. Nothing is sent over the cable until 8K is written to the OutputStream . Unfortunately, in some cases, at the time of writing 8K, the client has expired.

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

CommittingOutputStream enabled in CommittingOutputStream mode, and so flush () is essentially no-op until the response is complete (completed).

So I'm in the brine. How can I use StreamingOutput (or else write directly to the output stream) and make it send bytes over the wire until the whole response is complete? Is there any other way to do this with jersey? I cannot find any methods in ResponseBuilder for this. I can not find a way to disable buffering.

+6
source share
1 answer

There is a Jersey ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER property to set the buffer size, but changing it has consequences for the Content-Length header (<if that matters to you at all). You should read the docs about this property.

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

If the size of the entity exceeds the configured buffer size, buffering will be canceled and the size of the entity will not be determined. A value less than or equal to zero completely disables object buffering.

This property can be used on the server side to override the outgoing message buffer size β€” the default or the global custom value specified by the global jersey.config.contentLength.buffer property.

The default value is 8192.

+3
source

All Articles