1 2 Previous Next 17 Replies Latest reply on Apr 20, 2012 5:35 AM by gigazhang Go to original post
      • 15. Re: Drools, Spring integration and JTA in container
        rodri

        Hi Alex Manly:

         

        Could you please share any sample working example with the final configuration files?

        Also when you say 'overriding the SingleSessionCommandService', is it better to modify 'org.drools.persistence.session.SingleSessionCommandService' class and repackage the drools-persistence-jpa.jar file instead.

        • 16. Re: Drools, Spring integration and JTA in container
          alexmanly

          Hi Rodri,

           

          Here is my drools spring configuration file:

           

          {code:xml}

          <?xml version="1.0" encoding="UTF-8"?>

          <beans xmlns="http://www.springframework.org/schema/beans"

              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

              xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

              xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"

              xmlns:util="http://www.springframework.org/schema/util" xmlns:drools="http://drools.org/schema/drools-spring"

              xmlns:camel="http://camel.apache.org/schema/spring"

              xsi:schemaLocation="

                 http://www.springframework.org/schema/beans

                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

                 http://www.springframework.org/schema/tx

                 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

                 http://www.springframework.org/schema/aop

                 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

                 http://www.springframework.org/schema/context

                 http://www.springframework.org/schema/context/spring-context-3.0.xsd

                 http://www.springframework.org/schema/jee

                 http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

                 http://www.springframework.org/schema/util

                 http://www.springframework.org/schema/util/spring-util-3.0.xsd

                 http://drools.org/schema/drools-spring

                 http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring.xsd

                 http://camel.apache.org/schema/spring

                 http://camel.apache.org/schema/spring/camel-spring.xsd">

           

              <context:annotation-config />

              <context:property-placeholder location="classpath:/app_default.properties"/>

           

              <jee:jndi-lookup id="dataSource" jndi-name="jdbc/appDS_V1" />

              <jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/app-domain-pu" />

              <tx:jta-transaction-manager />

              <tx:annotation-driven transaction-manager="transactionManager"/>

           

              <context:component-scan base-package="com.company.app" />

           

              <drools:kbase id="kbase1">

                  <drools:resources>

                      <drools:resource id="cloneFlow" type="DRF" source="classpath:Clone.rf"/>

                      <drools:resource id="approvalFlow" type="DRF" source="classpath:Approval.rf"/>

                    </drools:resources>

           

                  <drools:configuration>

                      <drools:mbeans enabled="true" />

                      <drools:event-processing-mode mode="STREAM" />

                  </drools:configuration>

              </drools:kbase>

           

              <drools:ksession id="jpaSingleSessionCommandService" type="stateful" kbase="kbase1" name="stateful-session">

                  <drools:configuration>           

                      <drools:jpa-persistence>

                          <drools:transaction-manager ref="transactionManager" />

                          <drools:entity-manager-factory ref="entityManagerFactory" />

                          <drools:variable-persisters>

                              <drools:persister for-class="javax.persistence.Entity" implementation="org.drools.persistence.processinstance.persisters.JPAVariablePersister"/>

                              <drools:persister for-class="java.io.Serializable" implementation="org.drools.persistence.processinstance.persisters.SerializableVariablePersister"/>

                          </drools:variable-persisters>

                      </drools:jpa-persistence>

                      <drools:work-item-handlers>

                          <drools:work-item-handler name="Human Task" ref="humanTaskHandler"/>

                      </drools:work-item-handlers>

                      <drools:keep-reference enabled="true" />

                      <drools:clock-type type="REALTIME" />

                  </drools:configuration>

                  <drools:script>

                      <drools:fire-all-rules/>

                  </drools:script>

              </drools:ksession>

           

          </beans>

          {code:xml}

           

          I am using drools version 5.1.1, here is my maven config:

           

          {code:xml}

          <dependency>

                      <groupId>org.drools</groupId>

                      <artifactId>drools-core</artifactId>

                      <version>5.1.1</version>

                      <scope>compile</scope>

                  </dependency>

                  <dependency>

                      <groupId>org.drools</groupId>

                      <artifactId>drools-api</artifactId>

                      <version>5.1.1</version>

                      <scope>compile</scope>

                  </dependency>

                  <dependency>

                      <groupId>org.drools</groupId>

                      <artifactId>drools-compiler</artifactId>

                      <version>5.1.1</version>

                      <scope>compile</scope>

                  </dependency>

                  <dependency>

                      <groupId>org.drools</groupId>

                      <artifactId>drools-persistence-jpa</artifactId>

                      <version>5.1.1</version>

                      <scope>compile</scope>

                   </dependency>

                  <dependency>

                      <groupId>org.drools</groupId>

                      <artifactId>drools-spring</artifactId>

                      <version>5.1.1</version>

                      <scope>compile</scope>

                  </dependency>

                  <dependency>

                      <groupId>com.thoughtworks.xstream</groupId>

                      <artifactId>xstream</artifactId>

                      <version>1.3.1</version>

                      <scope>compile</scope>

                  </dependency>

          {code:xml}

           

          In my web application I copied the java file: org.drools.persistence.session.SingleSessionCommandService from the drools SVN site and then overriden and modified the method initTransactionManager (as long as this is higher up in the classpath the it will execute):

           

          {code}

          public void initTransactionManager(Environment env) {

                  Object tm = env.get( EnvironmentName.TRANSACTION_MANAGER );

                  //    START CODE OVERRIDE - so drools works with org.springframework.transaction.jta.JtaTransactionManager

                  if ( tm != null && tm.getClass().getName().equals( "org.springframework.transaction.jta.JtaTransactionManager" ) ) {

                      logger.debug( "Instantiating Spring JtaTransactionManager" );

                      this.txm = new JtaTransactionManager(((org.springframework.transaction.jta.JtaTransactionManager)tm).getUserTransaction(),

                                                            env.get( EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY ),

                                                            ((org.springframework.transaction.jta.JtaTransactionManager)tm).getTransactionManager() );

                      this.jpm = new DefaultJpaManager(this.env);

                  } else {

                  //    END CODE OVERRIDE - so drools works with org.springframework.transaction.jta.JtaTransactionManager

                      if ( tm != null && tm.getClass().getName().startsWith( "org.springframework" ) ) {

                          try {

                              Class<?> cls = Class.forName( "org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager" );

                              Constructor<?> con = cls.getConstructors()[0];

                              this.txm = (TransactionManager) con.newInstance( tm );

                              logger.debug( "Instantiating  DroolsSpringTransactionManager" );

           

                              if ( tm.getClass().getName().toLowerCase().contains( "jpa" ) ) {

                                  // configure spring for JPA and local transactions

                                  cls = Class.forName( "org.drools.container.spring.beans.persistence.DroolsSpringJpaManager" );

                                  con = cls.getConstructors()[0];

                                  this.jpm =  ( JpaManager) con.newInstance( new Object[] { this.env } );

                              } else {

                                  // configure spring for JPA and distributed transactions

                              }

                          } catch ( Exception e ) {

                              logger.warn( "Could not instatiate DroolsSpringTransactionManager" );

                              throw new RuntimeException( "Could not instatiate org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager", e );

                          }

                      } else {

                          logger.debug( "Instantiating  JtaTransactionManager" );

                          this.txm = new JtaTransactionManager( env.get( EnvironmentName.TRANSACTION ),

                                                                env.get( EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY ),

                                                                tm );

                          this.jpm = new DefaultJpaManager(this.env);

                      }

                  //    START CODE OVERRIDE

                  }

                  //    END CODE OVERRIDE

          {code}

           

          Hope this helps

          • 17. Re: Drools, Spring integration and JTA in container
            gigazhang

            well ,3ks this topic

            1 2 Previous Next