我有一个eclipse插件,它使用带有ProxyServlet的jetty服务器。基本上,实现如下:
ServletHolder proxyServletHolder = new ServletHolder(new SubClassOfProxyServlet());
proxyServletHolder.setAsyncSupported(true);
ServletHandler proxyServletHandler = new ServletHandler();
proxyServletHandler.addServletWithMapping(proxyServletHolder, "/mapping/url");
之后,我将代理处理程序添加到处理程序列表中,并将此列表设置为服务器:
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] {
. // Other Handlers
.
proxyServletHandler,
.
.
.
new DefaultHandler()
});
server.setHandler(handlers);
一切都像 jetty 8 的魅力一样,但在迁移到 jetty 9 后,我收到以下错误:
Caused by: java.lang.IllegalStateException: No server executor for proxy
at org.eclipse.jetty.proxy.ProxyServlet.createHttpClient(ProxyServlet.java:279)
at org.eclipse.jetty.proxy.ProxyServlet.init(ProxyServlet.java:123)
... 24 more
使用ProxyServer的机制是否已更改?我错过了什么吗?
答案 0 :(得分:5)
您需要更新SubClassOfProxyServlet
类,以包含现在从Server
传递到代理的各种配置,然后由内部HttpClient
特定错误意味着您没有正确传递执行程序。
Executor特定部分有2个选项(在解决此问题后,您可能需要配置更多内容)
maxThreads
设置为有效的整数值。ServletContext.setAttribute("org.eclipse.jetty.server.Executor", myExecutor)
的servlet上下文属性中设置它。 - 您也可以在SubClassOfProxyServlet.init(ServletConfig config)
方法中执行此操作。答案 1 :(得分:2)
我能够通过上面提到的maxThreads
方法让它工作,并在创建时设置它。将此应用于原始示例将导致:
ServletHolder proxyServletHolder = new ServletHolder(new SubClassOfProxyServlet());
proxyServletHolder.setAsyncSupported(true);
proxyServletHolder.setInitParameter("maxThreads", "2");
ServletHandler proxyServletHandler = new ServletHandler();
proxyServletHandler.addServletWithMapping(proxyServletHolder, "/mapping/url");
答案 2 :(得分:0)
以下是如何将servlet添加到处理程序列表的示例:
private void addWebApp(String contextPath, String resourceBase, Server server) {
WebAppContext webAppContext = new WebAppContext();
// webAppContext.setDescriptor(webapp + "/WEB-INF/web.xml");
webAppContext.setResourceBase(resourceBase);
webAppContext.setContextPath(contextPath);
webAppContext.setParentLoaderPriority(true);
webAppContext.setWelcomeFiles(new String[] {"index.html"});
webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
final ServletHolder servletHolder =new ServletHolder();
servletHolder.setAsyncSupported(ContentBasedProxyServlet.class);
servletHolder.setAsyncSupported(true);
webAppContext.addServlet(servletHolder, "/*");
HandlerList handlers = (HandlerList) server.getHandler();
handlers.addHandler(webAppContext);
}
答案 3 :(得分:0)
此外,您还可以将maxThreads
添加到web.xml
:
<servlet>
<servlet-name>proxy</servlet-name>
<servlet-class>example.MyProxyServlet</servlet-class>
<init-param>
<param-name>maxThreads</param-name>
<param-value>5</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>