Discussion:
[jboss-user] [jBPM] - Getting process instance variables
jnorris
2012-08-16 19:46:40 UTC
Permalink
jnorris [https://community.jboss.org/people/jnorris] created the discussion

"Getting process instance variables"

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

--------------------------------------------------------------
This question is similar to this discussion:  https://community.jboss.org/thread/204026?tstart=0 https://community.jboss.org/thread/204026?tstart=0

However I need to get the process variables from a process instance that is retrieved using:

WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance( processInstanceId );

I've looked at the data in the debugger and there is nothing in the metaData.

Is there a way to get that information?  Is there a way to get a org.jbpm.process.instance.ProcessInstance as stated in the referenced discussion?

Thanks,
Jim
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
jnorris
2012-08-16 20:03:09 UTC
Permalink
jnorris [https://community.jboss.org/people/jnorris] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
Ok, I see the getVariable() method which works.  However I need to get a list of all variables.  Is there a way to do that?
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Maciej Swiderski
2012-08-17 12:31:30 UTC
Permalink
Maciej Swiderski [https://community.jboss.org/people/swiderski.maciej] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
Try casting ksession.getProcessInstance() to org.jbpm.process.instance.ProcessInstance as it should implement it so you will have access to the context instance.

HTH
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
jnorris
2012-08-17 12:35:37 UTC
Permalink
jnorris [https://community.jboss.org/people/jnorris] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
Hi Maciej,

Thanks for the response.  I looked at the actual return object and it is a RuleFlowProcessInstance which does have a getVariables method so if I cast to the impl instead of the interface I should be ok.  I'd rather use an interface though so I'll try your suggestion.

Regards,
Jim
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
jnorris
2012-08-17 12:57:56 UTC
Permalink
jnorris [https://community.jboss.org/people/jnorris] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
Hi Maciej,

I tried casting to org.jbpm.process.instance.ProcessInstance as suggested and used the code suggested in the other post.  However this line causes a null pointer exception:

|
|
|
| VariableScopeInstance variableScope = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE); |

Casting to RuleFlowProcessInstance works.

Regards,
Jim
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Maciej Swiderski
2012-08-17 13:11:05 UTC
Permalink
Maciej Swiderski [https://community.jboss.org/people/swiderski.maciej] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
do you use persistence in your case? If so it's better to use command then to make sure everything is available.

Do you have a stack trace for this NPE?

HTH
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
jnorris
2012-08-17 13:43:08 UTC
Permalink
jnorris [https://community.jboss.org/people/jnorris] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
Hi Maciej,

Yes we are using persistence.  The api call to start a process and the call to get a process instance take place in seperate web service calls.

Here's the code:

org.jbpm.process.instance.ProcessInstance processInstance = (org.jbpm.process.instance.ProcessInstance) ksession.getProcessInstance( processInstanceId );
VariableScopeInstance variableScope = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
Map<String, Object> variables = variableScope.getVariables();

Here's the stacktrace:

2012-08-17 08:47:36,324 ERROR [ProcessInstanceProviderJbpm] (http-127.0.0.1-8080-1) ProcessInstanceProviderJbpm.getProcessDefinitionInstance() - Exception!
java.lang.NullPointerException
          at org.jbpm.process.instance.impl.ProcessInstanceImpl.getProcess(ProcessInstanceImpl.java:67)
          at org.jbpm.process.instance.impl.ProcessInstanceImpl.getContextInstance(ProcessInstanceImpl.java:127)
          at com.pb.keystone.bpm.provider.jbpm.ProcessInstanceProviderJbpm.getProcessDefinitionInstance(ProcessInstanceProviderJbpm.java:218)
          at com.pb.keystone.bpm.BPMServiceImpl.getProcessInstance(BPMServiceImpl.java:569)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          at java.lang.reflect.Method.invoke(Unknown Source)
          at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:173)
          at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:89)
          at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:60)
          at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:75)
          at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
          at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
          at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
          at java.util.concurrent.FutureTask.run(Unknown Source)
          at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
          at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:106)
          at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
          at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:111)
          at org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:99)
          at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:431)
          at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:186)
          at org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:148)
          at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)
          at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:103)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
          at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
          at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:183)
          at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:95)
          at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
          at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
          at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.internalProcess(ActiveRequestResponseCacheValve.java:74)
          at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:47)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
          at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:599)
          at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:451)
          at java.lang.Thread.run(Unknown Source)

Regards,
Jim
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Maciej Swiderski
2012-08-17 14:16:14 UTC
Permalink
Maciej Swiderski [https://community.jboss.org/people/swiderski.maciej] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
alright, kind of expected this error, you should run command in this case, something like this:


Map<String, Object> variables = ksession.execute(new GenericCommand<Map<String, Object>>() {
 
            public Map<String, Object> execute(Context context) {
                StatefulKnowledgeSession ksession = ((KnowledgeCommandContext) context).getStatefulKnowledgesession();
                ProcessInstance processInstance = (ProcessInstance) ksession.getProcessInstance(piId);
                VariableScopeInstance variableScope = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
        Map<String, Object> variables = variableScope.getVariables();
                return variables;
            }
        });

HTH
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
jnorris
2012-08-17 14:45:33 UTC
Permalink
jnorris [https://community.jboss.org/people/jnorris] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
Hi Maciej,

I tried your suggestion and it works and is a better solution than casting to an implementation.

Thank you for your help,
Jim
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Eric He
2012-12-14 06:24:42 UTC
Permalink
Eric He [https://community.jboss.org/people/ehe888] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
Hi Maciej

Thank you very  much for sharing this answer, it do help to resolve the issue.

My question, is why should I wrap it in GenericCommand will avoid the NullPointException?

Thanks Best Regards!
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Maciej Swiderski
2012-12-14 11:04:13 UTC
Permalink
Maciej Swiderski [https://community.jboss.org/people/swiderski.maciej] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
Eric, it is due to processinstance is disconnected from the runtime on transaction end so it removes some state related information and one of them is process definition. When enclosing it in command you ensure it will be executed within anther transaction and process instance will be reconnected with the runtime

HTH
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Eric He
2012-12-14 13:05:54 UTC
Permalink
Eric He [https://community.jboss.org/people/ehe888] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
https://community.jboss.org/people/swiderski.maciej Maciej, Thanks a lot!
--------------------------------------------------------------

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

Start a new discussion in jBPM at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034]
Sandra Renaud
2013-03-22 10:07:05 UTC
Permalink
Sandra Renaud [https://community.jboss.org/people/joploya] created the discussion

"Re: Getting process instance variables"

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

--------------------------------------------------------------
Hello,

I try this solution to retrieve processInstance varaibles but I got a NullPointerException.

Before I tried this solution : https://community.jboss.org/message/798123#798123 https://community.jboss.org/message/798123 without success.

Eric how can you do to have a not null processInstance please?

Here my code :
/**
           * This method allow to access object store in
           * process instance variables
           * @return an object link to the processInstance
           */
          public Object getProcessVariable(Long processInstanceId, Long taskId, String key){
                    final Long piId = processInstanceId;
                    Object retObj = null;
                    WorkflowProcessInstance wpi = (WorkflowProcessInstance)kSession.getProcessInstance(processInstanceId);
                    if((wpi == null) || (wpi.getVariable(key) == null)){

                              //method1 : retrieve content map variables
                              Task task = getTaskService().getTask(taskId);
                              TaskData tData = task.getTaskData();
                              Long docContentId = tData.getDocumentContentId();
                              System.out.println("process instance is null, doc contentId = "+docContentId);
                              Content content = getTaskService().getContent(docContentId);
                              Map<?, ?> variables = ((Map<?, ?>) ContentMarshallerHelper.unmarshall(content.getContent(), null));

                              //display keys
                              System.out.println("content map keys :");
                              for(Object o : variables.keySet()){
                                        System.out.println(o.toString());
                              }
                              System.out.println("and key search is "+key);
                              /*
                              content map keys :
                                        ActorId
                                        Skippable
                                        TaskName
                                        GroupId

                              key search is formId
                              */
                              retObj = variables.get(key);

                              //method 2 : retrieve the the variable Scope
                              Map<String, Object> variables2 = kSession.execute(new GenericCommand<Map<String, Object>>(){


                                        @Override
                                        public Map<String, Object> execute(Context context) {
                                                  StatefulKnowledgeSession ksession = ((KnowledgeCommandContext) context).getStatefulKnowledgesession();
                                                  ProcessInstanceImpl processInstance = (ProcessInstanceImpl) ksession.getProcessInstance(piId);
                                                  VariableScopeInstance variableScope = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE); // ==> NullPointerException
                                                  Map<String, Object> variables = variableScope.getVariables();
                                                  return variables;
                                        }

                              });
                              //display keys
                              System.out.println("variables keys :");
                              for(Object o : variables2.keySet()){
                                        System.out.println(o.toString());
                              }
                              System.out.println("key search is "+key);

                              retObj = variables2.get(key);
                    }else{
                              retObj = wpi.getVariable(key);
                    }
                    return retObj;
          }



Thanks and Best Regards.
--------------------------------------------------------------

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

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