Se ha llamado a joinTransaction en un EntityManager de recursos locales en ApacheCamel
-
24-12-2019 - |
Pregunta
Soy nuevo en Apache Camel y estoy probando camel-jpa para sondear desde la tabla y mostrar registros.
La siguiente es la clase principal.
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("LoanServicePU");
CamelContext camelContext = new DefaultCamelContext();
JpaComponent jpa = new JpaComponent();
jpa.setEntityManagerFactory(entityManagerFactory);
JpaTransactionManager myTM=new JpaTransactionManager();
myTM.setEntityManagerFactory(entityManagerFactory);
jpa.setTransactionManager( myTM );
jpa.setCamelContext(camelContext);
camelContext.addRoutes(new JpaRouteBuilder());
camelContext.addComponent("jpa",jpa);
camelContext.start();
Thread.sleep(10000);
camelContext.stop();
System.out.println("Done");
La siguiente es la clase jparouter
public void configure() throws Exception {
from("jpa://com.pns.ab.model.LoanRequest?consumeDelete=false;"
+ "consumer.delay=2000;maxMessagesPerPoll=1000;"
+ "consumer.namedQuery=selectLoanRequests").to("stream:out");
}
Configuré persistence.xml y está en META-INF, de hecho, en eclipse inicio el Proyecto Java y luego configuro la faceta JPA
persistencia.xml
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="LoanServicePU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.pns.ab.model.LoanRequest</class>
<properties>
<property name="eclipselink.target-server" value="None"/>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"/>
<property name="javax.persistence.jdbc.user" value="vs"/>
<property name="javax.persistence.jdbc.password" value="vs"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
Pero recibo el siguiente error:
[main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.12.3 (CamelContext: camel-1) started in 1.426 seconds
[Camel (camel-1) thread #0 - jpa://com.pns.ab.model.LoanRequest] WARN org.apache.camel.component.jpa.JpaConsumer - Consumer Consumer[jpa://com.pns.ab.model.LoanRequest?consumeDelete=false%3Bconsumer.delay%3D2000&consumer.namedQuery=selectLoanRequests] failed polling endpoint: Endpoint[jpa://com.pns.ab.model.LoanRequest?consumeDelete=false%3Bconsumer.delay%3D2000&consumer.namedQuery=selectLoanRequests]. Will try again at next poll. Caused by: [javax.persistence.TransactionRequiredException - joinTransaction has been called on a resource-local EntityManager which is unable to register for a JTA transaction.]
javax.persistence.TransactionRequiredException: joinTransaction has been called on a resource-local EntityManager which is unable to register for a JTA transaction.
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.registerIfRequired(EntityTransactionWrapper.java:91)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.joinTransaction(EntityManagerImpl.java:2081)
Solución
Del registro
EntityManager de recursos local que no puede registrarse para una transacción JTA
Concluyo que la ruta del camello se implementa en un entorno de transacciones JTA pero que en su persistence.xml
puedes usar el predeterminado transaction-type
cual es RESOURCE_LOCAL
en lugar de JTA
.
EDITAR:
Con la siguiente configuración, podría hacerlo funcionar:
No inicies el
EntityManagerFactory
yTransactionManager
usted mismo, sólo haga:final SimpleRegistry registry = new SimpleRegistry(); final CamelContext context = new DefaultCamelContext(registry); context.addRoutes(new JpaSetupRouteBuilder()); context.start();
En
persistence.xml
cambia el nombre de tupersistence-unit
acamel
como:<!-- setting the transaction-type to RESOURCE_LOCAL is optional as this is the default --> <persistence-unit name="camel" transaction-type="RESOURCE_LOCAL">
Sí, lo sé, esto no es muy satisfactorio.
EDITAR:
Si no desea o no puede cambiar el nombre del persistence-unit
a camel
entonces podrías establecer su nombre en el URI usando el persistenceUnit
opción como:
from("jpa://com.pns.ab.model.LoanRequest?consumeDelete=false"
+ "&consumer.delay=2000;maxMessagesPerPoll=1000"
+ "&consumer.namedQuery=selectLoanRequests"
+ "&persistenceUnit=LoanServicePU")
.to("stream:out");
EDITAR:
O alternativamente, use la configuración Spring XML como se describe aquí.