Jetty 7 的 websocket、servlet 和资源处理程序的嵌入式配置
我正在尝试配置嵌入式 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过简单地切换处理程序的顺序?
Jetty 按照您添加处理程序的顺序运行处理程序,并且资源处理程序处理收到的每个请求(可能通过提供
404
错误)。Have you tried simply switching the order of the handlers?
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).我遇到了同样的问题,但只使用了 websocketHandler 和 resourceHandler。我解决了这个问题,将resourceHandler设置为websocketHandler:
I had the same problem but using only websocketHandler and resourceHandler. I solve it setting the resourceHandler into the websocketHandler: