8 Replies Latest reply on Aug 25, 2012 4:58 AM by cwitschel

    Active tasks for process instance

    jnorris

      Hi All,

       

      Is there a way to get the active tasks for a process instance?  Can this information be retrieved from the node instances for a WorkflowProcessInstance?  I don't see the correlation between the HumanTaskNodeInstance/HumanTaskNode and a Task unless the getId method returns the task id which is not clear from the javadocs and is likely the id from the nodeinstancelog table.

       

      Thanks,

      Jim

        • 1. Re: Active tasks for process instance
          jnorris

          Here's what I'm thinking might work:

           

          Get the list of NodeInstances for the process instance

          For each node that is a HumanTaskNodeInstance get the workitemid for the node

               Get the task using localTaskService.getTaskByWorkItemId(workitemid)

           

          Jim

          • 2. Re: Active tasks for process instance
            eaa

            The best way to get this information is to use jbpm-bam module (basically it logs all the activities of your processes into a db) and to query NodeInstanceLog table.

            This is a query you can use:

             

                        SELECT

                            n

                        FROM

                            NodeInstanceLog n

                        WHERE

                            n.processInstanceId = :processInstanceId and

                            n.nodeName != ''

                        GROUP BY n.nodeName

                        HAVING MOD(COUNT(n), 2) = 1

            • 3. Re: Active tasks for process instance
              jnorris

              Hi Esteban,

               

              We are using persistence and logging activity to the DB.  We are trying to avoid directly querying the DB and sticking to using the API methods.  Also from the query you suggest I don't see how that gives me the list active Tasks for the process instance which is what the use case I'm trying to implement requires.

               

              Regards,

              Jim

              • 4. Re: Active tasks for process instance
                eaa

                AFAIK, there is no API to query history log.

                jbpm-bam module inserts a row in NodeInstanceLog each time a node is entered and a different row each time a node is exited. What the query does is to check for odd occurences of a node (using its name) in NodeInstanceLog.

                So, if the node is entered but not exited yet, you will have 1 row in NodeInstanceLog table for a particular process instance id and a particular node (assuming the names of your tasks are unique in a process). When the task is exited, a new row is inserted so the total number of rows for that particual node is even (not returned by the query). If it happens that the node is entered again (some kind of recursion in your process), then the row count will be odd again (3).

                 

                Best Regards,

                • 5. Re: Active tasks for process instance
                  jnorris

                  Hi Esteban,

                   

                  I implemented code which appears to work for a simple test case:

                   

                  List<Task> activeTasks = new ArrayList<Task>();

                  StatefulKnowledgeSession ksession = createKnowledgeSession();

                  org.jbpm.task.TaskService localTaskService = this.getTaskService();

                  JPAWorkingMemoryDbLogger jpaLogger = new JPAWorkingMemoryDbLogger( ksession );

                  KnowledgeRuntimeLoggerFactory.newConsoleLogger( ksession );

                  WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance( piId );

                  Collection<NodeInstance> nodes = processInstance.getNodeInstances();

                  Task jbpmTask;

                  for ( NodeInstance nodeInstance : nodes )

                  {

                      if( nodeInstance instanceof HumanTaskNodeInstance )

                      {

                          jbpmTask = localTaskService.getTaskByWorkItemId( ((HumanTaskNodeInstance) nodeInstance ).getWorkItemId() );

                          activeTasks.add( bpmTask );

                      }

                  }

                   

                  Regards,

                  Jim

                  • 6. Re: Active tasks for process instance
                    calca

                    You can also query the task service, something like

                     

                    String query = select t from Task twhere t.taskData.status = org.jbpm.task.Status.Reserved and t.taskData.processInstanceId = <<pid>>

                    localtaskservice.query(query,...)

                     

                    Regards,

                     

                    Demian

                    • 7. Re: Active tasks for process instance
                      jnorris

                      Hi Demian,

                       

                      Isn't the query method deprecated?  Is there another way to execute a custom query?

                       

                      Regards,

                      Jim

                      • 8. Re: Active tasks for process instance
                        cwitschel

                        Hi,

                         

                        it must be possible. in the jbpm-console when you click the diagram button on a running process, it points to the current task.

                         

                        cheers

                        christian