Reputation: 29
I create a simple streaming endpoint that periodically writes and flushes output to the client:
private static Response myMethod() {
StreamingOutput stream =
outputStream -> {
for (int i = 0; i < 10000; i++) {
try {
String output = "Output: " + i + "\n";
outputStream.write(output.getBytes("UTF-8"));
outputStream.flush();
Thread.sleep(100);
} catch (Exception e) {
}
}
};
return Response.ok(stream).build();
}
This output appears to be buffered and is only written after N bytes have been accumulated. How can I force each message to be send to the client immediately?
In addition to using StreamingOutput
, I also tried directly writing to HttpServletResponse.getOutputStream()
. In that case, HttpServletResponse.setBufferSize(..)
didn't change the behavior.
It is not possible to set server or application level properties in this environment because there are multiple usages. It also seems not possible to switch to ChunkedOutput
because of import rules.
Upvotes: 0
Views: 32