10 Replies Latest reply on Dec 13, 2011 8:43 AM by twizansk

    Combining human and automated tasks

    twizansk

      Hi All,

       

      I am trying to create a work flow  that includes a human task, followed by an automated task which should run after the human task is completed.

       

      Here's the naive implementation:

       

      flow1.png

       

      I start the process which reaches the human task and pauses.  I then create a human task client and complete the task.  The process then continues to the script task and completes successfully.  The problem is that this works only when the process and client task are being invoked in the same method call (as is the case in all of the jBPM examples).

       

      However I would like to begin the process from one service and complete the human task at some arbitrary time in the future using a completely different service.  Since the human task service and jBPM process engine are sharing the same DB, this should be possible.  I just can't get it to work.

       

      The only thing the works for me is the more complicated configuration:

       

      flow2.png

      where I have to raise the event manually when the human task is completed.  This seems overly complex.

       

      Can anyone help me implement the simpler version?

       

      Thanks

        • 1. Re: Combining human and automated tasks
          eaa

          Human Task server and jBPM process engine does not use the same DB. You can use the Human Task client API in your external system to complete a Human Task:

           

          <dependency>

                      <groupId>org.jbpm</groupId>

                      <artifactId>jbpm-human-task</artifactId>

                      <version>5.2.0-SNAPSHOT</version>

          </dependency>

          • 2. Re: Combining human and automated tasks
            twizansk

            Thanks for the quick reply.  I am using the human task API and I can complete the human task successfully.  The problem is that I can't restart the process afterwards so that the script task will be invoked.

             

            Thanks

            • 3. Re: Combining human and automated tasks
              eaa

              The process should automatically continue after you complete the Human Task. The session where you started the process must be up and running.

              • 4. Re: Combining human and automated tasks
                twizansk

                "The session where you started the process must be up and running."

                 

                Yes, that is the problem.  I want to continue the process at a later time, after the human task is completed.  The original session that created the process will be closed by then.  Is there any way to do this?

                • 5. Re: Combining human and automated tasks
                  calca

                  Tommer,

                  you should keep the session id where you created the process, and then load it like this:

                  // recreate the session from database using the sessionId

                  ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( sessionId, kbase, null, env );

                   

                  attach the work item handlers,

                   

                  and after using it disposing it.

                   

                  Demian

                  • 6. Re: Combining human and automated tasks
                    twizansk

                    Thanks but how do I restart the session from the stopped position.  If I do

                     

                    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( sessionId, kbase, null, env );

                    ksession.startProcess(...);

                     

                    the entire process is executed from the beginning.  I can't seem to tell jBPM to resume the process from the node immediately after the human task.  I'd expect there to exist some method of the form:

                     

                         ksession.resumeProcess()

                     

                    but I can't find one.

                     

                    Thanks again for the replies.

                     

                    Tommer

                     


                    • 7. Re: Combining human and automated tasks
                      calca

                      In fact, the resume is made automatically when you complete the task.

                       

                      For example,

                       

                      CommandBasedWSHumanTaskHandler handler = new CommandBasedWSHumanTaskHandler(ksession)

                      ...

                      handler.connect()

                      ..

                      session.registerWorkItemHandler("Human Task", handler);

                       

                      This will make that:

                      1) The human task listeners for task completion are registered.

                      2) When task is registered, the listener has the session to continue the process.

                       

                      Now, you can make

                      client.complete(taskId,...)

                       

                      Now the listener (you can check it, GetResultContentResponseHandler) will be notified about task completion and process continued.

                       

                      Hope this helps,

                       

                      Demian

                      • 8. Re: Combining human and automated tasks
                        twizansk

                        Yes!  That works!  Registering the task handler when invoking the human task client was the part I was missing.  Thank you.

                         

                        As it turns out, I don't even need to load the original session.  I can register the task handler with a new stateful session and it still works:

                         

                                // Create a new session

                                KnowledgeBase kbase = readKnowledgeBase(CREATE_INSTALLER_PROCESS);

                                Environment env = KnowledgeBaseFactory.newEnvironment();

                                env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);

                                env.set(EnvironmentName.TRANSACTION_MANAGER, transactionManager);

                                StatefulKnowledgeSession ksession = JPAKnowledgeService

                                        .newStatefulKnowledgeSession(kbase, null, env);

                         

                                // Register a handler

                                CommandBasedWSHumanTaskHandler handler = new CommandBasedWSHumanTaskHandler(ksession);

                                handler.connect();

                                ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);

                               

                                // Create a human task client

                                TaskClient client = new TaskClient(new MinaTaskClientConnector(

                                        "MinaConnector", new MinaTaskClientHandler(

                                                SystemEventListenerFactory.getSystemEventListener())));

                                client.connect("127.0.0.1", 9123);

                                Thread.sleep(1000);

                                BlockingTaskSummaryResponseHandler taskSummaryResponseHandler = new BlockingTaskSummaryResponseHandler();

                                client.getTasksAssignedAsPotentialOwner("krisv", "en-UK",

                                        taskSummaryResponseHandler);

                                List<TaskSummary> tasks = taskSummaryResponseHandler.getResults();

                                TaskSummary taskSummary = tasks.get(0);

                                long taskId = taskSummary.getId();

                                BlockingTaskOperationResponseHandler responseHandler = new BlockingTaskOperationResponseHandler();

                                client.start(taskId, "krisv", responseHandler);

                         

                                // Complete the task

                                long taskId = taskSummary.getId();

                                BlockingTaskOperationResponseHandler responseHandler = new BlockingTaskOperationResponseHandler();

                                client.start(taskId, "krisv", responseHandler);

                                client.complete(taskId, "krisv", null, responseHandler);

                         

                        I'd like to understand the details.  When is it necessary to load an existing session and when can you use a new one?  But, in the meantime, it works.

                         

                        Thanks again.

                         

                        Tommer

                        • 9. Re: Combining human and automated tasks
                          calca

                          Well, I was not clear about this too :S.

                           

                          Krisv kindly explained it to me in jbpm irc.

                           

                          Session contains data that you inserted in working memory when using bussiness rules (using session.insert to insert facts), rule state and timers. Process instances are almost independent units.

                          So If you don't have anything like this in the session, you can create another one and it will load your process data. If not (I mean, if process state and session state are linked), you have to load the correct session.

                           

                          Demian

                          • 10. Re: Combining human and automated tasks
                            twizansk

                            Yes, that does clarify things a bit.

                             

                            Thanks