Jetty 7嵌入式配置,用于websockets、servlet和资源处理程序



我正在尝试配置嵌入式Jetty(7.6),以便同时处理WebSocketHandler、ServletContextHandler和ResourceHandler类。

我尝试过同时使用HandlerCollection和HandlerList类,但我无法使这三个部分全部工作。

  Server server = new Server(8081);
  // static files handler
  ResourceHandler resourceHandler = new ResourceHandler();
  resourceHandler.setDirectoriesListed(true);
  resourceHandler.setResourceBase("./src/main/webapp/");
  // servlet handler
  ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
  servletContextHandler.setContextPath("/");
  servletContextHandler.addServlet(new ServletHolder(new myServlet()), "/myServlet");
  // websocket handler
  myWebSocketHandler myWebSocketHandler = new myWebSocketHandler();
  // putting it together
  HandlerCollection handlerList = new HandlerCollection();
  handlerList.setHandlers(new Handler[]{resourceHandler,myWebSocketHandler,servletContextHandler});
  server.setHandler(handlerList);

在这种状态下,静态文件和servlet处理得很好,但当我发送websocket请求时,我得到:

2012-02-22 10:16:44.703:WARN:oejs.Response:Committed before 503 null
2012-02-22 10:16:44.705:WARN:oejs.AbstractHttpConnection:/
java.lang.IllegalStateException: Committed
    at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1080)
...

如果我检查websockets处理程序中的基本请求"已处理"属性的状态,它已经设置为true。所以这意味着我的请求在到达myWebSocketHandler之前已经被处理和提交了?

如有任何意见,我们将不胜感激。

您是否尝试过简单地切换处理程序的顺序?

 handlerList.setHandlers(new Handler[]{myWebSocketHandler,servletContextHandler,resourceHandler});

Jetty按照添加处理程序的顺序运行处理程序,资源处理程序处理每一个请求(可能是通过提供404错误)。

我遇到了同样的问题,但只使用了websocketHandler和resourceHandler。我通过将resourceHandler设置到websocketHandler中来解决这个问题:

public static void main(String[] args) {
    try {
        Server server = new Server(8081);
        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setResourceBase("./chat");
        WebSocketHandler chatWebSocketHandler = new ChatWebSocketHandler();
        chatWebSocketHandler.setHandler(resourceHandler);
        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[]{chatWebSocketHandler});
        server.setHandler(handlers);
        server.start();
        // Jetty server is stopped when the Thread is interrupted.
        server.join();
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

相关内容

  • 没有找到相关文章

最新更新