【问题标题】:Embedded Jetty - Adding Context after starting Jetty ServerEmbedded Jetty - 启动 Jetty 服务器后添加上下文
【发布时间】:2016-04-05 05:53:42
【问题描述】:

在没有指定上下文且没有上下文处理程序的情况下启动一个码头实例是否正确,然后在服务器启动后继续为其添加上下文。尽管我能够使用可变的 HandlerCollection 来执行此操作,并且日志显示服务器和上下文已启动且可用,但我无法使用 URL 访问它。或者我们应该在启动服务器时向服务器添加至少一个根上下文和上下文处理程序?

我做了类似于以下链接中建议的示例的操作。 Jetty 9 (embedded): Adding handlers during runtime

我的码头版本是 9.3.7.v20160115

【问题讨论】:

    标签: jetty embedded-jetty jetty-9


    【解决方案1】:

    将处理程序添加到正在运行的服务器是一种常见的模式,但文档根本不清楚(“嵌入码头”教程中的所有示例都在配置后启动服务器。)AFAIK人们正在遵循这些方法:

    1) 使用 HandlerCollection(boolean mutableWhenRunning) ctor 添加/删除处理程序

    2) 显式添加并启动处理程序

    我观察到 Jetty 9.1.4 中不需要 #2,但它在 Jetty 9.2.14 及之后的版本中(顺便说一句,这些版本号被 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多