【问题标题】:Jetty uberjar jndi datasource configurationJetty uberjar jndi 数据源配置
【发布时间】:2013-01-08 04:15:34
【问题描述】:

更具体地说,我得到了包含所有依赖项的可执行战争。 应用程序在没有 jndi 数据源的情况下启动,就像使用 jndi 数据源运行它的魅力一样,但失败了。 我相信在这样的配置中没有jetty.xml的位置,所以jetty-env.xml也不行,因为jetty默认不会读取它。我尝试使用 jetty-web.xml 进行 jndi 数据源配置,但 jetty 无法部署应用程序并返回 503 错误代码。 我正在使用 Jetty9-M4。 尝试了 tomcat pooling 和 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 在 jetty-web 阶段为时已晚,或者配置是错误的。

抱歉英语不好。


忘了说我使用 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>

也不行。


作为一个小注解 jetty-web.xml 放在你的战争的 WEB-INF 中对于 JNDI 资源配置很好,默认情况下使用它。

【问题讨论】:

    标签: java jetty datasource jndi uberjar


    【解决方案1】:

    jetty-env.xml 文件应自动从 WEB-INF/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/

    【讨论】:

    • 用相关信息更新了问题。 wiki 还提到默认情况下,jetty 不会读取 jetty-env.xml,并建议进行 jetty.xml 调整,我在尝试 jetty-web.xml 设置之前玩过 jetty-env.xml,我相信它确实被忽略了。
    • 这一次使用jetty-env.xml 的结果是503。我相信它与配置有关。
    • 包含EnvConfiguration 后是否使用了jetty-env.xml?您能否发布您收到的错误消息?检查您的日志文件和/或标准输出
    • 我解决了这个问题。修复了 maven-shade 和 war 配置(ClassLoader 中有重复项)。对于注释 &lt;property name="hibernate.connection.datasource"&gt;jdbc/testDS&lt;/property&gt; 是一个休眠字符串(尽管它在问题的 2 或 3 中提到)。感谢@barry 的帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    相关资源
    最近更新 更多