11 Replies Latest reply on Feb 8, 2010 1:13 PM by Ronald van Kuijk

    How to get sub process instance id

    Nagarajan Kanna Newbie

      Information about subprocess execution is stored in JBPM_EXECUTION. But, how to get subprocess instance id. Is there any way to get a handle on OpenExecution interface ?

       

      If we have to signal transitions in the subprocess, is there a way to do that without subprocess id ?

        • 1. Re: How to get sub process instance id
          Santanu Saraswati Novice

          Two questions here:

          Question 1:

          How to get sub process instance id:

                 Here is a possible solution

           

                  //Assuming processINstance to be your main process instance

                  Collection<? extends Execution> executions = processInstance.getExecutions();
                  for (Execution execution:executions) {
                      //cast it to open execution..we will need it
                      OpenExecution openExecution = (OpenExecution)execution;
                      //check if this is a sub-process or just a fork
                      if (openExecution.getSubProcessInstance() != null) {
                          OpenExecution subProcessInstance = openExecution.getSubProcessInstance();

                          //you should be able to do whatever you want to
                       }
                  }

           

          Question 2:

          Can we signal without the sub process id:

               Yes. Here is a code snippet which is achieving that:

           

               //again..process instance is the main process instance

               //stateName is the name of the state node you want to signal

               Execution execution = processInstance.findActiveExecutionIn(stateName);

               //signal name is the transition you need to take on signalling

               executionService.signalExecutionById(execution.getId(), signalName);

           

               You need to be cautious here that you do not have two states active at the same time with the same name. Then you might get either of them and may get unexpected result.

          • 2. Re: How to get sub process instance id
            Giuseppe Trotta Newbie

            hi,

             

            your solutions don't work for me. That is, when I get executions from processInstance I have an empty collection.

            here my code:

             

            ProcessInstance mainProcessInstance = (ProcessInstance)executionService.findProcessInstanceById(mainProcessInstanceId);

                 Collection<? extends Execution> executions = mainProcessInstance.getExecutions();

                 // here 'executions' is empty

             

            but I'm sure to have a subprocess active in a 'wait' state.

            Maybe cause the subprocessInstance isn't a mainProcessInstance's child execution, rather another ProcessInstance wired to the mainProcessInstance.

             

            I tried to get subprocessid through OpenExecution interface as follows:

                 OpenExecution mainOpenExecution = (OpenExecution) mainProcessInstance;
                 OpenExecution subOpenExecution = mainOpenExecution.getSubProcessInstance();
                 String subId = subOpenExecution.getId();
                 logger.info("subProcessInstance ID: "+subId);

             

            but either this solution doesn't work throwing following exception:

                 org.hibernate.LazyInitializationException: could not initialize proxy - no Session
                 at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)

             

            Any idea?

             

            thanxs

            Giuseppe

            • 3. Re: How to get sub process instance id
              Sebastian Schneider Master
              I have not understood the use case from the post. Do you want to get the IDs of all sub-processes belonging to a specific process instance?
              • 4. Re: How to get sub process instance id
                Giuseppe Trotta Newbie

                Yes exactly..

                I've an extreme necessity to get the subprocessID starting from mainprocessID.

                 

                In jbpm4_execution table I can see subprocessid in subprocinst_ column but I'm not able to get it.

                 

                please help me

                cheers

                • 5. Re: How to get sub process instance id
                  Jas Lam Newbie

                  i am in a similar situation as well. I ended up having to write my own query to get the sub process id from jbpm_execution table.

                   

                  This method only return child executions: child here means execution forked from this mainProcessInstance rather than subprocesses.

                  Collection<? extends Execution> executions = mainProcessInstance.getExecutions();

                  • 6. Re: How to get sub process instance id
                    Giuseppe Trotta Newbie

                    Thanxs Jas for your suggestion, but I would like preserve my workflow structure composed by a mainprocess running different subprocess in different steps. I could explode the entire workflow in a unique one, but I don't like it and it would be too complex (unless there are no other available options).

                     

                    I can't believe there isn't a way to accomplish this apparently simple task

                    • 7. Re: How to get sub process instance id
                      Sebastian Schneider Master

                      IMHO this should be possible through the API. So why don't you guys create a JIRA issue and gather votes on this? Obviously some more people ran into this issue but nobody reported it properly in JIRA. After discussion everyone is free to create JIRA issues and to propose new features. This is a good way to contribute. The most important thing is to provide a use case for this. JIRA is used by the developers to organize their work so the earlier you reporte an issue they can take it into account when giving priorities to issues and scheduling issues for upcoming releases.

                       

                      P.S.: You've got my vote.

                      • 8. Re: How to get sub process instance id
                        Sebastian Schneider Master
                        But before creating an issue please check the API docs and search JIRA.
                        • 9. Re: How to get sub process instance id
                          Santanu Saraswati Novice

                          Giuseppe,

                              I am really surprised. I tried in multiple ways, but I always get correct values in "executions" collection. I have gone thourgh the queries  fired and the hibernate mapping of ExecutionImpl and everything justifies that.

                           

                              When you call this:

                              ProcessInstance mainProcessInstance = (ProcessInstance)executionService.findProcessInstanceById(mainProcessInstanceId);

                           

                              These are the queries which gets fired:

                           

                              select * from ( select executioni0_.DBID_ as DBID1_0_, executioni0_.DBVERSION_ as DBVERSION3_0_, executioni0_.ACTIVITYNAME_ as ACTIVITY4_0_, executioni0_.PROCDEFID_ as PROCDEFID5_0_, executioni0_.HASVARS_ as HASVARS6_0_, executioni0_.NAME_ as NAME7_0_, executioni0_.KEY_ as KEY8_0_, executioni0_.ID_ as ID9_0_, executioni0_.STATE_ as STATE10_0_, executioni0_.SUSPHISTSTATE_ as SUSPHIS11_0_, executioni0_.PRIORITY_ as PRIORITY12_0_, executioni0_.HISACTINST_ as HISACTINST13_0_, executioni0_.PARENT_ as PARENT14_0_, executioni0_.INSTANCE_ as INSTANCE15_0_, executioni0_.SUPEREXEC_ as SUPEREXEC16_0_, executioni0_.SUBPROCINST_ as SUBPROC17_0_ from JBPM4_EXECUTION executioni0_ where executioni0_.ID_=? ) where rownum <= ?

                           

                          and a number of

                           

                              select executions0_.PARENT_ as PARENT14_1_, executions0_.DBID_ as DBID1_1_, executions0_.PARENT_IDX_ as PARENT18_1_, executions0_.DBID_ as DBID1_0_0_, executions0_.DBVERSION_ as DBVERSION3_0_0_, executions0_.ACTIVITYNAME_ as ACTIVITY4_0_0_, executions0_.PROCDEFID_ as PROCDEFID5_0_0_, executions0_.HASVARS_ as HASVARS6_0_0_, executions0_.NAME_ as NAME7_0_0_, executions0_.KEY_ as KEY8_0_0_, executions0_.ID_ as ID9_0_0_, executions0_.STATE_ as STATE10_0_0_, executions0_.SUSPHISTSTATE_ as SUSPHIS11_0_0_, executions0_.PRIORITY_ as PRIORITY12_0_0_, executions0_.HISACTINST_ as HISACTINST13_0_0_, executions0_.PARENT_ as PARENT14_0_0_, executions0_.INSTANCE_ as INSTANCE15_0_0_, executions0_.SUPEREXEC_ as SUPEREXEC16_0_0_, executions0_.SUBPROCINST_ as SUBPROC17_0_0_ from JBPM4_EXECUTION executions0_ where executions0_.PARENT_=?

                           

                          The first one is to load the process inastance whose id you passed and the other ones are for the child execution(s) of that (please note the where clause of the second query). And if your sub process instance exists then one of the executions will have the name as that of the transition you made to reach the sub process. In the sub process execution object you will find the execution for your wait state.

                           

                              The queries to load childen execution instance are made because in hibernate mapping they are non lazy. Here is the snippet of the hibernate mapping (note lazy=false here):

                           

                              <list name="executions"
                                    cascade="all-delete-orphan"
                                    inverse="false"
                                    lazy="false">
                                <key column="PARENT_" foreign-key="FK_EXEC_PARENT" />
                                <list-index column="PARENT_IDX_" />
                                <one-to-many class="ExecutionImpl" />
                              </list>

                           

                              I am sure that it gives the correct list. I tried with Oracle and PostgreSQL (though does not make sense, but still to check if there  is any db dependency), but for both I got the same behaviour.

                           

                              About LazyInitializationException - if you are conversant with hibernate, then you already know that hibernate proxy objects are bound to a session. Once the session is closed they become orphan and become useless. So the probable reason for getting the exception is if you get the main object (here the mainProcessINstance object) in one session and try to get the proxy in another session (or out of the session). You may need to look at the transaction boundaries. Wrong transaction boundary will close the session at a wrong place.

                           

                             If you can give some more detail of your implementation then I can try. I am pretty sure it works.

                          • 10. Re: How to get sub process instance id
                            Paolo F Newbie

                            I can confirm that as soon as you enter a subprocess the following code results in a 0-size executions collection, and this in turn makes it impossible to check if a sub-process is running. I am using 4.3 under hsql.

                             

                            ProcessInstance mainProcessInstance = (ProcessInstance) engine.getExecutionService().findProcessInstanceById(processInstanceId);

                            Collection<? extends Execution> executions = mainProcessInstance.getExecutions();

                            System.out.println("EXECUTIONS IS SIZE: " + executions.size());

                            Is this already on Jira?

                            • 11. Re: How to get sub process instance id
                              Ronald van Kuijk Master
                              PLEASE post full unittests and not snipets in these cases. We'd like to know *everything* you do...;-) to be able to reproduce. One user saying it works, another saying it doesn't...