Add flush method to ServerHttpResponse

This is useful to make sure response headers are written to the
underlying response. It is also useful in conjunction with long
running, async requests and HTTP streaming, to ensure the Servlet
response buffer is sent to the client without additional delay and
also causes an IOException to be raised if the client has gone away.
This commit is contained in:
Rossen Stoyanchev
2013-04-11 17:15:18 -04:00
parent db6f8f2d4b
commit 3a2c15b0fd
25 changed files with 258 additions and 138 deletions

View File

@@ -116,21 +116,35 @@ public class AsyncServletServerHttpRequest extends ServletServerHttpRequest
// Implementation of AsyncListener methods
// ---------------------------------------------------------------------
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
}
@Override
public void onError(AsyncEvent event) throws IOException {
}
@Override
public void onTimeout(AsyncEvent event) throws IOException {
for (Runnable handler : this.timeoutHandlers) {
handler.run();
try {
for (Runnable handler : this.timeoutHandlers) {
handler.run();
}
}
catch (Throwable t) {
// ignore
}
}
@Override
public void onComplete(AsyncEvent event) throws IOException {
for (Runnable handler : this.completionHandlers) {
handler.run();
try {
for (Runnable handler : this.completionHandlers) {
handler.run();
}
}
catch (Throwable t) {
// ignore
}
this.asyncContext = null;
this.asyncCompleted.set(true);

View File

@@ -17,6 +17,7 @@
package org.springframework.http.server;
import java.io.Closeable;
import java.io.IOException;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.HttpStatus;
@@ -35,6 +36,11 @@ public interface ServerHttpResponse extends HttpOutputMessage, Closeable {
*/
void setStatusCode(HttpStatus status);
/**
* TODO
*/
void flush() throws IOException;
/**
* Close this response, freeing any resources created.
*/

View File

@@ -80,6 +80,13 @@ public class ServletServerHttpResponse implements ServerHttpResponse {
return this.servletResponse.getOutputStream();
}
@Override
public void flush() throws IOException {
writeCookies();
writeHeaders();
this.servletResponse.flushBuffer();
}
public void close() {
writeCookies();
writeHeaders();