环境:
问题:
"反向代理servlet"被映射到URL" / visual-analytics / proxy / *"。
还有另一个"过滤器"映射到URL" /visual-analytics/proxy/elasticsearch/.kibana/search / *"其中正在使用ContinuationListener,如以下代码段所示:
ContinuationSupport.getContinuation(myRequestWrapper).addContinuationListener(new ContinuationListener() {
@Override
public void onTimeout(Continuation continuation) {
logger.log(Level.WARNING, "Request timeout...");
}
@Override
public void onComplete(Continuation continuation) {
HttpServletResponse httpResponse = (HttpServletResponse)continuation.getServletResponse();
if (httpResponse.getStatus() == HttpServletResponse.SC_OK || httpResponse.getStatus() == HttpServletResponse.SC_CREATED ) {
//some business logic
}
}
});
chain.doFilter(myRequestWrapper, response);
上面的ContinuationListener与 Jetty版本8.1.15.v20140411 一起正常工作,并且正在调用侦听器的onComplete()方法。但是在将Jetty版本升级到 9.3.14.v20161028 之后,ContinuationListener不再工作,即,不会调用侦听器的onComplete()方法和onTimeout()方法。
由于ContinuationListener不再有效,我将其替换为" javax.servlet.AsyncListener"实现,如以下代码段所示:
AsyncContext asyncContext = myRequestWrapper.startAsync();
asyncContext.addListener(new AsyncListener() {
@Override
public void onTimeout(AsyncEvent event) throws IOException
{
logger.log(Level.WARNING, "Async timeout...");
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException
{
logger.log(Level.INFO, "Async start...");
}
@Override
public void onError(AsyncEvent event) throws IOException
{
logger.log(Level.SEVERE, "Async error...");
}
@Override
public void onComplete(AsyncEvent event) throws IOException
{
HttpServletResponse httpResponse = (HttpServletResponse) event.getSuppliedResponse();
//HttpServletResponse httpResponse = (HttpServletResponse) event.getAsyncContext().getResponse();
if (httpResponse.getStatus() == HttpServletResponse.SC_OK || httpResponse.getStatus() == HttpServletResponse.SC_CREATED ) {
//some business logic
}
}
}, myRequestWrapper, httpServletResponse);
chain.doFilter(myRequestWrapper, response);
但添加上述" AsyncListener"实现导致以下" IllegalStateException":
https://164.99.175.139:8443/visual-analytics/proxy/elasticsearch/.kibana/search/test1?op_type=create java.lang.IllegalStateException: s=DISPATCHED i=true a=STARTED at org.eclipse.jetty.server.HttpChannelState.startAsync(HttpChannelState.java:264) at org.eclipse.jetty.server.Request.startAsync(Request.java:2235) at javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:432) at javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:432) at javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:432) at org.eclipse.jetty.proxy.ProxyServlet.service(ProxyServlet.java:88) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:845) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1712) at com.netiq.sentinel.kibana.search.KibanaSearchFilter.doFilter(KibanaSearchFilter.java:249) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1699) at com.netiq.sentinel.kibana.proxy.AcceptEncodingHeaderModificationFilter.doFilter(AcceptEncodingHeaderModificationFilter.java:37) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1699) at com.netiq.sentinel.kibana.proxy.SecurityFilter.doFilter(SecurityFilter.java:41) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1699) at com.netiq.sentinel.kibana.proxy.AuditFilter.doFilter(AuditFilter.java:104) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1699) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:582) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213) at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:119) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134) at org.eclipse.jetty.server.Server.handle(Server.java:534) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95) at org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:202) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95) at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671) at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589) at java.lang.Thread.run(Thread.java:745)
" ProxyServlet.java中的第88行"上面的堆栈跟踪中提到的代码如下:
final AsyncContext asyncContext = request.startAsync();
" IllegalStateException"抛出因为startAsync()方法在同一个请求上被调用两次 - 这是我的过滤器中的第一次,而第二次是Jetty ProxyServlet的service()方法?如果是,是否无法使用" AsyncListener"在由Jetty的ProxyServlet终止的过滤器链中?
有关如何进一步行动的任何指示将不胜感激。期待任何回应...
答案 0 :(得分:1)
仅供参考我修正了这个问题如下:
因此更新的代码段如下所示:
chain.doFilter(myRequestWrapper, response);
AsyncContext asyncContext = myRequestWrapper.getAsyncContext();
asyncContext.addListener(new AsyncListener() {
@Override
public void onTimeout(AsyncEvent event) throws IOException
{
logger.log(Level.WARNING, "Async timeout...");
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException
{
logger.log(Level.INFO, "Async start...");
}
@Override
public void onError(AsyncEvent event) throws IOException
{
logger.log(Level.SEVERE, "Async error...");
}
@Override
public void onComplete(AsyncEvent event) throws IOException
{
HttpServletResponse httpResponse = (HttpServletResponse) event.getSuppliedResponse();
//HttpServletResponse httpResponse = (HttpServletResponse) event.getAsyncContext().getResponse();
if (httpResponse.getStatus() == HttpServletResponse.SC_OK || httpResponse.getStatus() == HttpServletResponse.SC_CREATED ) {
//some business logic
}
}
}, myRequestWrapper, httpServletResponse);