Search code examples
javajpaapache-camelpersistencejndi

Persistence Unit not available in JNDI when camel route starts


I am trying to use a persistence unit to configure my JPA component in a camel route.

I am using EAP 6.4 and Fuse 6.3.

Right now the route is really basic just to get it working against my datasource:

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);
}

The problem here is that when I start this on my server, this line: entityManagerFactory = InitialContext.doLookup("java:/UtskicksbegaranOraclePU"); throws javax.naming.NameNotFoundException: UtskicksbegaranOraclePU -- service jboss.naming.context.java.UtskicksbegaranOraclePU

The above code is mostly taken from a quickstart packaged with Jboss.

When I start the server without using or configuring the JPA component in my route, I can access my server and see that my persistence unit has been registered in JNDI.

Here's what my persistence.xml looks like:

<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>

Basically, what I'm trying to achieve is a XA transacted fetch from my datasource which will then be distributed to different queues.

I'm using camel-spring to start the route, and this is my configuration for that:

<?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>

Is there any other way to access a persistence unit declared in persistence.xml using Spring? What am I missing?


Solution

  • This is finally resolved.
    It had to do with load order on server startup. For some reason, using jboss-deployment-structure.xml did not resolve this. I am now using a WEB-INF/jboss-all.xml and the file in question contains the following, which my camel integration depends on before starting:

    <?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>
    

    Everything runs smoothly with this.