Camel JPA Transaction + EntityManager

时间:2017-06-07 16:56:28

标签: apache-camel spring-transactions camel-jpa

在我的实际应用程序中,我有一个使用JPA根据某些业务规则持久保存数据的业务层,问题是camel-jpa事务不与业务层事务共享。我需要在业务类中将我的EntityManager与Camel事务范围集成,我该怎么做?

下面是一个简单的例子,但这反映了实际设计中的问题。

Project example

服务类

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

驼context.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>

1 个答案:

答案 0 :(得分:1)

我刚刚在上下文中添加了这个代码段,但它确实有效。

<bean class="org.apache.camel.spring.spi.SpringTransactionPolicy" id="requiredPolicy">
    <property name="transactionManager" ref="jpaTxManager"/>
    <property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/>
</bean>