8 Replies Latest reply on Aug 4, 2010 3:13 PM by matrixpooh

    Get subprocess id from outside the process

    matrixpooh

      Hi,

       

      My unit test starts a process.

      The process starts a subprocess at some point in time. How can I get a subprocess id in my test having a process id?

        • 1. Re: Get subprocess id from outside the process
          rebody

          Hi Al,

           

          There is a new API in org.jbpm.api.Exection interface in jBPM-4.4.  named getSubProcessInstance().  You could use it to get related sub process instance of an execution.

           

          In jBPM-4.3,  this method wasn't expose in API interface,  so you have to cast class by yourself and get related sub process instance from ExecutionImpl.

          • 2. Re: Get subprocess id from outside the process
            matrixpooh

            Thanks for help, HuiSheng, but it still doesn't work:

             

            Execution exe = processInstance.findActiveExecutionIn("subprocess-task");
            String subprocessid = ((ExecutionImpl)execution).getSubprocessInstance(); -- fails with
            
            org.hibernate.LazyInitializationException:could not initialize proxy - no Session
            
            
            

             

            Modified jbpm.execution.hbm.xml to do eager fetching. Works now.

             

            <many-to-one name="subProcessInstance" ... lazy="false"/>
            
            • 3. Re: Get subprocess id from outside the process
              mwohlf

              use a command:


              processEngine.execute(new Command() {
                public Object execute(Environment env) {
                  Execution exe = processInstance.findActiveExecutionIn("subprocess-task");
                  String subprocessid = ((ExecutionImpl)execution).getSubprocessInstance();

                  System.out.printlnl("subprocessid is: " + subprocessid);
                  return null;
                }
              });

              • 4. Re: Get subprocess id from outside the process
                matrixpooh
                final Execution exectution = processInstance.findActiveExecutionIn("subprocess");
                String pid = processEngineFactory.getProcessEngine().execute(
                 new Command<String>(){
                   public String execute(Environment env){
                    return ((ExecutionImpl)execution).getSubProcessInstance().getId();
                   }
                 }
                ); 
                

                 

                fails with LazyInitializationException - i.e. the same exception as before

                • 5. Re: Get subprocess id from outside the process
                  mwohlf

                  that's interesting,

                  the following code should check for the hibernate session in your environment:

                   

                      processEngine.execute(
                           new Command(){
                             public String execute(Environment env){
                                 org.hibernate.Session session = env.get(org.hibernate.Session.class);
                                 System.out.println("session is: " + session);
                                 return null;
                             }
                           }
                          );

                   

                  if it is null you might have a problem in the session config for your tests, but then there should be way more problems, are you running other tests that pass? Also note you had "exectution" and "execution" in the code fragment.

                  • 6. Re: Get subprocess id from outside the process
                    matrixpooh

                    My company disables copy/paste of the content. I had to type in the code manually so must've mistyped the variables. The session is definitely present.

                    • 7. Re: Get subprocess id from outside the process
                      mwohlf

                      I tried to reproduce this issue so I added a testcase to

                      org.jbpm.examples.subprocess.outcomeactivity.SubProcessOutcomeActivityTest

                       

                      but the following testcase passed for me:

                      public void testSubProcessIdQuery() {
                              ProcessInstance processInstance = executionService.startProcessInstanceByKey("SubProcessDocument");
                              final Execution execution = processInstance.findActiveExecutionIn("review");
                              assertNotNull(execution);
                              assertNotNull(processEngine);
                              String pid = ((ExecutionImpl) execution).getSubProcessInstance().getId();
                              assertNotNull(pid);
                              List<Task> taskList = taskService.findPersonalTasks("johndoe");
                              Task task = taskList.get(0);
                              taskService.completeTask(task.getId(), "reject");
                      }
                      

                       

                      so maybe if you post your testcase someone is able to shed some light on this

                      1 of 1 people found this helpful
                      • 8. Re: Get subprocess id from outside the process
                        matrixpooh

                        Tried with the SubProcessOutcomeActivityTest - worked like a charm. Extended the process defnition to include <foreach /> - still works

                         

                        Looking into specifics of my process defnitions. Will keep you posted.

                         

                        Thanks a bunch for your help!