有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java嵌入式Jetty,Jetty env文件不工作

我一直在尝试设置一个嵌入式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服务器,因此知道此配置是有效的

下面是我试图用来创建jetty服务器的类:

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类中引用:

@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$1.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$000(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]

正如我所说的,如果我使用相同的配置从配置启动jetty服务器,那么没有问题,我得到了一个服务器,因此我假设问题一定与我的JettyServer类有关,但我无法在网上找到任何东西来表明我做错了什么

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


共 (1) 个答案

  1. # 1 楼答案

    最后,我看了一下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();
        }
    }
    

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