2017-06-07 3 views
0

In meiner eigentlichen Anwendung, ich habe eine Business-Schicht, die JPA verwendet, um Daten bestehen nach Ansicht einiger Geschäftsregeln, das Problem ist, dass die Kamel-JPA Transaktion nicht mit der Business-Schicht-Transaktion geteilt. Ich muss meinen EntityManager in Business-Klassen in den Camel-Transaktionsbereich integrieren. Wie kann ich das tun?Camel JPA Transaktion + EntityManager

Unten ist ein einfaches Beispiel, aber das spiegelt das Problem in dem aktuellen Design.

Project example

Service-Klasse

@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()+"]"); 


    } 

Kamel Strecke

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

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

Antwort

0

ich diesen Schnipsel in Zusammenhang gerade hinzugefügt und i Das hat funktioniert.

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