Jetty 9 (embedded): Adding handlers during runtime

前端 未结 2 583
遥遥无期
遥遥无期 2021-01-13 17:33

Is there any way to add handlers to a running embedded Jetty instance? We have migrated an old Jetty 6 based project to Jetty 9 and we need for our plugin system the possibi

2条回答
  •  再見小時候
    2021-01-13 18:05

    With Jetty 9.1.0.v20131115 you can use the mutableWhenRunning flag on HandlerCollection constructor ...

    HandlerCollection coll = new HandlerCollection(true);
    

    This will ignore the isStarted() tests on the collection itself during .setHandlers(Handlers[]) and .addHandler(Handler) calls.

    This behavior is only available for the HandlerCollection itself, you can add individual handlers, or set the entire handler tree without regards to the LifeCycle of the HandlerCollection.

    Eg:

    Server server = new Server(8080);
    HandlerCollection myhandlers = new HandlerCollection(true);
    server.setHandler(myhandlers);
    
    // add some initial handlers
    myhandlers.setHandlers(new Handlers[] { helloHandler, indexHandler });
    
    // start server
    server.start();
    
    // ... at some point later, during runtime
    FooHandler fooHandler = new FooHandler();
    fooHandler.start();
    myhandlers.addHandler(fooHandler);
    
    BarHandler barHandler = new BarHandler();
    barHandler.start();
    myhandlers.addHandler(barHandler);
    

提交回复
热议问题