我有一个服务器,可以传输各种内容,例如通过长期 HTTP 响应进行日志输出。然而,当使用 Jetty 的代理 servlet 时,我无法让它传输响应(它在发送之前缓冲整个响应)。
使用重写普通的 ProxyServlet
类,以下内容似乎可以工作:
@Override
protected void onResponseContent(HttpServletRequest request, HttpServletResponse response, Response proxyResponse, byte[] buffer, int offset, int length, Callback callback) {
super.onResponseContent(request, response, proxyResponse, buffer, offset, length, callback);
try {
response.getOutputStream().flush();
} catch (IOException e) {
log.warn("Error flushing", e);
}
}
但是,在重写 AsyncProxyServlet
时这样做不起作用。 (完整源代码here。)
那么,有两个问题:
- 使用
ProxyServlet
时,收到每一位内容后是否会刷新? - 有没有办法让它与
AsyncProxyServlet
一起工作?
最佳答案
成功了。无论是否使用异步,正确的方法都是有效的,即在创建 Jetty 服务器连接器时设置输出缓冲区大小。
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setOutputBufferSize(1024);
ServerConnector httpConnector = new ServerConnector(jettyServer,
new HttpConnectionFactory(httpConfig));
默认值为 32768。
(注意:无需重写onResponseContent
方法)
关于java - 使用 Jetty AsyncProxyServlet 流式传输 HTTP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40571811/