嵌入式Jetty-启动Jetty服务器后添加上下文
在没有指定上下文和上下文处理程序的情况下启动jetty实例是否正确,然后在服务器启动后继续向其添加上下文。虽然我可以使用可变HandlerCollection完成这项工作,并且日志显示服务器和上下文已启动并可用,但我无法使用URL访问它。或者我们应该在启动服务器时至少向服务器添加一个根上下文和contexthandler吗 我做了一些类似于下面链接中建议的示例的事情。嵌入式Jetty-启动Jetty服务器后添加上下文,jetty,embedded-jetty,jetty-9,Jetty,Embedded Jetty,Jetty 9,在没有指定上下文和上下文处理程序的情况下启动jetty实例是否正确,然后在服务器启动后继续向其添加上下文。虽然我可以使用可变HandlerCollection完成这项工作,并且日志显示服务器和上下文已启动并可用,但我无法使用URL访问它。或者我们应该在启动服务器时至少向服务器添加一个根上下文和contexthandler吗 我做了一些类似于下面链接中建议的示例的事情。 我的jetty版本是9.3.7.v20160115向运行中的服务器添加处理程序是一种常见的模式,但文档一点也不清楚(“嵌入je
我的jetty版本是9.3.7.v20160115向运行中的服务器添加处理程序是一种常见的模式,但文档一点也不清楚(“嵌入jetty”教程中的所有示例都是在配置完成后启动服务器) 1) 使用HandlerCollection(boolean mutableWhenRunning)构造函数添加/删除处理程序 2) 显式添加并启动处理程序 我注意到,Jetty 9.1.4中不需要#2,但Jetty 9.2.14及其后版本都需要#2(顺便说一句,这些版本号被Maven选为Jersey依赖项,与此问题完全无关)。例如:
// after server creation ...
ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
jettyServer.setHandler(contextHandlerCollection);
jettyServer.start();
// ...
ServletContextHandler newSCH= new ServletContextHandler(ServletContextHandler.SESSIONS);
newSCH.setResourceBase(System.getProperty("java.io.tmpdir"));
newSCH.setContextPath("/servlets");
ServletHolder newHolder = new SwServletHolder(servlet);
newSCH.addServlet(newHolder, "/*");
contextHandlerCollection.addHandler(newSCH);
try {
newSCH.start(); // needed from about 9.2
} catch (Exception e) {
logger.info("Exception starting ServletContextHandler for Jetty", e);
}
为了添加SOAP上下文,这是一段代码,在9.1.4中“用于工作”(在9.2.14中报告404):
后来,我不得不为最后一种方法这样做(不确定是否有更好的方法):
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import org.eclipse.jetty.http.spi.JettyHttpServerProvider;
import org.eclipse.jetty.http.spi.HttpSpiContextHandler;
import org.eclipse.jetty.http.spi.JettyHttpContext;
import org.eclipse.jetty.http.spi.JettyHttpServer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import com.sun.net.httpserver.HttpContext;
public class JettyJaxWs {
public static void main(String[] args) throws Exception {
Server server = new Server(7777);
ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
server.setHandler(contextHandlerCollection);
server.start();
HttpContext context = buildOrig(server, "/ws");
MyWebService ws = new MyWebService();
Endpoint endpoint = Endpoint.create(ws);
endpoint.publish(context);
// access wsdl on http://localhost:7777/ws/MyWebService?wsdl
}
@WebService
public static class MyWebService {
public String hello(String s) {
return "hi " + s;
}
}
public static HttpContext buildOrig(Server server, String contextString) throws Exception {
JettyHttpServerProvider.setServer(server);
return new JettyHttpServerProvider().createHttpServer(new InetSocketAddress(7777), 5).createContext(contextString);
}
public static HttpContext buildNew(Server server, String contextString) {
JettyHttpServer jettyHttpServer = new JettyHttpServer(server, true);
JettyHttpContext ctx = (JettyHttpContext) jettyHttpServer.createContext(contextString);
try {
Method method = JettyHttpContext.class.getDeclaredMethod("getJettyContextHandler");
method.setAccessible(true);
HttpSpiContextHandler contextHandler = (HttpSpiContextHandler) method.invoke(ctx);
contextHandler.start();
} catch (Exception e) {
e.printStackTrace();
}
return ctx;
}