在我的实际应用中,我有一个业务层使用JPA根据一些业务规则来持久化数据,问题是camel-jpa事务没有与业务层事务共享。我需要业务类中的 EntityManager 与 Camel 事务范围集成,我该怎么做?
下面是一个简单的例子,但这反映了实际设计中的问题。
服务等级
@Component
public class MyService {
@PersistenceContext(unitName="persistenceUnit") private EntityManager em;
private static final Logger LOG = LoggerFactory.getLogger(MyService.class);
public void recordLog(@Body Client client) {
LOG.info("Inserting LogClient");
LogClient log = new LogClient();
log.setClient(client);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String strDate = sdf.format(new Date());
log.setTxtLog("Persisted Client ["+client.getName()+"] at ["+strDate+"]");
em.merge(log);
LOG.info("Inserted LogClient ["+log.getId()+"]");
}
骆驼路线
@Component
public class CamelRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("jpa:com.mycompany.model.Client?persistenceUnit=persistenceUnit&consumeDelete=false&consumer.delay=15000").transacted()
.split().simple("${body}")
.log("Processing Client [${body.name}]")
.bean(MyService.class, "recordLog")
.log("${body.name} processed");
}
}
骆驼上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
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://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<osgix:cm-properties id="parametros.spring" persistent-id="parametros.spring">
<prop key="db.driverClassName">org.postgresql.Driver</prop>
<prop key="db.url">jdbc:postgresql://192.168.238.1:5432/camel-jpa</prop>
<prop key="db.username">camel-jpa</prop>
<prop key="db.password">123456</prop>
<prop key="connection.show_sql">true</prop>
</osgix:cm-properties>
<ctx:property-placeholder properties-ref="parametros.spring"/>
<bean class="com.mycompany.routes.CamelRoute" id="javaCamelRoute"/>
<ctx:annotation-config/>
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" id="jpaAdapter">
<property name="showSql" value="${connection.show_sql}"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/>
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
<property name="driverClassName" value="${db.driverClassName}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<bean class="org.apache.camel.component.jpa.JpaComponent" id="jpa">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="transactionManager" ref="jpaTxManager"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="jpaTxManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaAdapter"/>
</bean>
<camelContext id="amq-example-context"
xmlns="http://camel.apache.org/schema/spring" xmlns:order="http://com.mycompany/examples/order">
<propertyPlaceholder id="properties" location="ref:parametros.spring"/>
<routeBuilder ref="javaCamelRoute"/>
</camelContext>
</beans>