Embedded jetty Jetty:使用WebAppProvider将对象从主方法传递到Servlet

Embedded jetty Jetty:使用WebAppProvider将对象从主方法传递到Servlet,embedded-jetty,jetty-9,Embedded Jetty,Jetty 9,我需要在一个servlet中使用的主方法中从嵌入式jetty代码传递一个对象 这是一个问题,因为WebAppContext中使用了单独的类加载器——否则我只会使用静态变量 我的主代码设置如下: Server=newserver(); //在此处设置连接器。。。 ContextHandlerCollection contexts=新建ContextHandlerCollection(); RequestLogHandler RequestLogHandler=新的RequestLogHandler

我需要在一个servlet中使用的主方法中从嵌入式jetty代码传递一个对象

这是一个问题,因为WebAppContext中使用了单独的类加载器——否则我只会使用静态变量

我的主代码设置如下:

Server=newserver();
//在此处设置连接器。。。
ContextHandlerCollection contexts=新建ContextHandlerCollection();
RequestLogHandler RequestLogHandler=新的RequestLogHandler();
HandlerCollection handlers=新的HandlerCollection();
setHandlers(新处理程序[]{context,新DefaultHandler(),requestLogHandler});
setHandler(instrumentedHandler(handlers,metrics));
addRequestLogging(requestLogHandler);
DeploymentManager DeploymentManager=新的DeploymentManager();
deploymentManager.setContexts(上下文);
WebAppProvider WebAppProvider=新的WebAppProvider();
setmonitoredirName(jettyHome+“/webapps”);
webAppProvider.setParentLoaderPriority(false);
webAppProvider.setExtractWars(true);
webAppProvider.setScanInterval(1);
setDefaultsDescriptor(jettyHome+“/webdefault.xml”);
setConfigurationManager(新属性Configuration Manager());
deploymentManager.addAppProvider(webAppProvider);
addBean(deploymentManager);
//尝试在服务器上设置度量-但我无法在Servlet中访问它们
setAttribute(MetricRegistry.class.getName(),metrics);
server.start();
join();
我尝试了一些这方面的东西,但都不起作用。具体来说,servlet上下文中没有设置
org.eclipse.jetty.server.server
属性


(特别是,我正在尝试在jetty对象上设置dropwizard度量,但我的应用程序的其余部分需要相同的MetricRegistry对象,以便我可以将所有度量和报告器放在一起)

使用
DeploymentManager
时,您无法访问
WebAppContext
,或
ServletContextHandler
,或
ContextHandler
在主启动代码期间

相反,您必须使用部署管理器中的工具来提供一个定制的
AppLifeCycle.Binding
,它完成部署阶段所需的工作

另一个好处是,这在热部署(重新部署)期间也能正常工作

下面是一个在嵌入式jetty中的工作示例

package org.eclipse.jetty.cookbook;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.util.HashMap;
导入java.util.Map;
导入org.eclipse.jetty.deploy.App;
导入org.eclipse.jetty.deploy.AppLifeCycle;
导入org.eclipse.jetty.deploy.DeploymentManager;
导入org.eclipse.jetty.deploy.graph.Node;
导入org.eclipse.jetty.deploy.providers.WebAppProvider;
导入org.eclipse.jetty.server.Handler;
导入org.eclipse.jetty.server.server;
导入org.eclipse.jetty.server.handler.ContextHandler;
导入org.eclipse.jetty.server.handler.ContextHandlerCollection;
导入org.eclipse.jetty.server.handler.DefaultHandler;
导入org.eclipse.jetty.server.handler.HandlerCollection;
公共类部署webapps
{
公共静态void main(字符串[]args)引发异常
{
服务器=新服务器(8080);
ContextHandlerCollection contexts=新建ContextHandlerCollection();
HandlerCollection handlers=新的HandlerCollection();
setHandlers(新的处理程序[]{context,新的DefaultHandler()});
setHandler(处理程序);
Path confFile=Path.get(System.getProperty(“user.dir”),“example.conf”);
ContextAttributeCustomizer ContextAttributeCustomizer=新的ContextAttributeCustomizer();
contextAttributeCustomizer.setAttribute(“common.conf”,confFile);
DeploymentManager DeploymentManager=新的DeploymentManager();
deploymentManager.setContexts(上下文);
deploymentManager.addLifeCycleBinding(contextAttributeCustomizer);
字符串jettyBaseProp=System.getProperty(“jetty.base”);
if(jettyBaseProp==null)
{
抛出新的FileNotFoundException(“缺少系统属性'jetty.base'”);
}
Path jettyBase=新文件(jettyBaseProp).toPath().toabsolutionPath();
WebAppProvider WebAppProvider=新的WebAppProvider();
webAppProvider.setmonitoredirname(jettyBase.resolve(“webapps”).toString());
deploymentManager.addAppProvider(webAppProvider);
addBean(deploymentManager);
//让我们在启动后转储服务器。
//我们可以查找已部署的上下文,以及
//“处理程序属性”转储部分中ContextAttributesCustomizer的结果
server.setDumpAfterStart(true);
server.start();
join();
}
公共静态类ContextAttributeCustomizer实现AppLifeCycle.Binding
{
public final Map attributes=new HashMap();
public void setAttribute(字符串名称、对象值)
{
this.attributes.put(名称、值);
}
@凌驾
公共字符串[]getBindingTargets()
{
返回新字符串[]{AppLifeCycle.DEPLOYING};
}
@凌驾
public void processBinding(节点、应用程序)引发异常
{
ContextHandler=app.getContextHandler();
if(handler==null)
{
抛出新的NullPointerException(“没有为App:+App创建处理程序”);
}
attributes.forEach((名称,值)->handler.setAttribute(名称,值));
}
}
}