4 Replies Latest reply on Feb 23, 2015 10:15 AM by bsum

    How to get KieSession for a process launched from jbpm-console

    ghollins

      Hi,

       

         I'm using jbpm 6.0.0.Final, and I have set it up to use a custom WorkItemHandler (by creating my own handlers JAR and putting it in WEB-INF/lib).  My custom WorkItemHandler gets called as expected when I launch a process from jbpm-console.  However, I'm having trouble getting a hold of the KieSession associated with the process from within the WorkItemHandler.

       

      I've tried several approaches so far.

       

      Attempt #1:

      (see http://stackoverflow.com/questions/20862350/accessing-kiesession-from-jbpm6-workitemhandler)

       

      Attempt #2:

        RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().get();

        RuntimeManager manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);

        RuntimeEngine runtimeEngine = manager.getRuntimeEngine(EmptyContext.get());

        KieSession ksession = runtimeEngine.getKieSession();


      This results in:

      13:46:22,149 ERROR [builtin.jbpm.JbpmTask] (http-localhost-127.0.0.1-8080-3) problem getting processInstance: javax.persistence.PersistenceException: No Persistence provider for EntityManager named org.jbpm.persistence.jpa

        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69) [hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final]

        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) [hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final]

        at org.jbpm.runtime.manager.impl.DefaultRuntimeEnvironment.init(DefaultRuntimeEnvironment.java:72) [jbpm-runtime-manager-6.0.0.Final.jar:6.0.0.Final]

        at org.jbpm.runtime.manager.impl.RuntimeEnvironmentBuilder.get(RuntimeEnvironmentBuilder.java:314) [jbpm-runtime-manager-6.0.0.Final.jar:6.0.0.Final]

        at org.jbpm.runtime.manager.impl.RuntimeEnvironmentBuilder.get(RuntimeEnvironmentBuilder.java:56) [jbpm-runtime-manager-6.0.0.Final.jar:6.0.0.Final]

      ...

       

      This error is thrown from this line:

      RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().get();

       

      I'm using the out-of-the-box full installer, and therefore it's using H2.

       

      I know there must be a KieSession.  I just want to re-use this existing one, and NOT create a new one.  My question is just "How do I get access to it?"

       

      Thanks!

        • 1. Re: How to get KieSession for a process launched from jbpm-console
          swiderski.maciej

          how do you register work item handlers? Via CDI producers, conf file or? Look at the docs about registering work item handlers as there is some info on how to get hold of the RuntimeManager, RuntimeEngine and KieSession.

           

          HTH

          • 2. Re: How to get KieSession for a process launched from jbpm-console
            ghollins

            Hi Maciej,

             

             

               I did the following:

             

             

            1) Defined a work item definition in jbpm-console "Work Item Editor".  For example:

            [
                "name" : "Sleep",
                "parameters" : [
                  "durationMillis" : new StringDataType()
                ],
                "displayName" : "Sleep",
                "icon" : "defaultservicenodeicon.png"
              ]
            

             

             

            2) Added an entry in deployments/jbpm-console.war/WEB-INF/classes/META-INF/CustomWorkItemHandlers.conf:

            [
              "Log": new org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler(),
              "WebService": new org.jbpm.process.workitem.webservice.WebServiceWorkItemHandler(ksession),
              "Rest": new org.jbpm.process.workitem.rest.RESTWorkItemHandler(),
              "Service Task" : new org.jbpm.process.workitem.bpmn2.ServiceTaskHandler(ksession),
              "Sleep": new jpl.cws.task.builtin.jbpm.SleepTask()
            ]
            

             

             

            3)  Put my custom JAR under deployments/jbpm-console.war/WEB-INF/lib/

             

             

            +++++++++++++++++++++++++++++++++++++++++++++

             

            I just noticed that I'm not passing the "ksession" parameter to my SleepTask constructor in the CustomWorkItemHandlers.conf.  Would this be the reason why I'm not able to get a hold of it?

            The documentation says that work item handlers that are "stateless or requiring only KieSession" can be done in the way I'm trying to do it.  This should work for my use case, since my service will be stateless.

             

            Thanks,

            Galen

            • 3. Re: How to get KieSession for a process launched from jbpm-console
              swiderski.maciej

              Galen Hollins wrote:

               

              I just noticed that I'm not passing the "ksession" parameter to my SleepTask constructor in the CustomWorkItemHandlers.conf.  Would this be the reason why I'm not able to get a hold of it?

              yes, if you include ksession in the constructor of the work item handler when defining it in the conf file then the work item handler will get ksession injected. Make sure you have proper constructor in your handler implementation that accepts ksession as argument.

               

              HTH

              • 4. Re: How to get KieSession for a process launched from jbpm-console
                bsum

                Hi Maciej,

                 

                What if I wanted to get  the RuntimeManager, RuntimeEngine and KieSession from another war file that's deployed on the same server?   I plan to setup a mdb to listen to a jms queue to complete my custom work items.

                 

                Thanks!