Jetty 7 的 websocket、servlet 和资源处理程序的嵌入式配置

发布于 2025-01-08 07:54:25 字数 1448 浏览 0 评论 0原文

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

我尝试过使用 HandlerCollection 和 HandlerList 类,但我无法让所有 3 个部分都工作。

  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 处理程序中检查基本请求“handled”属性的状态,它已经设置为 true。那么这意味着我的请求在到达 myWebSocketHandler 之前正在被处理并提交?

任何意见都表示赞赏,谢谢。

I am trying to configure embedded Jetty (7.6) to handle the WebSocketHandler, ServletContextHandler and ResourceHandler classes at the same time.

I have tried using both HandlerCollection and HandlerList classes but I can't get all 3 parts to work.

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

In this state, the static files and the servlet are handled fine, but when I send a websocket request, I get:

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

and if I check the state of the base requests "handled" property inside my websockets handler, it's already set to true. So this means that my request is being handled and committing before it gets to myWebSocketHandler?

Any comments are appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白昼 2025-01-15 07:54:25

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

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

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

Have you tried simply switching the order of the handlers?

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

Jetty runs the handlers in the order you add them, and the resource handler handles every request that comes its way (potentially by serving a 404 error).

水晶透心 2025-01-15 07:54:25

我遇到了同样的问题,但只使用了 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();
    }
}

I had the same problem but using only websocketHandler and resourceHandler. I solve it setting the resourceHandler into the 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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文