5 Replies Latest reply on Oct 13, 2011 4:30 AM by super_man.sh

    About jBPM5.1 ProcessInstance and HumanTask persisit

    super_man.sh

      Dear All,

       

      I'm use MySQL for persist ProcessInstance and ProcessInstance and HumanTask Object.This is the code:

       

       

       
      public static final void main(String[] args) {
           TaskClient taskClient = new TaskClient(new MinaTaskClientConnectorExt("MinaConnector",
                     new MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));
           taskClient.connect("127.0.0.1", 9123);
           //init datasource.......
           KnowledgeBase kbase = readKnowledgeBase();               
           EntityManagerFactory emf =Persistence.createEntityManagerFactory("org.jbpm.jpa" );
           Environment env = KnowledgeBaseFactory.newEnvironment();
           env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emf );
           env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
           // create a new knowledge session that uses JPA to store the runtime state
           
           StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env );
           //StatefulKnowledgeSession ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(1, kbase, null, env);
           //StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
           
              WSHumanTaskHandler taskHandler=new WSHumanTaskHandler();
              taskHandler.setClient(taskClient);
              ksession.getWorkItemManager().registerWorkItemHandler("Human Task", taskHandler);
              Map params = new HashMap();
              params.put("userId", "krisv");
              params.put("description", "Need a new laptop computer");
              ksession.startProcess("com.sample.humantask", params);
      
           ksession.dispose();
           taskClient.disconnect();
      }
      
      

       

      This main methd execute is ok,then I found ProcessInstance and Human info is in MySQL(in sessioninfo and task tables).Next,I write complete the task code as below:

       

      public static final void main(String[] args) {
                TaskClient taskClient = new TaskClient(new MinaTaskClientConnectorExt("MinaConnector",
                          new MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));
                taskClient.connect("127.0.0.1", 9123);
      
      
                //init datasource......
        
                KnowledgeBase kbase = readKnowledgeBase(); 
                EntityManagerFactory emf =Persistence.createEntityManagerFactory("org.jbpm.jpa" );
                Environment env = KnowledgeBaseFactory.newEnvironment();
                env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emf );
                env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
                // create a new knowledge session that uses JPA to store the runtime state
        
                StatefulKnowledgeSession ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(1, kbase, null, env);
                BlockingTaskSummaryResponseHandler taskSummaryHandler = new BlockingTaskSummaryResponseHandler();
                List<String> groups = new ArrayList<String>();
                groups.add("sales");
                taskClient.getTasksAssignedAsPotentialOwner("sales-rep", groups, "en-UK", taskSummaryHandler);
                TaskSummary task1 = taskSummaryHandler.getResults().get(0);
                BlockingTaskOperationResponseHandler taskOperationHandler = new BlockingTaskOperationResponseHandler();
                taskClient.claim(task1.getId(), "sales-rep", groups, taskOperationHandler);
                taskOperationHandler = new BlockingTaskOperationResponseHandler();
                taskClient.start(task1.getId(), "sales-rep", taskOperationHandler);
                taskOperationHandler.waitTillDone(1000);
                taskOperationHandler = new BlockingTaskOperationResponseHandler();
                Map<String, Object> results = new HashMap<String, Object>();
                results.put("comment", "Agreed, existing laptop needs replacing");
                results.put("outcome", "Accept");
                ContentData contentData = new ContentData();
                contentData.setAccessType(AccessType.Inline);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ObjectOutputStream out;
                try {
                          out = new ObjectOutputStream(bos);
                          out.writeObject(results);
                          out.close();
                          contentData = new ContentData();
                          contentData.setContent(bos.toByteArray());
                          contentData.setAccessType(AccessType.Inline);
                } catch (IOException e) {
                          e.printStackTrace();
                }
                taskClient.complete(task1.getId(), "sales-rep", contentData, taskOperationHandler);
                taskOperationHandler.waitTillDone(1000);
                ksession.signalEvent("Signal",null);
                ksession.dispose();
                taskClient.disconnect();
      }
      
      

       

      Execute the code,The task will be completed.

      I want to the ProcessInstance Continue to flow after task complete.So I write


      ksession.signalEvent("Signal",null);

      But the processInstance not continue to flow, Please help me!

        • 1. Re: About jBPM5.1 ProcessInstance and HumanTask persisit
          salaboy21

          Hi Jack.

          You don't need to do a signal to make the process continue. If you create a persistence session you only need to attack the correspondant listener and as soon as you complete the human task the listener will be in charge of continuing the process for you. Check at the documentation to see how you can register the listener but it will be something like this:

          ksession.getWorkItemManager().register("Human Task", new CommandBasedWSHumanTaskHandler(ksession));

           

          Cheers

          • 2. Re: About jBPM5.1 ProcessInstance and HumanTask persisit
            super_man.sh

            Hey Mauricio,

             

            Thanks for reply,I have already change my code as follows:

             

            public static final void startProcess() {
              TaskClient taskClient = new TaskClient(new MinaTaskClientConnectorExt("MinaConnector",
                                new MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));
              taskClient.connect("127.0.0.1", 9123);
            
              KnowledgeBase kbase = readKnowledgeBase();
              EntityManagerFactory emf =Persistence.createEntityManagerFactory("org.jbpm.jpa" );
              Environment env = KnowledgeBaseFactory.newEnvironment();
              env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
              env.set(EnvironmentName.GLOBALS, new MapGlobalResolver());
              env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
              env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[]{new JPAPlaceholderResolverStrategy(env),new SerializablePlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT)});
              StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env );
            
              CommandBasedWSHumanTaskHandler taskHandler=new CommandBasedWSHumanTaskHandler(ksession);
              taskHandler.setClient(taskClient);
              ksession.getWorkItemManager().registerWorkItemHandler("Human Task", taskHandler);
            
              ksession.startProcess("com.sample.humantask");
            }
            

             

            The first Human Task was generated after execute the startProcess method, then I write the method completeTask1 as follows:

             

            public static final void completeTask1() {
              TaskClient taskClient = new TaskClient(new MinaTaskClientConnectorExt("MinaConnector",
                                new MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));
              taskClient.connect("127.0.0.1", 9123);
              
              BlockingTaskSummaryResponseHandler taskSummaryHandler = new BlockingTaskSummaryResponseHandler();
              List<String> groups = new ArrayList<String>();
              groups.add("sales");
              taskClient.getTasksAssignedAsPotentialOwner("sales-rep", groups, "en-UK", taskSummaryHandler);
              TaskSummary task1 = taskSummaryHandler.getResults().get(0);
              BlockingTaskOperationResponseHandler taskOperationHandler = new BlockingTaskOperationResponseHandler();
              taskClient.claim(task1.getId(), "sales-rep", groups, taskOperationHandler);
              taskOperationHandler = new BlockingTaskOperationResponseHandler();
              taskClient.start(task1.getId(), "sales-rep", taskOperationHandler);
              taskOperationHandler.waitTillDone(1000);
              taskOperationHandler = new BlockingTaskOperationResponseHandler();
              Map<String, Object> results = new HashMap<String, Object>();
              results.put("comment", "Agreed, existing laptop needs replacing");
              results.put("outcome", "Accept");
              ContentData contentData = new ContentData();
              contentData.setAccessType(AccessType.Inline);
              ByteArrayOutputStream bos = new ByteArrayOutputStream();
              ObjectOutputStream out;
              try {
                      out = new ObjectOutputStream(bos);
                      out.writeObject(results);
                      out.close();
                      contentData = new ContentData();
                      contentData.setContent(bos.toByteArray());
                      contentData.setAccessType(AccessType.Inline);
              } catch (IOException e) {
                      e.printStackTrace();
              }
              taskClient.complete(task1.getId(), "sales-rep", contentData, taskOperationHandler);
              taskOperationHandler.waitTillDone(1000);
            }
            

            Execute the completeTask1 method,first Human Task was be completed in db,but the processinstance was not continue,task table screenshot as follows:

            tasklist.png

            As you see,the first HumanTask status was completed,because processinstance was not continue,so no new HumanTask generate.If continue the processInstance will be generate new HumanTask.But there is no.I don't know why.

            Thanks & Best Regards

            • 3. Re: About jBPM5.1 ProcessInstance and HumanTask persisit
              salaboy21

              Hi Jack,

              I think that the problem is related with the fact that the thread that start the process with ksession.startProcess("com.sample.humantask");

              Died and it's not up when you complete the task. So the listener that you register died with it. You need to make sure that the listener is still active in order to be called on taskCompletion.

               

              Cheers

              • 4. Re: About jBPM5.1 ProcessInstance and HumanTask persisit
                calca

                In order to check what Mauricio says, you can debug CommandBasedWSHumanTaskHandler.TaskCompletedHandler to see whether the listener is receiving notification of task completion.

                 

                Sometimes happens that after the process continues, there is some exception that you are not seing, and it is being rolledback.

                 

                Demian

                • 5. Re: About jBPM5.1 ProcessInstance and HumanTask persisit
                  super_man.sh

                  Thanks Mauricio & Demian to reply,I will try it later!