Discussion:
[jboss-user] [jBPM] - Access variables bound to process instance during its lifecyle
Minh Hoang TO
2013-05-22 19:08:15 UTC
Permalink
Minh Hoang TO [https://community.jboss.org/people/hoang_to] created the discussion

"Access variables bound to process instance during its lifecyle"

To view the discussion, visit: https://community.jboss.org/message/818949#818949

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

Reply to this message by going to Community
[https://community.jboss.org/message/818949#818949]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Shobhit Tyagi
2013-05-23 19:11:20 UTC
Permalink
Shobhit Tyagi [https://community.jboss.org/people/roxy1987] created the discussion

"Re: Access variables bound to process instance during its lifecyle"

To view the discussion, visit: https://community.jboss.org/message/819172#819172

--------------------------------------------------------------
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
[https://community.jboss.org/message/819172#819172]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Minh Hoang TO
2013-05-28 03:42:26 UTC
Permalink
Minh Hoang TO [https://community.jboss.org/people/hoang_to] created the discussion

"Re: Access variables bound to process instance during its lifecyle"

To view the discussion, visit: https://community.jboss.org/message/819750#819750

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

Reply to this message by going to Community
[https://community.jboss.org/message/819750#819750]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Prathap V S
2013-06-17 07:38:31 UTC
Permalink
Prathap V S [https://community.jboss.org/people/prathap.vs] created the discussion

"Re: Access variables bound to process instance during its lifecyle"

To view the discussion, visit: https://community.jboss.org/message/823388#823388

--------------------------------------------------------------
Hi,
     We are trying to find process variables by the method mentioned above. But i couldn't find a way to get the objContext. Let me know if you have got through this approach and got process variables.

Thanks,
Prathap
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/823388#823388]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Minh Hoang TO
2013-06-17 10:03:54 UTC
Permalink
Minh Hoang TO [https://community.jboss.org/people/hoang_to] created the discussion

"Re: Access variables bound to process instance during its lifecyle"

To view the discussion, visit: https://community.jboss.org/message/823423#823423

--------------------------------------------------------------
@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 :)
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/823423#823423]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Ashish T
2013-06-21 13:34:34 UTC
Permalink
Ashish T [https://community.jboss.org/people/ashpcs] created the discussion

"Re: Access variables bound to process instance during its lifecyle"

To view the discussion, visit: https://community.jboss.org/message/824289#824289

--------------------------------------------------------------
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? 
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/824289#824289]

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Loading...