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

时间:2017-02-22 03:13:25

标签: java servlet-filters embedded-jetty servlet-3.0 jetty-9

环境:

  • 在我的环境中,Kibana 4.5.2运行在"反向代理servlet"这是通过扩展Jetty" ProxyServlet"类。
  • 这样做是为了可以使用URL https://Jetty_Server_IP:8443/visual-analytics/proxy/访问Kibana Web界面...此URL的请求被Jetty服务器中运行的反向代理拦截并重定向到https://localhost:5601/。 ..即,与Jetty Server在同一台机器上运行的Kibana服务器。
  • 然后,Kibana Server处理Jetty服务器转发的请求,并将响应返回给Web浏览器。
  • 注意: 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()方法?如果是,是否无法使用" AsyncListener"在由Jetty的ProxyServlet终止的过滤器链中?

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

1 个答案:

答案 0 :(得分:1)

仅供参考我修正了这个问题如下:

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

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