Java 已在ApacheCamel中的资源本地EntityManager上调用joinTransaction
我是apache camel的新手,我正在测试camel jpa以从表和显示记录进行轮询 下面是主要课程Java 已在ApacheCamel中的资源本地EntityManager上调用joinTransaction,java,jpa,eclipselink,apache-camel,integration-patterns,Java,Jpa,Eclipselink,Apache Camel,Integration Patterns,我是apache camel的新手,我正在测试camel jpa以从表和显示记录进行轮询 下面是主要课程 EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("LoanServicePU"); CamelContext camelContext = new DefaultCamelContext(); JpaComponent jpa = new JpaCo
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");
以下是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");
}
我在META-INF下配置了persistence.xml和它,事实上在eclipse中我启动了Java项目,然后设置了JPA方面
persistence.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>
从日志中
无法注册JTA事务的资源本地EntityManager
我的结论是驼峰路由被部署到JTA事务环境中,但是在persistence.xml中,您可以使用默认的事务类型,即RESOURCE_LOCAL,而不是JTA
编辑:
通过以下设置,我可以使其正常工作:
不要自己初始化EntityManagerFactory和TransactionManager,只需执行以下操作:
final SimpleRegistry registry = new SimpleRegistry();
final CamelContext context = new DefaultCamelContext(registry);
context.addRoutes(new JpaSetupRouteBuilder());
context.start();
在persistence.xml中,将持久性单元重命名为camel,例如:
编辑:
或者,也可以使用所述的Spring XML设置。是的,我使用的是RESOURCE\u LOCAL,因为它是独立的应用程序,没有部署在服务器上。如何为JTA配置JDBC属性。JTA询问数据源您是否可以在问题中添加persistence.xml?
final SimpleRegistry registry = new SimpleRegistry();
final CamelContext context = new DefaultCamelContext(registry);
context.addRoutes(new JpaSetupRouteBuilder());
context.start();
<!-- setting the transaction-type to RESOURCE_LOCAL is optional as this is the default -->
<persistence-unit name="camel" transaction-type="RESOURCE_LOCAL">
from("jpa://com.pns.ab.model.LoanRequest?consumeDelete=false"
+ "&consumer.delay=2000;maxMessagesPerPoll=1000"
+ "&consumer.namedQuery=selectLoanRequests"
+ "&persistenceUnit=LoanServicePU")
.to("stream:out");