更具体地说,我得到了包含所有依赖项的可执行war。但是,应用程序在没有jndi数据源的情况下启动,就像一个魅力,用jndi数据源运行它失败了。我认为在这样的配置中没有jetty.xml的位置,所以jettyenv.xml也不能运行,因为jetty默认不会读取它。我试图使用jettyweb.xml进行jndi数据源配置,但是jetty未能部署应用程序,返回503错误代码。我用的是Jetty9-M4。尝试使用tomcat池和BoneCP,结果是相同的。
入门班:
import java.net.URL;
import java.security.ProtectionDomain;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.webapp.WebAppContext;
public final class Loader {
public static void main(String[] args) throws Exception
{
String jetty_home = "..";
int appli_port = 8080;
Server server = new Server(appli_port);
ProtectionDomain protectionDomain = Loader.class.getProtectionDomain();
URL location = protectionDomain.getCodeSource().getLocation();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(location.toExternalForm());
server.setHandler(webapp);
server.start();
server.join();
}
}
jetty-web.xml:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="testDS" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/testDS</Arg>
<Arg>
<New class="com.jolbox.bonecp.BoneCPDataSource">
<Set name="driverClass">org.postgresql.Driver</Set>
<Set name="jdbcUrl">jdbc:postgresql://localhost:5432/testDS</Set>
<Set name="username">name</Set>
<Set name="password">password</Set>
<Set name="minConnectionsPerPartition">5</Set>
<Set name="maxConnectionsPerPartition">50</Set>
<Set name="acquireIncrement">5</Set>
<Set name="idleConnectionTestPeriod">30</Set>
</New>
</Arg>
</New>
</Configure>
我怀疑jndi在jndi发布到jndi(Jndi)到jndi的阶段或配置都是错误的。
抱歉英语不太好。
忘记提到我正在使用Hibernate作为ORM。
web.xml包含:
<resource-ref>
<description>Postgres datasource</description>
<res-ref-name>jdbc/testDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Hibernate.cfg.xml包含
<property name="hibernate.connection.datasource">java:comp/env/jdbc/testDS</property>
已尝试
<property name="hibernate.connection.datasource">jdbc/testDS</property>
也不起作用。
请注意,放置在war的jetty-web.xml
中的JNDI资源配置是可以的,默认情况下使用它。
发布于 2013-01-24 12:27:22
jetty-env.xml
文件应该自动从WEB/jetty-env.xml中获取,参见这里的http://www.eclipse.org/jetty/documentation/current/jetty-env-xml.html。我建议使用jetty-env.xml
而不是jetty-web.xml
。
您还可以从应用程序中显示如何执行JNDI查找吗?也许JNDI数据源被正确初始化了,但是JNDI名称不正确?
这里有一些JNDI示例:http://www.eclipse.org/jetty/documentation/current/jndi-datasource-examples.html
更新:似乎Jetty在运行嵌入式时不会自动拾取它。试试这个:
public static void main(String[] args) throws Exception
{
String jetty_home = "..";
int appli_port = 8080;
Server server = new Server(appli_port);
ProtectionDomain protectionDomain = Loader.class.getProtectionDomain();
URL location = protectionDomain.getCodeSource().getLocation();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(location.toExternalForm());
// setup JNDI
EnvConfiguration envConfiguration = new EnvConfiguration();
URL url = new File("path to jetty-env.xml").toURI().toURL();
envConfiguration.setJettyEnvXml(url);
webapp.setConfigurations(new Configuration[] {new WebInfConfiguration(), envConfiguration, new WebXmlConfiguration()});
server.setHandler(webapp);
server.start();
server.join();
}
(见http://blog.armhold.com/2011/12/27/how-to-get-jndi-working-with-wicket-1-5-and-jetty-7-5/)
https://stackoverflow.com/questions/14500809
复制相似问题