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 possibility add and remove dynamically handlers...
See the example below...
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)
Note: newHandler
is a HandlerCollection
...
Here a complete code sample. Next to using
HandlerCollection(true)
, it is also important to start the new context handler explicitly.With Jetty 9.1.0.v20131115 you can use the
mutableWhenRunning
flag onHandlerCollection
constructor ...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 theLifeCycle
of theHandlerCollection
.Eg: