I need to pass an object from the embedded jetty code in a main method to be used in a servlet.
This is an issue because of the separate classloader used within the WebAppContext - otherwise I would just use a static variable.
My main code sets things up like this:
Server server = new Server();
// setup connectors here...
ContextHandlerCollection contexts = new ContextHandlerCollection();
RequestLogHandler requestLogHandler = new RequestLogHandler();
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
server.setHandler(instrumentedHandler(handlers, metrics));
addRequestLogging(requestLogHandler);
DeploymentManager deploymentManager = new DeploymentManager();
deploymentManager.setContexts(contexts);
WebAppProvider webAppProvider = new WebAppProvider();
webAppProvider.setMonitoredDirName(jettyHome + "/webapps");
webAppProvider.setParentLoaderPriority(false);
webAppProvider.setExtractWars(true);
webAppProvider.setScanInterval(1);
webAppProvider.setDefaultsDescriptor(jettyHome + "/webdefault.xml");
webAppProvider.setConfigurationManager(new PropertiesConfigurationManager());
deploymentManager.addAppProvider(webAppProvider);
server.addBean(deploymentManager);
// Attempt to set the metrics on the server - but I can't access them in the Servlet
server.setAttribute(MetricRegistry.class.getName(), metrics);
server.start();
server.join();
I tried a few things from this question, but they did not work. Specifically, there is no org.eclipse.jetty.server.Server
attribute set on the servlet context.
(Specifically, I am trying to setup dropwizard metrics on the jetty objects, but I need the same MetricRegistry object for the rest of my application so I can keep all my metrics and reporters together)