JBoss Community

Re: Access variables bound to process instance during its lifecyle

created by Shobhit Tyagi in jBPM - View the full discussion

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);

  }

}

Reply to this message by going to Community

Start a new discussion in jBPM at Community