嵌入带有模块化XmlConfiguration的Jetty 9.3 [英] Embedding Jetty 9.3 with modular XmlConfiguration

查看:129
本文介绍了嵌入带有模块化XmlConfiguration的Jetty 9.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Jetty 8.1.17迁移到Jetty 9.3.9.我们的应用程序 嵌入 码头.以前,我们只有一个XML配置文件jetty.xml,其中包含我们需要的所有内容.

我认为使用Jetty 9.3.9可以更好地使用他们建议的模块化方法,到目前为止,我的$JETTY_HOME/etc中有jetty.xmljetty-http.xmljetty-https.xmljetty-ssl.xml.这些几乎是9.3.9发行版中的副本.当我使用start.jar而不是通过我自己的嵌入Jetty的代码时,这似乎工作得很好.

理想情况下,我希望能够扫描$JETTY_HOME/etc文件夹中的所有jetty xml文件并加载配置.但是对于嵌入式模式,由于它们之间的<ref id="x"/>依赖关系等原因,我没有找到一种无需明确定义文件加载顺序的方法.

我的最初尝试基于如何以编程方式启动具有多个配置文件的码头服务器?如下所示:

final List<Object> configuredObjects = new ArrayList();
XmlConfiguration last = null;
for(final Path confFile : configFiles) {
    logger.info("[loading jetty configuration : {}]", confFile.toString());
    try(final InputStream is = Files.newInputStream(confFile)) {
        final XmlConfiguration configuration = new XmlConfiguration(is);
        if (last != null) {
            configuration.getIdMap().putAll(last.getIdMap());
        }
        configuredObjects.add(configuration.configure());
        last = configuration;
    }
}

Server server = null;
// For all objects created by XmlConfigurations, start them if they are lifecycles.
for (final Object configuredObject : configuredObjects) {
    if(configuredObject instanceof Server) {
        server = (Server)configuredObject;
    }

    if (configuredObject instanceof LifeCycle) {
        final LifeCycle lc = (LifeCycle)configuredObject;
        if (!lc.isRunning()) {
            lc.start();
        }
    }
}

但是,如果在jetty-ssl.xml之前加载jetty-https.xml,或者如果我在jetty.xml中放置了来自子配置jetty-blah.xml的对象的引用,该对象尚未首先加载,则启动时会出现异常. >

在我看来,当您调用java -jar start.jar时,Jetty本身可以做到这一点,所以我想让Jetty不在乎解析配置文件的顺序是什么?

解决方案

在加载Jetty XML文件时,订单非常重要.

这是整个start.jar及其模块系统所要解决的问题,具有适当的属性集,服务器类路径是合理的,并确保XML的正确加载顺序.

注意:不可能同时加载${jetty.home}/etc中的所有内容,因为您会在常见技术的替代实现上遇到冲突(start.jar也会为您管理)

I am migrating from Jetty 8.1.17 to Jetty 9.3.9. Our application embeds Jetty. Previously we had a single XML configuration file jetty.xml which contained everything we needed.

I felt that with Jetty 9.3.9 it would be much nicer to use the modular approach that they suggest, so far I have jetty.xml, jetty-http.xml, jetty-https.xml and jetty-ssl.xml in my $JETTY_HOME/etc; these are pretty much copies of those from the 9.3.9 distribution. This seems to work well when I use start.jar but not through my own code which embeds Jetty.

Ideally I would like to be able to scan for any jetty xml files in the $JETTY_HOME/etc folder and load the configuration. However for embedded mode I have not found a way to do that without explicitly defining the order that those files should be loaded in, due to <ref id="x"/> dependencies between them etc.

My initial attempt is based on How can I programmatically start a jetty server with multiple configuration files? and looks like:

final List<Object> configuredObjects = new ArrayList();
XmlConfiguration last = null;
for(final Path confFile : configFiles) {
    logger.info("[loading jetty configuration : {}]", confFile.toString());
    try(final InputStream is = Files.newInputStream(confFile)) {
        final XmlConfiguration configuration = new XmlConfiguration(is);
        if (last != null) {
            configuration.getIdMap().putAll(last.getIdMap());
        }
        configuredObjects.add(configuration.configure());
        last = configuration;
    }
}

Server server = null;
// For all objects created by XmlConfigurations, start them if they are lifecycles.
for (final Object configuredObject : configuredObjects) {
    if(configuredObject instanceof Server) {
        server = (Server)configuredObject;
    }

    if (configuredObject instanceof LifeCycle) {
        final LifeCycle lc = (LifeCycle)configuredObject;
        if (!lc.isRunning()) {
            lc.start();
        }
    }
}

However, I get Exceptions at startup if jetty-https.xml is loaded before jetty-ssl.xml or if I place a reference in jetty.xml to an object from a sub-configuration jetty-blah.xml which has not been loaded first.

It seems to me like Jetty manages to do this okay itself when you call java -jar start.jar, so what am I missing to get Jetty to not care about what order the config files are parsed in?

解决方案

Order is extremely important when loading the Jetty XML files.

That's the heart of what the entire start.jar and its module system is about, have an appropriate set of properties, the server classpath is sane, and ensuring proper load order of the XML.

Note: its not possible to have everything in ${jetty.home}/etc loaded at the same time, as you will get conflicts on alternate implementations of common technologies (something start.jar also manages for you)

这篇关于嵌入带有模块化XmlConfiguration的Jetty 9.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆