7 Replies Latest reply on Feb 25, 2013 10:08 AM by roxy1987

    How to reload knowledgeSession from database?

    wwweeii

      If there is a process instance with several human tasks inside, and a sudden exception, for example, electricity failure, occurs when one of the human tasks is processing. When coming back from the exception and reprocessing the human task, how can I reload the Ksession from the database and what are the steps? And when the intercepted human task is completed at last, how can I notify the Ksession with the current state of the human task and continue with the left tasks? Why is the state of the task is still 1 when I have completed it?

        • 1. Re: How to reload knowledgeSession from database?
          cristiano.nicolai

          Hi wei,

           

          Please have a look at: http://docs.jboss.org/jbpm/v5.1/userguide/ch07.html#d0e2745

          It explains how to create and load a jpa based session.

          • 2. Re: How to reload knowledgeSession from database?
            wwweeii

            Thank you! I have tried this method, but after the intercepted human task was completed, the Ksession couldn’t know it, the state of the task was still 1, and I couldn’t continue with the other tasks. How can I solve this problem?

            Here are codes:

             

            package com.sample;

            import java.util.HashMap;
            import java.util.Map;
            import java.util.Properties;

            import javax.naming.InitialContext;
            import javax.persistence.EntityManagerFactory;
            import javax.persistence.Persistence;
            import javax.transaction.UserTransaction;

            import org.drools.KnowledgeBaseFactory;
            import org.drools.base.MapGlobalResolver;
            import org.drools.runtime.*;
            import org.drools.persistence.jpa.*;
            import org.jbpm.process.audit.JPAWorkingMemoryDbLogger;
            import org.jbpm.process.workitem.wsht.CommandBasedWSHumanTaskHandler;

            import bitronix.tm.TransactionManagerServices;
            import bitronix.tm.resource.jdbc.PoolingDataSource;

            public class TestLoadSession {
            public  void loadSession(){
            EntityManagerFactory emf = Persistence.createEntityManagerFactory( "org.jbpm.persistence.jpa" );
            Environment env = KnowledgeBaseFactory.newEnvironment();
            env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
            env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
            env.set( EnvironmentName.GLOBALS, new MapGlobalResolver() );
            StatefulKnowledgeSession ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( 1, KBaseFactory.getKbase(), null, env );

            }
            public static final void main(String[] args) {
              try{
              UserTransaction ut = (UserTransaction) new InitialContext().lookup( "java:comp/UserTransaction" );
                   

              PoolingDataSource ds1 = new PoolingDataSource();
                  
             
                
                 ds1 = new PoolingDataSource();
                    ds1.setUniqueName( "jdbc/testDS1" );
                    ds1.setClassName( "com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" );
                    
                    ds1.setMaxPoolSize( 3 );
                    ds1.setAllowLocalTransactions( true );
                    ds1.getDriverProperties().put( "user","root" );
                    ds1.getDriverProperties().put( "password","111111" );
                    ds1.getDriverProperties().put( "URL",
                                                  "jdbc:mysql://localhost:3306/jbpm5db" );
                 
                    
                    ds1.init();
             
             
              ut.begin();
              EntityManagerFactory emf = Persistence.createEntityManagerFactory( "org.jbpm.persistence.jpa" );
              Environment env = KnowledgeBaseFactory.newEnvironment();
              env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
              env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
              env.set( EnvironmentName.GLOBALS, new MapGlobalResolver() );
              //Properties properties = new Properties();
              //properties.put("drools.processInstanceManagerFactory", "org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory");
              //properties.put("drools.processSignalManagerFactory", "org.jbpm.persistence.processinstance.JPASignalManagerFactory");
              //KnowledgeSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(properties);
              StatefulKnowledgeSession ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( 1, KBaseFactory.getKbase(), null, env );
              ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new CommandBasedWSHumanTaskHandler(ksession));
              JPAWorkingMemoryDbLogger logger = new JPAWorkingMemoryDbLogger(ksession);
             
                    //ksession.notifyAll();
              //ksession.abortProcessInstance(1);
              //ksession.startProcessInstance(1);
             
             

                    //ksession.getProcessInstance(1).
                   
                    ut.commit();
                    logger.dispose();
                   
              ut.begin();
              Map data2=new HashMap();
              Map data3=new HashMap();
              data3.put("name", "This is my test Human Task param 222222!!");
              data3.put("second", "The Second is KYO!!");
              data2.put("name1",data3);
              System.out.println("This is before commit:"+data2);
             
                 JbpmAPIUtil.completeTask(1, data2, "krisv");
              ut.commit();
              System.out.println("LOADSSESSION  success!");
              }
              catch(Exception e){
               e.printStackTrace();
              }
            }
            }

            • 3. Re: How to reload knowledgeSession from database?
              cristiano.nicolai

              The key point for the session to know about when a task is completed is the CommandBasedWSHumanTaskHandler in this case. If you're using the Mina based human task service your session must be up and your handler connected to be notified that a task was completed.  There is a jms based human task service and handler that can help you to keep this service independent from the session.

              • 4. Re: How to reload knowledgeSession from database?
                wwweeii

                Thanks Cristiano

                I want to know how to use a jms based human task service?

                • 5. Re: How to reload knowledgeSession from database?
                  wwweeii

                  I can use JPAKnowledgeService.loadStatefulKnowledgeSession(taskid,kbase,null ,env) method reload a stateful session from database.But when i use taskclient complete the human task,the stateful session can not know the human task have been completed.I don't know how to continue the process.I want krisv or salaboy come to answer my question! thanks!

                  • 6. Re: How to reload knowledgeSession from database?
                    sc_boss

                    i'm running into the same issue. Any luck?

                    • 7. Re: How to reload knowledgeSession from database?
                      roxy1987

                      To use JMS human task service, you need to modify the human task war. In web.xml, you can change the value of the parameter from hornetQ or mina to jms.