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

标签 jetty embedded-jetty jetty-9

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

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

我的jetty版本是9.3.7.v20160115

最佳答案

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

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

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

我观察到 #2 在 Jetty 9.1.4 中不需要,但它在 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;
    }

关于jetty - 嵌入式 Jetty - 启动 Jetty 服务器后添加上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36418158/

相关文章:

java - 如何在javax.websocket服务器中获取远程客户端地址?

embedded-jetty - 嵌入式jetty中的外部文件(css、js)

java - 如何使keycloak与嵌入式 jetty 一起工作?

solr - 在最近的 solr-jetty 更新后,如何让 Solr 和 CKAN 在 Ubuntu 18.04 上运行?

java - 使用嵌入式 Jetty 提供部分内容

java - 在 Eclipse 中运行 wicket 项目时绑定(bind)异常

java - jetty 9.0.3 中的 Close_wait 过多

java - Jetty 9.4 在启动时启动两个 jvm

java - 如何为 Jetty SSL 设置密码套件的顺序?

jetty - Gradle Jetty 插件锁定文件