2016-03-02 83 views
0

我一直在试图建立一个嵌入式码头服务器(码头版本9.3.7),并一直有jetty-env文件的一些困难。嵌入式码头与码头环境文件不起作用

我的应用程序已经成功部署在多个服务器上,所以我知道这个问题必须与jetty config有关。

我已经看过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> 

我能够使用摇篮码头插件启动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] 

正如我所说,如果我使用相同的配置s从配置tart一个码头服务器,没有问题,我得到一个服务器,所以我认为这个问题必须涉及我的JettyServer类,但我找不到任何东西在线,以指示我做错了什么。

任何人都可以发现任何明显的东西吗?

回答

0

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

我想我必须有我的项目设置不正确略,这意味着默认配置不工作。现在我不会接受这个答案,以防有人告诉我任何其他的选择。