12 Replies Latest reply on Mar 22, 2013 8:40 AM by joploya

    Getting process instance variables

    jnorris Newbie

      This question is similar to this discussion:  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

        • 1. Re: Getting process instance variables
          jnorris Newbie

          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?

          • 2. Re: Getting process instance variables
            Maciej Swiderski Master

            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

            1 of 1 people found this helpful
            • 3. Re: Getting process instance variables
              jnorris Newbie

              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

              • 4. Re: Getting process instance variables
                jnorris Newbie

                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

                • 5. Re: Getting process instance variables
                  Maciej Swiderski Master

                  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

                  • 6. Re: Getting process instance variables
                    jnorris Newbie

                    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

                    • 7. Re: Getting process instance variables
                      Maciej Swiderski Master

                      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

                      • 8. Re: Getting process instance variables
                        jnorris Newbie

                        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

                        • 9. Re: Getting process instance variables
                          Eric He Newbie

                          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!

                          • 10. Re: Getting process instance variables
                            Maciej Swiderski Master

                            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

                            • 12. Re: Getting process instance variables
                              joploya Newbie

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

                               

                              This is the error log :

                               

                              Caused by: java.lang.NullPointerException
                                        at com.st.ams.task.management.TaskManager$1.execute(TaskManager.java:264) [classes:]
                                        at com.st.ams.task.management.TaskManager$1.execute(TaskManager.java:258) [classes:]
                                        at org.drools.impl.StatefulKnowledgeSessionImpl.execute(StatefulKnowledgeSessionImpl.java:812) [drools-core-5.5.0.Final.jar:5.5.0.Final]
                                        at org.drools.impl.StatefulKnowledgeSessionImpl.execute(StatefulKnowledgeSessionImpl.java:795) [drools-core-5.5.0.Final.jar:5.5.0.Final]
                                        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_07]
                                        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_07]
                                        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_07]
                                        at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_07]
                                        at org.jboss.weld.bean.proxy.AbstractBeanInstance.invoke(AbstractBeanInstance.java:44) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
                                        at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:105) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
                                        at org.jboss.weld.proxies.CommandExecutor$KnowledgeRuntime$KnowledgeRuntimeEventManager$ProcessEventManager$ProcessRuntime$StatefulKnowledgeSession$StatefulProcessSession$StatefulRuleSession$WorkingMemory$WorkingMemoryEntryPoint$WorkingMemoryEventManager$1366998185$Proxy$_$$_WeldClientProxy.execute(CommandExecutor$KnowledgeRuntime$KnowledgeRuntimeEventManager$ProcessEventManager$ProcessRuntime$StatefulKnowledgeSession$StatefulProcessSession$StatefulRuleSession$WorkingMemory$WorkingMemoryEntryPoint$WorkingMemoryEventManager$1366998185$Proxy$_$$_WeldClientProxy.java) [weld-core-1.1.5.AS71.Final.jar:]
                                        at com.st.ams.task.management.TaskManager.getProcessVariable(TaskManager.java:258) [classes:]
                                        at com.st.ams.task.management.TaskManager$Proxy$_$$_WeldClientProxy.getProcessVariable(TaskManager$Proxy$_$$_WeldClientProxy.java) [classes:]
                                        at com.st.ams.task.display.TaskDisplayBean.getTaskInfo(TaskDisplayBean.java:73) [classes:]
                                        at com.st.ams.task.display.TaskDisplayBean$Proxy$_$$_WeldClientProxy.getTaskInfo(TaskDisplayBean$Proxy$_$$_WeldClientProxy.java) [classes:]
                                        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_07]
                                        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_07]
                                        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_07]
                                        at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_07]
                                        at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:735) [jboss-el-api_2.2_spec-1.0.0.Final.jar:1.0.0.Final]
                                        at javax.el.BeanELResolver.invoke(BeanELResolver.java:467) [jboss-el-api_2.2_spec-1.0.0.Final.jar:1.0.0.Final]
                                        at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:246) [jboss-el-api_2.2_spec-1.0.0.Final.jar:1.0.0.Final]
                                        at org.apache.el.parser.AstValue.getValue(AstValue.java:159) [jbossweb-7.0.13.Final.jar:]
                                        at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189) [jbossweb-7.0.13.Final.jar:]
                                        at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
                                        at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:227) [jsf-impl-2.1.7-jbossorg-2.jar:]
                                        at com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:150) [jsf-impl-2.1.7-jbossorg-2.jar:]
                                        at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85) [jsf-impl-2.1.7-jbossorg-2.jar:]
                                        ... 67 more
                              

                              Thanks and Best Regards.