[jboss-user] [jBPM] - Re: Drools, Spring integration and JTA in container

Alex Manly do-not-reply at jboss.com
Tue Apr 12 04:37:22 EDT 2011


Alex Manly [http://community.jboss.org/people/alexmanly] created the discussion

"Re: Drools, Spring integration and JTA in container"

To view the discussion, visit: http://community.jboss.org/message/599448#599448

--------------------------------------------------------------
Hi Rodri,

Here is my drools spring configuration file:


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



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


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



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):


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 &amp;&amp; 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 &amp;&amp; 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



Hope this helps
--------------------------------------------------------------

Reply to this message by going to Community
[http://community.jboss.org/message/599448#599448]

Start a new discussion in jBPM at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110412/e67ad392/attachment-0001.html 


More information about the jboss-user mailing list