【问题标题】:Persistence Unit not available in JNDI when camel route starts当骆驼路线开始时,持久性单元在 JNDI 中不可用
【发布时间】:2017-10-03 14:56:27
【问题描述】:

我正在尝试使用持久性单元在骆驼路线中配置我的 JPA 组件。

我正在使用 EAP 6.4 和 Fuse 6.3。

现在这条路线真的很基本,只是为了让它与我的数据源一起工作:

private UserTransaction userTransaction;
private JpaEndpoint jpaEndpoint;
private JaxbDataFormat jaxbDataFormat;

@Override
public void configure() throws Exception {
    // @formatter:off
    configureErrorHandling();

    configureJpa();

    from("timer:startup?repeatCount=1")
    .to(jpaEndpoint)
    .process(exchange -> {
        Exchange ex = exchange;
    });

    // @formatter:on
}

private void configureJpa() {
    // Configure our JaxbDataFormat to point at our 'model' package
    EntityManagerFactory entityManagerFactory = null;
    try {
        userTransaction = InitialContext.doLookup("java:jboss/UserTransaction");
        entityManagerFactory = InitialContext.doLookup("java:/UtskicksbegaranOraclePU");
    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    jaxbDataFormat = new JaxbDataFormat();
    jaxbDataFormat.setContextPath(UtskicksbegaranRequestOracle.class.getPackage().getName());

    // Configure a JtaTransactionManager by looking up the JBoss transaction manager from JNDI

    JtaTransactionManager transactionManager = new JtaTransactionManager(userTransaction);
    transactionManager.afterPropertiesSet();

    // Configure the JPA endpoint to use the correct EntityManagerFactory and JtaTransactionManager
    jpaEndpoint = new JpaEndpoint();
    jpaEndpoint.setCamelContext(getContext());
    jpaEndpoint.setConsumeDelete(false);
    jpaEndpoint.setMaximumResults(500);
    jpaEndpoint.setQuery(consumerQuery);
    jpaEndpoint.setEntityType(UtskicksbegaranRequestOracle.class);
    jpaEndpoint.setEntityManagerFactory(entityManagerFactory);
    jpaEndpoint.setTransactionManager(transactionManager);
}

这里的问题是,当我在我的服务器上启动它时,这一行: entityManagerFactory = InitialContext.doLookup("java:/UtskicksbegaranOraclePU"); 抛出javax.naming.NameNotFoundException: UtskicksbegaranOraclePU -- service jboss.naming.context.java.UtskicksbegaranOraclePU

以上代码大部分取自 Jboss 打包的快速入门。

当我在没有使用或配置路由中的 JPA 组件的情况下启动服务器时,我可以访问我的服务器并看到我的持久性单元已在 JNDI 中注册。

这是我的 persistence.xml 的样子:

<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="UtskicksbegaranOraclePU" transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/xa/utskicksbegaran</jta-data-source>
        <class>classes</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.default_schema" value="icc" />
            <property name="jboss.entity.manager.factory.jndi.name" value="java:/UtskicksbegaranOraclePU"/>
        </properties>
    </persistence-unit>
</persistence>

基本上,我想要实现的是从我的数据源进行 XA 事务提取,然后将其分发到不同的队列。

我正在使用 camel-spring 启动路线,这是我的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans                  
        http://www.springframework.org/schema/beans/spring-beans.xsd                    
        http://camel.apache.org/schema/spring                  
        http://camel.apache.org/schema/spring/camel-spring.xsd>
    <camelContext id="camelContext_utskicksbegaran-oracle-adapter" xmlns="http://camel.apache.org/schema/spring">
        <packageScan>
            <package>se.sjv.integration.camel.hanterautskick</package>
        </packageScan>
    </camelContext>
</beans>

还有其他方法可以使用 Spring 访问在 persistence.xml 中声明的持久性单元吗?我错过了什么?

【问题讨论】:

    标签: java jpa apache-camel persistence jndi


    【解决方案1】:

    终于解决了。
    它与服务器启动时的加载顺序有关。 出于某种原因,使用 jboss-deployment-structure.xml 并没有解决这个问题。我现在正在使用WEB-INF/jboss-all.xml 并且有问题的文件包含以下内容,我的骆驼集成在开始之前取决于:

    <?xml version="1.0" encoding="UTF-8"?>
    <jboss umlns="urn:jboss:1.0">
        <jboss-deployment-dependencies xmlns="urn:jboss:deployment-dependencies:1.0">
            <dependency name="activemq-rar.rar" />
            <dependency name="ojdbc6.jar" />
        </jboss-deployment-dependencies>
    </jboss>
    

    一切顺利。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2017-07-06
      • 2016-04-21
      • 2020-12-17
      • 2018-05-30
      相关资源
      最近更新 更多