3 Replies Latest reply on May 6, 2016 10:36 PM by sepideh

    Persist Entity from within ScriptTask - best practice?

    wtimpany

      My question: Is what I am doing sensible/recommended, as I have just started using JBPM.

       

      I have a few Entities stored in my Oracle Database that I want to insert/update/delete as part of my workflow processing.    All the changes need to be processed in the same transaction as the rest of the workflow persistance to ensure the integrity of the flow/data.   I want to keep it all the code as simple as possible.

       

      I am able to do this by using Java within the standard ScriptTask, using the EntityManager extracted from within the kcontext.

       

      The Enities to be updated have been added to the "org.jbpm.persistence.jpa" persistance unit.

       

      Here is the code from within the ScriptTask.   This all works and a new "ScriptActionTriggerBean" is created and persisted to the Oracle DB every time the ScriptTask is called.

       

       

      -- WorkFlow Code Snipit --

      <extensionElements>
      <tns:import name="com.db.gto.tms.agr.bean.model.workflow.ScriptActionTriggerBean" />
      <tns:import name="javax.persistence.EntityManager" />
      <tns:import name="org.drools.runtime.Environment" />
      <tns:import name="org.drools.runtime.EnvironmentName" />
      </extensionElements>

       

      <scriptTask id="_6" name="PreProcess" scriptFormat="http://www.java.com/java" >
      <script>
        ScriptActionTriggerBean satb = new ScriptActionTriggerBean();
        satb.setComments("Starting: Script Pre-Processing");

        Environment   env = kcontext.getKnowledgeRuntime().getEnvironment();
        EntityManager em = (EntityManager) env.get(EnvironmentName.CMD_SCOPED_ENTITY_MANAGER);

        em.persist(satb);
      </script>
      </scriptTask>

        • 1. Re: Persist Entity from within ScriptTask - best practice?
          swiderski.maciej

          In my opinion using script task to perform logic is not best practice - mainly because you embed your java code inside the process definition (bpmn2). You could give a try with following approaches:

          • use Service task that executes (with default work item handler) your java class
          • use domain specific nodes that allows you to do whatever you like

           

          For details about domain specific nodes please refer to the documentation http://docs.jboss.org/jbpm/v5.2/userguide/ch13.html and for ServiceTask take a look at example shipped with jBPM.

           

          HTH

          • 2. Re: Persist Entity from within ScriptTask - best practice?
            wtimpany

            Thanks for the advice,   

             

            Setting up and using the Service Task(s) were easier than I expected.    

             

            Following the instructions in the example worked.  Just a little bit of extra config was required to get the "Service Task Handler" added to the "KnowledgeSession" in our "Spring" configuration

             

             

            e.g.  Spring Config

             

              <

            bean id="jboss.jbpm.serviceTaskHandler" class="org.jbpm.bpmn2.handler.ServiceTaskHandler" />

             

             

              <

            bean id="jboss.jbpm.knowledgeSession1.workItemManager" factory-bean="jboss.jbpm.knowledgeSession1" factory-method="getWorkItemManager"/>

             

             

             

             

             

             

             

             

             

             

             

              <

            bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">

             

                <property name="targetObject" ref="jboss.jbpm.knowledgeSession1.workItemManager"/>

             

                <property name="targetMethod" value="registerWorkItemHandler"/>

             

                <property name="arguments">

             

                <list>

             

                  <value>Service Task</value>

             

                  <ref bean="jboss.jbpm.serviceTaskHandler"/>

             

                </list>

             

                </property>

             

              </bean>

            • 3. Re: Persist Entity from within ScriptTask - best practice?
              sepideh

              hi william

               

              i try to use your practise(script task) but i have a problem

              the value of em (EntityManager em = (EntityManager) env.get(EnvironmentName.CMD_SCOPED_ENTITY_MANAGER);) is null.

              please to help me to in solving this problem