嵌入式 Jetty - 启动 Jetty Server 后添加上下文

V G*_*V G 2 jetty embedded-jetty jetty-9

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

我做了类似于以下链接中建议的示例的操作。 Jetty 9(嵌入式):在运行时添加处理程序

我的码头版本是 9.3.7.v20160115

DBE*_*DBE 6

向正在运行的服务器添加处理程序是一种常见模式,但文档根本不清楚(“嵌入码头”教程中的所有示例在配置启动服务器。)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);
        }
Run Code Online (Sandbox Code Playgroud)

为了添加 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);
    }
Run Code Online (Sandbox Code Playgroud)

后来,我不得不为最后一种方法这样做(不确定是否有更好的方法):

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;
    }
Run Code Online (Sandbox Code Playgroud)