[英]Embedded Jetty with jetty env file not working
我一直在尝试设置嵌入式Jetty服务器(Jetty版本9.3.7),并且在jetty-env文件方面遇到了一些困难。
我的应用程序已成功部署在多台服务器上,因此我知道问题必须与码头配置有关。
我已经看过这个问题 ,并基于答案回答了当前的实现,但是它没有用。
我有以下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码头插件来启动码头服务器,因此知道此配置有效。
下面是我试图用来创建码头服务器的类:
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]
就像我说的,如果我使用相同的配置从配置启动码头服务器,那么就没有问题,而且我得到了服务器,所以我认为问题一定与我的JettyServer
类有关,但是我找不到在线内容指出我在做什么错。
有人可以发现明显的东西吗?
最后,我看了一下gradle 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);
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();
}
}
我想我的项目设置必须有点不正确,这意味着默认配置无法正常工作。 如果有人可以告诉我其他任何选择,我暂时不会接受此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.