8 Replies Latest reply on Jan 11, 2013 10:46 AM by doboss

    Help using persistence with JBPM 5.4

    doboss Newbie

      Can someone give me some concrete steps on how to implmenet database persistence of the workflow state in JBPM 5.4?

       

       

      My end goal is to be able to have save points in my process so I can come back to it and restart it where it left off.  I have managed to create a very simple web app which can pull a JBPM process from Guvnor and run it. But every time I try to add persistence stuff, the app stops deploying and complains about missing datasources and/or persistence units.

       

      My setup is JBPM 5.4, with the Guvnor and JBPM Console all running on the same JBoss 7.1.1 instance.  I have my jdbc drivers properly deployed in JBoss as a module as well.

       

      Attached is my simple web app. If somone could tell me what the next parts to add are to make persistence happen, that would be great.

       

      For those who do not want to download the zip, here is the meat of the code I am running:

       

      {code}

      package org.jboss.jbpm.webapp;

       

      import java.util.HashMap;

      import java.util.Map;

       

      import javax.ejb.Singleton;

      import javax.inject.Named;

       

      import org.drools.KnowledgeBase;

      import org.drools.builder.KnowledgeBuilder;

      import org.drools.builder.KnowledgeBuilderFactory;

      import org.drools.builder.ResourceType;

      import org.drools.io.ResourceFactory;

      import org.drools.io.impl.UrlResource;

      import org.drools.runtime.StatefulKnowledgeSession;

      import org.jbpm.examples.quickstarts.Person;

       

      @Singleton

      @Named

      public class Runner

      {

         private int timesRun = 0;

       

         private KnowledgeBase kbase = null;

       

         public void runIt()

         {

            try

            {

               if (kbase == null)

               {

                  // load up the knowledge base

                  kbase = readKnowledgeBase();

               }

               StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

               Map<String, Object> params = new HashMap<String, Object>();

               Person p = new Person("Maciej Swiderski");

               params.put("person", p);

               ksession.startProcess("com.sample.script", params);

            }

            catch (Throwable t)

            {

               t.printStackTrace();

            }

            timesRun++;

         }

       

         private static KnowledgeBase readKnowledgeBase() throws Exception

         {

            String url = "http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/defaultPackage/LATEST";

       

            UrlResource resource = (UrlResource) ResourceFactory.newUrlResource(url);

            resource.setBasicAuthentication("enabled");

            resource.setUsername("guest");

            resource.setPassword("guest");

       

            KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

            kbuilder.add(resource, ResourceType.PKG);

       

            return kbuilder.newKnowledgeBase();

         }

       

         public int getTimesRun()

         {

            return timesRun;

         }

       

         public void setTimesRun(int timesRun)

         {

            this.timesRun = timesRun;

         }

      }

      {code}

       

      The path I was going down was with the JBPMHelper...

       

      If I modify the pom.xml (to add hibernate-core and jbpm-test) change the code like this:

       

      {code}

         public void runIt()

         {

            try

            {

               if (kbase == null)

               {

                  JBPMHelper.setupDataSource();

                  // load up the knowledge base

                  kbase = readKnowledgeBase();

               }

               StatefulKnowledgeSession ksession = JBPMHelper.newStatefulKnowledgeSession(kbase);

      ...

      {code}

       

      Then I see deployment problems like this :

       

      {code}

      13:26:33,891 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS015863: Replacement of deployment "jbpm-web-app.war" by deployment "jbpm-web-app.war" was rolled back with failure message {"JBAS014771: Services with missing/unavailable dependencies" => ["jboss.persistenceunit.\"jbpm-web-app.war#org.jbpm.persistence.jpa\"jboss.naming.context.java.jdbc.jbpm-dsMissing[jboss.persistenceunit.\"jbpm-web-app.war#org.jbpm.persistence.jpa\"jboss.naming.context.java.jdbc.jbpm-ds]"]}

       

      ...

       

      BAS014775:New missing/unsatisfied dependencies:

                               service jboss.naming.context.java.jdbc.jbpm-ds (missing) dependents: [service jboss.persistenceunit."jbpm-web-app.war#org.jbpm.persistence.jpa"]

      {code}

       

       

       

      Thanks!

        • 1. Re: Help using persistence with JBPM 5.4
          doboss Newbie

          Okay, I think by including jbpm-test it is defining the same persistence unit. So I am going to try to do it without the JBPMHelper.

          • 2. Re: Help using persistence with JBPM 5.4
            doboss Newbie

            Okay, I got it working by changing the code like this:

             

             

            {code}

            @PersistenceUnit(unitName = "org.jbpm.persistence.jpa")

               private EntityManagerFactory emf;

             

               public void runIt()

               {

                  try

                  {

                     if (kbase == null)

                     {

                        // load up the knowledge base

                        kbase = readKnowledgeBase();

                     }

                     Environment env = KnowledgeBaseFactory.newEnvironment();

                     env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);

             

                     // create a new knowledge session that uses JPA to store the runtime

                     // state

                     StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);

                     int sessionId = ksession.getId();

                     JPAWorkingMemoryDbLogger logger = new JPAWorkingMemoryDbLogger(ksession);

            {code}

             

            If anyone wants more details, I would be happy to provide them.

             

            I hope to post the who example when I am done.

            • 3. Re: Help using persistence with JBPM 5.4
              Shobhit Tyagi Master

              Hey doboss.

               

              I am in a need of your help.

              Did you change anything in your persistence.xml to achieve the above?

              How do you use the logger object after it has been created?

               

              Thanks.

              • 4. Re: Help using persistence with JBPM 5.4
                doboss Newbie

                This is my current persistence.xml, I believe I added the <jta-data-source> stuff after the original post.

                 

                Here it is:

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

                <persistence version="2.0"

                    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                    xsi:schemaLocation="

                        http://java.sun.com/xml/ns/persistence

                        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

                    <persistence-unit name="org.jbpm.persistence.jpa"

                        transaction-type="JTA">

                        <provider>org.hibernate.ejb.HibernatePersistence</provider>

                        <jta-data-source>java:jboss/datasources/jbpmDS</jta-data-source>

                        <mapping-file>META-INF/JBPMorm-JPA2.xml</mapping-file>

                        <mapping-file>META-INF/ProcessInstanceInfoMapping-JPA2.xml</mapping-file>

                 

                        <class>org.drools.persistence.info.SessionInfo</class>

                        <class>org.drools.persistence.info.WorkItemInfo</class>

                        <class>org.jbpm.process.audit.ProcessInstanceLog</class>

                        <class>org.jbpm.process.audit.NodeInstanceLog</class>

                        <class>org.jbpm.process.audit.VariableInstanceLog</class>

                 

                        <properties>

                            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />

                            <property name="hibernate.max_fetch_depth" value="3" />

                            <!-- property name="hibernate.hbm2ddl.auto" value="update" /-->

                            <property name="hibernate.show_sql" value="false" />

                            <property name="hibernate.transaction.manager_lookup_class"

                                value="org.hibernate.transaction.JBossTransactionManagerLookup" />

                 

                            <!-- BZ 841786: AS7/EAP 6/Hib 4 uses new (sequence) generators which seem

                                to cause problems -->

                            <property name="hibernate.id.new_generator_mappings" value="false" />

                        </properties>

                 

                    </persistence-unit>

                 

                    <persistence-unit name="org.jbpm.task"

                        transaction-type="RESOURCE_LOCAL">

                        <provider>org.hibernate.ejb.HibernatePersistence</provider>

                        <jta-data-source>java:jboss/datasources/jbpmDS</jta-data-source>

                        <mapping-file>META-INF/Taskorm-JPA2.xml</mapping-file>

                 

                        <class>org.jbpm.task.Attachment</class>

                        <class>org.jbpm.task.BooleanExpression</class>

                        <class>org.jbpm.task.Comment</class>

                        <class>org.jbpm.task.Content</class>

                        <class>org.jbpm.task.Deadline</class>

                        <class>org.jbpm.task.Delegation</class>

                        <class>org.jbpm.task.EmailNotification</class>

                        <class>org.jbpm.task.EmailNotificationHeader</class>

                        <class>org.jbpm.task.Escalation</class>

                        <class>org.jbpm.task.Group</class>

                        <class>org.jbpm.task.I18NText</class>

                        <class>org.jbpm.task.Notification</class>

                        <class>org.jbpm.task.OnAllSubTasksEndParentEndStrategy</class>

                        <class>org.jbpm.task.OnParentAbortAllSubTasksEndStrategy</class>

                        <class>org.jbpm.task.PeopleAssignments</class>

                        <class>org.jbpm.task.Reassignment</class>

                        <class>org.jbpm.task.Status</class>

                        <class>org.jbpm.task.SubTasksStrategy</class>

                        <class>org.jbpm.task.Task</class>

                        <class>org.jbpm.task.TaskData</class>

                        <class>org.jbpm.task.User</class>

                        <properties>

                            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />

                            <property name="hibernate.max_fetch_depth" value="3" />

                            <!-- property name="hibernate.hbm2ddl.auto" value="update" /-->

                            <property name="hibernate.show_sql" value="false" />

                            <property name="hibernate.transaction.manager_lookup_class"

                                value="org.hibernate.transaction.JBossTransactionManagerLookup" />

                 

                            <!-- BZ 841786: AS7/EAP 6/Hib 4 uses new (sequence) generators which seem

                                to cause problems -->

                            <property name="hibernate.id.new_generator_mappings" value="false" />

                        </properties>

                    </persistence-unit>

                </persistence>

                 

                In my pom.xml I do *not* have a reference to jbpm-test as it causes a conflict with "org.jbpm.persistence.jpa"

                 

                I also manually changed (throught the JBoss console) the "H2DS" datasource installed by default with the jbpm-installer to point to my local (postgres) database instead of the H2 one. (Since the have the same persistence-unit name).

                 

                Let me know if you need more info.

                • 5. Re: Help using persistence with JBPM 5.4
                  Shobhit Tyagi Master

                  Thanks. I cant see the file. Hey will it be possible for you to post your entire code? This history enable thing is a little confusing for me. May the because of the way I have designed my application.

                   

                  Regards.

                  • 6. Re: Help using persistence with JBPM 5.4
                    doboss Newbie

                    Here it is in it's present state.

                     

                    HTH!

                    • 8. Re: Help using persistence with JBPM 5.4
                      doboss Newbie

                      I have made a much more standalone version of this app, and cleaned it up a bit. For anyone interested, you can now find it here:

                       

                      https://community.jboss.org/wiki/SimpleJBPM54InsideJBossAS711WebAppExample