嵌入式 Jetty - 启动 Jetty 服务器后添加上下文

2024-02-28

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

我做了类似于下面链接中建议的示例的操作。Jetty 9(嵌入式):在运行时添加处理程序 https://stackoverflow.com/questions/20043097/jetty-9-embedded-adding-handlers-during-runtime

我的jetty版本是9.3.7.v20160115


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

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;
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

嵌入式 Jetty - 启动 Jetty 服务器后添加上下文 的相关文章

随机推荐