4 Replies Latest reply on Feb 25, 2009 9:27 AM by apdo

    JbpmContext usage & best practices

    Lucas Gonzalez Newbie

      Hi!,
      I am using JBPM + Spring (springmodules0.8a) + Struts to create an application.

      I would like to know what are the best practices regarding the jbpmContext and how to create it/reuse it.

      This is my spring configuration

       <bean id="dc-workflow"
       class="org.springmodules.workflow.jbpm31.definition.ProcessDefinitionFactoryBean">
       <property name="definitionLocation"
       value="classpath:dc/processdefinition.xml"/>
       </bean>
      
       <bean id="jbpmConfiguration"
       class="com.ea.dc.core.CustomLocalJbpmConfigFactoryBean">
       <property name="sessionFactory" ref="sessionFactory"/>
       <property name="configuration" value="classpath:jbpm.cfg.xml"/>
       <!-- property name="configuration" value="classpath:jbpm.cfg.xml"/ -->
       <property name="processDefinitions">
       <list>
       <ref local="dc-workflow"/>
       </list>
       </property>
       <property name="processDefinitionsResources">
       <list>
       <value>classpath:dc/processdefinition.xml</value>
       </list>
       </property>
       </bean>
      
       <!-- jBPM template -->
       <bean id="jbpmTemplate" class="org.springmodules.workflow.jbpm31.JbpmTemplate">
       <constructor-arg index="0" ref="jbpmConfiguration"/>
       <constructor-arg index="1" ref="dc-workflow"/>
       </bean>


      and then I am injecting those beans to a service of mine...
      now, whenever I want to do something with the workflow, I do something like:
      /** Moves the process forward.
       *
       * @param jobId the process instance id
       * @param transitionId the transition id to select
       */
       public void moveForward(final Long jobId, final String transitionName){
       JbpmContext jbpmContext = getContext();
      
       try {
       ProcessInstance instance = jbpmContext.getProcessInstanceForUpdate(jobId);
      
       instance.signal(transitionName);
       } finally {
       // make sure we save everything
       jbpmContext.close();
       }
       }

      as shown in the documentation...

      the thing is... I am closing the context every time I leave a service method...

      isn't it better to leave it open? Is this a good practice?
      I was thinking about using something similar to openSessionInView for the jbpmContext (though I'm not sure how to do it)... will that be better?

      Any ideas/pointers would be great!

      Thanks a lot,
      Lucas

        • 1. Re: JbpmContext usage & best practices
          Ronald van Kuijk Master

           

          isn't it better to leave it open?


          No

          Is this a good practice?


          Leaving open? No... Closing? Yes

          I was thinking about using something similar to openSessionInView for the jbpmContext (though I'm not sure how to do it)...
          It is an option, but does not thing more than hiding it somewhere. If you do not want to open/close it all the time...

          will that be better?

          Better in providing cleaner code maybe, but nothing more

          • 2. Re: JbpmContext usage & best practices
            Lucas Gonzalez Newbie

            Thanks for the reply...

            unfortunately, if I close the context every time (with the finally clause) the performance is terrible...

            I thought that maybe I could have the context get closed by spring as soon as the entire transaction is finished....

            there are some cases where the context gets opened 5/6 times for a single request.. :(

            • 3. Re: JbpmContext usage & best practices
              Ronald van Kuijk Master

              closing and opening the context is not expensive afaik... recreating a new JbpmConfiguration is!!!

              I thought that maybe I could have the context get closed by spring as soon as the entire transaction is finished....


              Yes that is an option, but just make sure that one transaction does not span multiple requests (realy keeping it open). . That way your application would not scale.

              there are some cases where the context gets opened 5/6 times for a single request.. :(


              Why? I've never had that. Sure there is no 'design error' here?

              • 4. Re: JbpmContext usage & best practices
                apdo Newbie

                We are using jbpm with EJB3. we use jbpm beind SLSB

                Here is a solution that we have implemented.

                We have implemented an Interceptor (using jboss AOP) that identify when it exit from the latest layer of a SLSB (exit from the business layer). When the aspect identify that the execution path is going to exit from the business layer it close the ejbcontext.

                In the Interceptor, we identify when we exit from the first called SLSB (exit from the business layer) by using a counter in the Interceptor. the counter is incremented when entering in the aspect and in the finaly block, if the counter is 0 the jbpmcontext is closed if it has been opened during the transaction.


                It will have been easier to register a listener to the jboss transaction but I didn`t find anyway to do it. Is there a way to doing it?

                An Phong Do
                www.solabs.com