11 Replies Latest reply on Dec 30, 2009 7:50 AM by Ronald van Kuijk

    Cannot access process instance after calling CompleteTask(), Why?

    Er Djigit Newbie

      Hi all,

       

      I am a new user of this JBPM forum and JBPM itself. I am trying to integrate JBPM 4 to my JEE project.

      But first tried out some simple examples to be familiar with JBPM 4. I am using:

      jbpm-4.2

      jboss-5.1.0.GA

      eclipse 3.5

       

      So, here is my question:

      With the following process definition and test class,


      {code:xml}

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

       

      <process name="demoProcess" xmlns="http://jbpm.org/4.2/jpdl">
          <start g="93,122,48,48" name="start1">
            <transition name="to end1" to="end1" g="-45,-20"/>
         </start>
         <end g="326,133,48,48" name="end1" state="normal_completed"/>
      </process>

      {code}

       

      {code:java}

      public class JbpmTest {

       

          public static void main(String[] args)
          {
              ProcessEngine processEngine = new Configuration().buildProcessEngine();
              RepositoryService repositoryService = processEngine.getRepositoryService();
              ExecutionService executionService = processEngine.getExecutionService();
             
              String deploymentID = repositoryService.createDeployment().addResourceFromClasspath("demoProcess.jpdl.xml").deploy();
              ProcessInstance processInstance = executionService.startProcessInstanceByKey("demoProcess");
             
              System.out.println("--------------------- processInstance : " + processInstance);
              System.out.println("--------------------- processInstance getId : " + processInstance.getId());
              System.out.println("--------------------- processInstance getState : " + processInstance.getState());
              System.out.println("--------------------- processInstance isEnded : " + processInstance.isEnded());
             
              repositoryService.deleteDeploymentCascade(deploymentID);
          }
      }

      {code}

       

      Got the following output:

       

      --------------------- processInstance : execution[demoProcess.1]
      --------------------- processInstance getId : demoProcess.1
      --------------------- processInstance getState : normal_completed
      --------------------- processInstance isEnded : true

       

       

      But when i added a task, could not access to process instance,

      as in the following process definiton and test class:

       

      {code:xml}

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

       

      <process name="demoProcess" xmlns="http://jbpm.org/4.2/jpdl">
          <start g="93,122,48,48" name="start1">
            <transition name="to some task" to="some task" g="-72,-20"/>
         </start>
         <task name="some task" assignee="er" g="299,121,92,52">
            <transition name="to end1" to="end1" g="-45,-20"/>
         </task>
         <end g="578,123,48,48" name="end1" state="normal_completed"/>
      </process>

      {code}

       

      {code:java}

      public class JbpmTest {

       

          public static void main(String[] args)
          {
              ProcessEngine processEngine = new Configuration().buildProcessEngine();
              RepositoryService repositoryService = processEngine.getRepositoryService();
              ExecutionService executionService = processEngine.getExecutionService();
              TaskService taskService = processEngine.getTaskService();

       

              String deploymentID = repositoryService.createDeployment().addResourceFromClasspath("demoProcess.jpdl.xml").deploy();
              ProcessInstance processInstance = executionService.startProcessInstanceByKey("demoProcess");
              String pID = processInstance.getId();
             
              System.out.println("(before completeTask) processInstance : " + processInstance);
              System.out.println("(before completeTask) processInstance getId : " + processInstance.getId());
              System.out.println("(before completeTask) processInstance getState : " + processInstance.getState());
              System.out.println("(before completeTask) processInstance isEnded : " + processInstance.isEnded());
             
              List<Task> taskList = taskService.findPersonalTasks("er");
              Task task = taskList.get(0);

       

              System.out.println("----- task : " + task);
              System.out.println("----- task getId : " + task.getId());
              System.out.println("----- task getName : " + task.getName());
              System.out.println("----- task getActivityName : " + task.getActivityName());
              System.out.println("----- task getAssignee : " + task.getAssignee());
             
              taskService.completeTask(task.getId());
             
              processInstance = executionService.findProcessInstanceById(pID);
             
              System.out.println("(after completeTask) processInstance : " + processInstance);
              System.out.println("(after completeTask) processInstance getId : " + processInstance.getId());
              System.out.println("(after completeTask) processInstance getState : " + processInstance.getState());
              System.out.println("(after completeTask) processInstance isEnded : " + processInstance.isEnded());
             
              repositoryService.deleteDeploymentCascade(deploymentID);
          }
      }

      {code}

       

      Output is:

       

      (before completeTask) processInstance : execution[demoProcess.1]
      (before completeTask) processInstance getId : demoProcess.1
      (before completeTask) processInstance getState : active-root
      (before completeTask) processInstance isEnded : false
      ----- task : Task(some task)
      ----- task getId : 1
      ----- task getName : some task
      ----- task getActivityName : some task
      ----- task getAssignee : er
      (after completeTask) processInstance : null
      Exception in thread "main" java.lang.NullPointerException
          at com.tutorial.JbpmTest.main(JbpmTest.java:51)

       

      As i know completeTask deletes the task from the DB, not a processInstance.

      If processInstance is deleted because there is no wait state after the task,

      then why i can acces to processInstance in a first case?

       

      That is all for now. Thanks in advance for any suggestions..

       

      p.s. As you guess, my english is not the best .

        • 1. Re: Cannot access process instance after calling CompleteTask(), Why?
          Jas Lam Newbie

          I briefly came across this myself but haven't investigated it fully.

           

          i believe if the process has entered the end state, it's process instance is completed and it is removed from the jbpm4_execution table. The instance no longer exist as a active one. In order to access it you might have to look into the history service to find that process instance

           

          Correct me if i am wrong.

          1 of 1 people found this helpful
          • 2. Re: Cannot access process instance after calling CompleteTask(), Why?
            Ronald van Kuijk Master
            In both cases the processinstance is deleted from the runtime/operational database and moved to the history database. The difference is that in the second case you reload the processinstance (you need to) whereas in te first case you do not. If you'd reload the processinstance in the first case as well, you'd see the same behaviour
            • 3. Re: Cannot access process instance after calling CompleteTask(), Why?
              Er Djigit Newbie

              Many thanks Jas Lam and Ronald van Kuijk for your replies. Past 3 days I was

              trying to reply to this post or edit it. But the message editor could not loaded,

              could not shown up.. Now it is working.

               

              So I understand that if in my process definition there are multiple end nodes and

              multiple task nodes just before these end nodes, and if I want to get the final state

              of the process instance namely in which end node the execution ended, the only

              choice doing it is to use the history service. But if I put state nodes before

              every end node, I could get the final status by calling signalExecutionById(),

              just don't want to use history service for now..

               

              Have a nice day. Regards.

              • 4. Re: Cannot access process instance after calling CompleteTask(), Why?
                Jas Lam Newbie

                Hey Ronald

                 

                You seem to be very knowledgable about this Jbpm. I am a new to this also and I am a bit confuse about how jbpm manages processinstances.

                 

                 

                1) When u said "reload the processinstance", i assume this command: "processInstance = executionService.findProcessInstanceById(pID);". So does the jbpm framework actually do a query on execution table in the database for that execution ID?

                 

                2) In example one by Er, if I was to reload the process instance using "processInstance = executionService.findProcessInstanceById(pID);" after starts the process, shouldn't this return null? since the actually process entry in the table no longer there after it enters the end state.

                 

                3) When I played with signalExecutionById(), "processInstanceNew = executionService.signalExecutionById()" it returns a processInstance and this instance is the updated execution instance reflecting its new state.

                While the previous reference "processInstanceOld = executionService.startProcessInstanceByKey("demoProcess");" now no longer reflecting the current state of the execution. Why is that?

                     This seems to contradict the first example by Er, in which the orignial processinstance reference reflected the state change as the process is executed.

                 

                Thanks

                 

                Jason Lam

                • 5. Re: Cannot access process instance after calling CompleteTask(), Why?
                  Ronald van Kuijk Master

                  lamj1 wrote:

                   

                  Hey Ronald

                   

                  You seem to be very knowledgable about this Jbpm.

                  I am

                   

                  1) When u said "reload the processinstance", i assume this command: "processInstance = executionService.findProcessInstanceById(pID);". So does the jbpm framework actually do a query on execution table in the database for that execution ID?

                  Yes, but not directly, in the background it uses the jBPM commands

                   

                  2) In example one by Er, if I was to reload the process instance using "processInstance = executionService.findProcessInstanceById(pID);" after starts the process, shouldn't this return null? since the actually process entry in the table no longer there after it enters the end state.

                   

                  Correct and processInstance.isEnded() throws a nullpointerexception

                  3) When I played with signalExecutionById(), "processInstanceNew = executionService.signalExecutionById()" it returns a processInstance and this instance is the updated execution instance reflecting its new state.

                  While the previous reference "processInstanceOld = executionService.startProcessInstanceByKey("demoProcess");" now no longer reflecting the current state of the execution. Why is that?

                       This seems to contradict the first example by Er, in which the orignial processinstance reference reflected the state change as the process is executed.

                   

                  Starting and ending the process in on command (that is what happens) can be seen as a special case. I think it could be changed to have the same behaviour, but since it is often either on purpose, or never happens, people will not get this mixed up.

                  • 6. Re: Cannot access process instance after calling CompleteTask(), Why?
                    Jas Lam Newbie

                    Thanks for the quick response. would you know of any book? or good reference manual/example about this JBPM stuff? since JBPM v4 user guide is quite incomplete in explaining how things work.

                     

                    Thanks

                     

                    Merry xmas

                     

                    Jason

                    • 7. Re: Cannot access process instance after calling CompleteTask(), Why?
                      Er Djigit Newbie

                      Yes I am agree with Jason. We are new users of the JBPM and started it from vesion 4.x. While many JBPM

                      experts know earlier versions and thus know the underlying mechanism. Like that "The service API is in fact

                      an abstraction on top of the CommandService facade approach".

                      (from http://www.jorambarrez.be/blog/2009/07/01/jbpm4-what%E2%80%99s-new-part-3/).

                      Of course I can dig into the source code and find out what is going on. But the improved documentation is

                      a better choice. And also I should indicate that after some research, the current tutorials is enough for my

                      very very basic business process logic. No doubt that in future there will be excellent works on JBPM

                      and its documentation as well.

                       

                      Regards.

                      • 8. Re: Question about docs
                        Ronald van Kuijk Master

                        First of all, new kind of unrelated questions are best put in new topics so they are better to find by others while searching

                         

                        How things work is not userguide stuff... It's hidden behind a stable public api. The developerguide has a more info on this, but certainly not everything. For real details there is no better place than the source.

                        • 9. Re: Question about docs
                          Ronald van Kuijk Master

                          Same here, 'new' questions in new topics please...

                           

                          In 3 it was kind of needed to know the underlying mechanism (e.g. the jbpm object/data model) to do more than basic things. One of the (imo good) changes in 4 is that this is not needed anymore. The public api should give you most of what you need. If it does not do so, we are always open to feature requests (look in the jira, there are alread some in there)

                           

                          So if you could explain why you needed to go in the jbpm source (other than examples) I can go into details..

                          • 10. Re: Cannot access process instance after calling CompleteTask(), Why?
                            Er Djigit Newbie

                            Ok, it is my fault because I confused the "user guide" with the "tutorial". I expected from the

                            user guide:

                            * to give more info, maybe historical info, maybe info about new features different from  the earlier

                            versions in comparison.

                            * to have a standalone sections in content. Namely re-mentioning some important statements

                            and facts in different secitons. Because human learns by repeating.

                            * to keep in mind that since it is a users guide not developers, some beginners may have a BPM

                            experience first time with the JBPM and explain the BPM design, structure etc by using JBPM.

                             

                            But all of these are present in the tutorial, not in the user guide. As I said after some surfing in

                            the net, reading the certain tutorials and blogs, the current features are enough for me, and of

                            course new questions will be in new topics.

                             

                            Thanks!

                            • 11. Re: Cannot access process instance after calling CompleteTask(), Why?
                              Ronald van Kuijk Master
                              If you have a nice collection of related articles, you can always make a 'document' in this jbpm community section (see the tab above)