带有 jetty env 文件的嵌入式 Jetty 无法正常工作

Embedded Jetty with jetty env file not working

我一直在尝试设置嵌入式 Jetty 服务器(Jetty 版本 9.3.7),但在使用 jetty-env 文件时遇到了一些困难。

我的应用程序已经成功部署在多台服务器上,所以我知道问题一定与jetty配置有关。

我查看了 this question,并根据答案进行了当前实施,但它不起作用。

我有以下 src/main/resources/WEB-INF/jetty-env.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <New id="datasource" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>jdbc/datasource</Arg>
    <Arg>
      <New class="org.apache.commons.dbcp.BasicDataSource">
        <Set name="url">jdbc:h2:tcp://localhost:9092/build/db/testdb;USER=sa</Set>
      </New>
    </Arg>
  </New>
</Configure>

我还有一个 src/main/resources/WEB-INF/web.xml 文件,其中包含以下片段:

<resource-ref>
    <description>TESTDB</description>
    <res-ref-name>jdbc/datasource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

我可以使用 Gradle jetty 插件来启动 jetty 服务器,所以知道这个配置是有效的。

下面是我试图用来创建码头服务器的class:

public class JettyServer {

    private static final Logger LOGGER = LoggerFactory.getLogger(JettyServer.class);

    public static void main(final String[] args) throws Exception {
        LOGGER.debug("Starting jetty server");
        int port = 8083;
        LOGGER.debug("Setting the port to {}", port);
        final Server server = new Server(port);
        Configuration.ClassList classList = Configuration.ClassList.serverDefault(server);
        classList.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration",
            "org.eclipse.jetty.plus.webapp.PlusConfiguration");
        final WebAppContext root = new WebAppContext();
        root.setContextPath("/");
        root.setResourceBase(".");
        root.setDescriptor(JettyServer.class.getResource("/WEB-INF/web.xml").toString());
        server.setHandler(root);
        server.start();
        server.join();
    }
}

JNDI 名称在 spring 配置中被引用 java class:

@Bean
public DataSource dataSource() throws NamingException {
    final JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
    jndiObjectFactoryBean.setJndiName("java:comp/env/jdbc/datasource");
    jndiObjectFactoryBean.setResourceRef(true);
    jndiObjectFactoryBean.setProxyInterface(DataSource.class);
    jndiObjectFactoryBean.afterPropertiesSet();
    return (DataSource)jndiObjectFactoryBean.getObject();
}

当我尝试启动我的应用程序时,出现以下错误:

Caused by: javax.naming.NameNotFoundException: null
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:532) ~[jetty-jndi-9.3.7.v20160115.jar:9.3.7.v20160115]
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:563) ~[jetty-jndi-9.3.7.v20160115.jar:9.3.7.v20160115]
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:578) ~[jetty-jndi-9.3.7.v20160115.jar:9.3.7.v20160115]
at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:106) ~[jetty-jndi-9.3.7.v20160115.jar:9.3.7.v20160115]
at javax.naming.InitialContext.lookup(InitialContext.java:417) ~[na:1.8.0_73]
at org.springframework.jndi.JndiTemplate.doInContext(JndiTemplate.java:154) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiObjectTargetSource.afterPropertiesSet(JndiObjectTargetSource.java:97) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiObjectFactoryBean$JndiObjectProxyFactory.createJndiObjectProxy(JndiObjectFactoryBean.java:315) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiObjectFactoryBean$JndiObjectProxyFactory.access[=14=]0(JndiObjectFactoryBean.java:304) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:200) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE]

正如我所说,如果我使用相同的配置从配置启动码头服务器,则没有问题,我得到了一个服务器,所以我认为问题一定与我的 JettyServer class,但我在网上找不到任何东西来表明我做错了什么。

有人能发现任何明显的东西吗?

最后,我查看了 gradle jetty plugin 的源代码,发现他们添加的配置略有不同。这对我有用,我的服务器现在按预期 运行:

public class JettyServer {

    private static final Logger LOGGER = LoggerFactory.getLogger(JettyServer.class);

    public static void main(final String[] args) throws Exception {
        LOGGER.debug("Starting jetty server");
        int port = 8083;
        LOGGER.debug("Setting the port to {}", port);
        final Server server = new Server(port);
        final WebAppContext root = new WebAppContext();
        root.setContextPath("/");
        root.setResourceBase(".");
        EnvConfiguration envConfiguration = new EnvConfiguration();
        envConfiguration.setJettyEnvXml(JettyServer.class.getResource("/WEB-INF/jetty-env.xml").toURI().toURL());
        //Adding the actual classes instead of just the class names
        root.setConfigurations(new Configuration[]{envConfiguration, new PlusConfiguration(), new WebXmlConfiguration()});
        root.setDescriptor(JettyServer.class.getResource("/WEB-INF/web.xml").toString());
        server.setHandler(root);
        server.start();
        server.join();
    }
}

我想我的项目设置一定有点不正确,这意味着默认配置不起作用。如果有人可以告诉我任何其他选择,我现在不会接受这个答案。