java - jetty 9(嵌入式): Adding handlers during runtime

标签 java jetty embedded-jetty

有什么方法可以将处理程序添加到正在运行的嵌入式 Jetty 实例中吗?我们已经将一个基于 Jetty 6 的旧项目迁移到 Jetty 9,我们的插件系统需要能够动态添加和删除处理程序...

看下面的例子...

Server server = new Server();
[...]
server.start();
[...]
Handler[] existingHandler = server.getHandlers();

// There is no more
server.addHandler(newHandler);

// only this you can do, but only if the server is stopped
server.setHandler(newHandler)

注意:newHandler 是一个HandlerCollection...

最佳答案

这里是一个完整的代码示例。除了使用 HandlerCollection(true) 之外,显式启动新的上下文处理程序也很重要。

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;

public class DynamicContextHandlers {
    public static void main(String[] args) throws Exception {
        new DynamicContextHandlers().run();
    }

    public void run() throws Exception {
        int port = 8080;
        Server server = new Server(port);

        ContextHandler contextHandler = new ContextHandler();
        contextHandler.setContextPath("/hello");
        contextHandler.setResourceBase(".");
        contextHandler.setClassLoader(Thread.currentThread().getContextClassLoader());
        contextHandler.setHandler(new HelloHandler(""));

        HandlerCollection contextHandlerCollection = new HandlerCollection(true); // important! use parameter
                                                                                    // mutableWhenRunning==true

        // add context handler before starting server (started implicitly)
        contextHandlerCollection.addHandler(contextHandler);
        server.setHandler(contextHandlerCollection);

        server.start();
        System.out.println("Server started at port " + port + " with context handler for /hello");

        System.out.println("Press enter to add context handler for /hello2");
        System.in.read();

        ContextHandler contextHandler2 = new ContextHandler();
        contextHandler2.setContextPath("/hello2");
        contextHandler2.setResourceBase(".");
        contextHandler2.setClassLoader(Thread.currentThread().getContextClassLoader());
        contextHandler2.setHandler(new HelloHandler("2"));

        // add handler after starting server.
        contextHandlerCollection.addHandler(contextHandler2);
        // important! start context explicitly.
        contextHandler2.start();

        System.out.println("Press enter to exit.");
        System.in.read();

        server.stop();

    }

    public class HelloHandler extends AbstractHandler {
        String string;

        public HelloHandler(String string) {
            this.string = string;
        }

        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException {
            response.setContentType("text/html;charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);
            baseRequest.setHandled(true);
            response.getWriter().println("<h1>Hello World" + string + "</h1>");
        }
    }
}

关于java - jetty 9(嵌入式): Adding handlers during runtime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20043097/

相关文章:

java - Android相机通过OpenGL编码

java - Ubuntu中如何配置环境变量?

java - 如何在 jsonb postgresql 中查询并转换为谓词 JPA

java - 关于 GWT 和 dockpanel 的问题,大小和调整大小

configuration - jetty -设置系统属性

java - 如何在提供文件之前使用嵌入式 Jetty 转换文件?

scala - 在 SBT 0.10 中指定 Jetty 端口

java - 如何从 servlet 中获取 org.eclipse.jetty.server.Request

java - 如何在JETTY服务器上部署war文件

java - Jetty 的多个 webroot 文件夹