4 Replies Latest reply on Oct 20, 2011 10:57 AM by rrpeterson

    How to get active processes via ksession.getProcessInstances()  (Jbpm 5.1.1)

    rrpeterson

      Hey guys,

       

      I'm using a single StatefulKnowledgeSession to manage my processes.  Each processInstance is mapped to a userId, which is stored in the params map as a process variable upon the start of the process: 

       

      ksession.startProcess(processId, params)

       

      Later I'd like to check if a specific userId exists in the workflow engine, which I was hoping to check all the active processInstances and search for the userId within their processVariables.

       

      Unfortunately when I use ksession.getProcessInstances() I get 0 results.  If I "cheat" and use ksession.getProcessInstance(procInstId) it returns with a status of Active, so it's definitely there & active.

       

      Is there a way to do a "deep" search to find the active processes?  Is it necessary to store a list of every instantiated process ID to query the ksession individually? 

      I am persisting the processes and StatefulKnowledgeSession, but I have not called .dispose() and am still in the same jvm instance (haven't restored the ksession from the database).

       

      Is there a better way to find if a user is already in a workflow based on process variables?

       

      Thanks for any help!

        • 1. Re: How to get active processes via ksession.getProcessInstances()  (Jbpm 5.1.1)
          amin-mc

          I had the same issue as well.  In the end I created my own JPA query using the entity manager.  I had a requirement to create a runtime operation to check process meta data and this was the only way I could do it.  I'm not convinced my approach is the best way...

          • 2. Re: How to get active processes via ksession.getProcessInstances()  (Jbpm 5.1.1)
            mnorsic

            Hi,

             

            I spotted the exact same problem (please see http://community.jboss.org/message/628732, unrelated to your issue, but I have mentioned it as issue found while creating my processes).

            It seems that Javadoc about method getProcessInstances() does not imply that in persistent scenario this method returns anything:

            "Returns a collection of currently active process instances.  Note that only process instances that are currently loaded and active inside the engine will be returned.

            When using persistence, it is likely not all running process instances will be loaded as their state will be stored persistently.  It is recommended not to use this method to collect information about the state of your process instances but to use a history log for that purpose."

             

            So, I think you have to resort to the following:

            1. create your own JPA query to fetch all running process instances for particular knowledge session, or

            2. use history log

             

            HTH

            Miljenko

            1 of 1 people found this helpful
            • 3. Re: How to get active processes via ksession.getProcessInstances()  (Jbpm 5.1.1)
              salaboy21

              Totally agree with Miljenko, History logs is the way to go for getting that information and not impacting the jBPM5 runtime database.

              Cheers

              1 of 1 people found this helpful
              • 4. Re: How to get active processes via ksession.getProcessInstances()  (Jbpm 5.1.1)
                rrpeterson

                I wound up doing something similar to Amin's solution, where I check the ProcessInstanceInfo table first to see if the session is "alive".  If so I know its still active & working, and if no result is found then it's probable the processInstance is complete.  For anyone else that may have the same issue, I used the JPA query:

                 

                 

                // Query the DB for active processInstances that match the given processId

                final Query nativeQuery = entityManagerFactory.createEntityManager().createQuery("SELECT procInfo from ProcessInstanceInfo procInfo where procInfo.processId = :processId");

                nativeQuery.setParameter("processId", processId);