5 Replies Latest reply on Jun 21, 2013 9:34 AM by ashpcs

    Access variables bound to process instance during its lifecyle

    hoang_to

      Having look at the API of JBPM, i see that while triggering a process via engine session, it is possible to parse variables

       

      StatefulKnowledgeSession session = ...

      Map<String, Object> params = ....

      session.startProcess("process_id", params)

       

      Objects stored on that map, which is associated with a process instance, are somehow accessible via contextual process instance.

       

      My question is: What is the canonical way to read/write those variables during process instance lifecycle (ex: in order to communicate with external systems)

        • 1. Re: Access variables bound to process instance during its lifecyle
          roxy1987

          To read a process variable you could use the following code :

           

           

          public Map<String, Object> getProcessVariables(final long intProcessInstId
          
          ) {
            StatefulKnowledgeSession objKSession = getSession(); //Get the session
             Map<String, Object> hshVariables = null;
            
            try {
             hshVariables = objKSession.execute(new GenericCommand<Map<String, Object>>() {
              private static final long serialVersionUID = 1L;
          
              public Map<String, Object> execute(Context objContext) {
                     StatefulKnowledgeSession objKSession = ((KnowledgeCommandContext) objContext)
                                  .getStatefulKnowledgesession();
                     org.jbpm.process.instance.ProcessInstance objProcessInstance = (org.jbpm.process.instance.
                                     ProcessInstance) objKSession.
                                     getProcessInstance(intProcessInstId);
                     VariableScopeInstance objVariableScope = (VariableScopeInstance) objProcessInstance.
                                    getContextInstance(VariableScope.VARIABLE_SCOPE);
                  /*Obtiene las variables del Proceso*/
                     Map<String, Object> hshVariables = objVariableScope.getVariables();
                     return hshVariables;
                 }
             });
            } catch(Exception objException) {
             objLogger.loggerError(objException);
            } 
            return hshVariables;
           }
          
          

           

          And to put a variable in the process

           

           

          public void setProcessVariables(final long intProcessInstId, final Map<String, Object> hshVariableMap)

          {

            StatefulKnowledgeSession objKSession = getSession(); //Get the session

            try {

             objKSession.execute(new GenericCommand<Map<String, Object>>() {

            

              private static final long serialVersionUID = 1L;

           

              public Map<String, Object> execute(Context objContext) {

                     StatefulKnowledgeSession objKSession = ((KnowledgeCommandContext) objContext).

                                getStatefulKnowledgesession();

                     org.jbpm.process.instance.ProcessInstance objProcessInstance = (org.jbpm.process.instance.

                                     ProcessInstance) objKSession.

                                     getProcessInstance(intProcessInstId);

                     VariableScopeInstance objVariableScope = (VariableScopeInstance) objProcessInstance.

                                 getContextInstance(VariableScope.VARIABLE_SCOPE);

                     Iterator<Map.Entry<String, Object>> objIterator = hshVariableMap.entrySet().iterator();

                     Map.Entry<String, Object> objPairs;

                     while(objIterator.hasNext()) {

                      objPairs = (Map.Entry<String, Object>)objIterator.next();

           

                      objVariableScope.setVariable(objPairs.getKey(), objPairs.getValue());

                     }

                     return null;

                 }

             });

            } catch(Exception objException) {

             objLogger.loggerError(objException);

            }

          }

          1 of 1 people found this helpful
          • 2. Re: Access variables bound to process instance during its lifecyle
            hoang_to

            Ok, so the gist is to wrapp business code requiring contextual variables in callback (to have access to Context object). Thanks Shobhit

            • 3. Re: Access variables bound to process instance during its lifecyle
              prathap.vs

              Hi,

                   We are trying to find process variables by the method mentioned above. But we are unable to find process variables. The list is coming as empty. We have defined two process variables in vardefs section while creating a workflow process in designer. Let me know if you have got through this approach and got process variables.

               

              Thanks,

              Prathap

              • 4. Re: Access variables bound to process instance during its lifecyle
                hoang_to

                @Prathap: Make sure that while you created your process definition via web-based editor, you have defined a list of variables. Just open the process definition editor, then click the arrow button on right side of editor to show the popup allowing to edit metadata (including list of variables) of your process

                 

                Having played with JBPM for a while, i figured out that fetching variables in this way is not a good manner. We had better configure the process variable <--> task params bindings in process designer tool

                • 5. Re: Access variables bound to process instance during its lifecyle
                  ashpcs

                  Hi Minh,

                   

                  can you let me know why--> n click the arrow button on right side of editor to show the popup allowing to edit metadata (including list of variables) of your process.

                  Is not a good idea?  Is it because of designer limitation?