gpt4 book ai didi

java - 对 servlet 请求调用 startAsync() 会在由 Jetty 9 的 ProxyServlet 终止的过滤器链中抛出 IllegalStateException

转载 作者:行者123 更新时间:2023-12-01 08:51:23 27 4
gpt4 key购买 nike

环境:

  • 在我的环境中,Kibana 4.5.2 在“反向代理 servlet”后面运行,该“反向代理 servlet”是通过扩展 Jetty 的“ProxyServlet”类创建的。
  • 这样做是为了可以使用 URL https://Jetty_Server_IP:8443/visual-analytics/proxy/ 访问 Kibana Web 界面...对此 URL 的请求被 Jetty 服务器中运行的反向代理拦截并重定向到 https://localhost:5601/ ...即,到与 Jetty 服务器在同一台机器上运行的 Kibana 服务器。
  • 然后,Kibana 服务器处理 Jetty 服务器转发的请求,并将响应返回给网络浏览器。
  • 注意: Jetty 在我的应用程序中以“嵌入模式”运行。

问题:

“反向代理 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() 方法中?如果是,是否无法在由 Jetty 的 ProxyServlet 终止的过滤器链中使用“AsyncListener”?

任何有关如何进一步进行的指示将不胜感激。期待任何回应...

最佳答案

仅供引用,我按如下方式修复了此问题:

  1. 我没有使用 Jetty 的“ContinuationListener”,而是使用了 Servlet 3.0 的“AsyncListener”,因为升级到 Jetty 9.3.14.v20161028 后,Jetty 的“ContinuationListener”似乎不起作用。这可能是 Jetty 的错误。
  2. 监听器相关的代码段位于过滤器的“chain.doFilter()”调用之前。我将此代码段移动到过滤器的“chain.doFilter()”调用之后的位置,以便我可以检索在 Jetty ProxyServlet 的 service() 方法中“已创建”的 AsyncContext。然后我可以将 AsyncListener 添加到检索到的 AsyncContext 中。
  3. 我将过滤器中的“request.startAsync()”调用更改为“request.getAsyncContext()”,这样我就不会启动一个导致 IllegalStateException 的新 AsyncContext,而只检索已在中创建的 AsyncContext Jetty 的 ProxyServlet。

所以更新后的代码段如下所示:

            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);

关于java - 对 servlet 请求调用 startAsync() 会在由 Jetty 9 的 ProxyServlet 终止的过滤器链中抛出 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42382024/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com