1 Reply Latest reply on Sep 20, 2013 1:35 AM by Maciej Swiderski

    How to join a JBPM5 transaction in a standalone web design?

    mvermand Newbie

      Hi,

       

      I'm using the JBPM 5.2 jars inside a webapp that runs on a JBoss AS 7.

      jbpmEmf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa"));

       

      JBPM works fine.

      Now I want to create custom JPA entities and manipulate these together with the JBPM entities.

      Example: when a requests comes to start an instance of a Process, I want to store the external Id that is provided in the request because in that way I can reload the specific kbase and ksession when later on a sequential request is made with the same external Id as argument.

       

      My problem is that I do not know how to join the transaction that will be used by JBPM to create the process instance entity and workitem entities...

       

      I extended the persistence.xml:

       

      <persistence-unit name="org.jbpm.persistence.jpa"
              transaction-type="JTA">
              <provider>org.hibernate.ejb.HibernatePersistence</provider>
              <jta-data-source>jdbc/WmsFlow</jta-data-source>
              <mapping-file>META-INF/JBPMorm.xml</mapping-file>
              <mapping-file>META-INF/Taskorm.xml</mapping-file>
              <mapping-file>META-INF/ProcessInstanceInfo.hbm.xml</mapping-file>
              <class>org.jbpm.persistence.processinstance.ProcessInstanceInfo</class>
              <class>org.drools.persistence.info.SessionInfo</class>
         ...
              <class>org.jbpm.task.OnParentAbortAllSubTasksEndStrategy</class>
              <class>org.jbpm.task.OnAllSubTasksEndParentEndStrategy</class>
              <class>org.jbpm.task.User</class>
      
              <!-- START OF WMS ENTITIES -->
              <class>be.wms.DeploymentProperty</class>
              <class>be.wms.Deployment</class>
              <class>be.wms.Scenario</class>
              <class>be.wms.ScenarioInstanceLog</class>
              <class>be.wms.Artifact</class>
              <class>be.wms.ItemEvent</class>
              <class>be.wms.SessionLookup</class>        
              <!-- END OF WMS ENTITIES -->
      
      
              <properties>
                  <property name="hibernate.max_fetch_depth" value="3" />
                  <property name="hibernate.hbm2ddl.auto" value="none" />
                  <property name="hibernate.show_sql" value="false" />
                  <property name="hibernate.transaction.manager_lookup_class"
                      value="org.hibernate.transaction.JBossTransactionManagerLookup" />
                  <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
              </properties>
          </persistence-unit>
      
      

       

      I have created a EntityManagerFactory:

      jbpmEmf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
      
      

       

      Now, before I start a new process instance, I want to store the external Id in the same JTA transaction as will be used by JBPM.

      I have a REST resource in my app that is called to start a process instance:

       

      (simplified code:)

      @PUT
      @Path("/{name}/{version}")
      @Produces(MediaType.APPLICATION_JSON)
      public ScenarioInstance startProcessInstance(@PathParam("name") String name, @PathParam("version") String version,
              Map<String, Object> params) {
         
          String externalId = params.get("externalid").toString();
         
          KnowledgeBase kbase = buildKnowledgeBase(name, version); // support for different versions.
         
          // jbpmEmf is a EntityMananagerFactory created from "org.jbpm.persistence.jpa"
          StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, createEnvironment(jbpmEmf));
          int ksessionId = ksession.getId();
         
          WMSSession wsession = new WMSSession(externalId, ksessionId);
         
          //=> how to get the EntityManager used by JBPM to persist wsession?
          EntityManager em = ??? <===
          em.persist(wsession);
         
          ProcessInstance processInstance = ksession.startProcess(processId, params);
         
          ...
      }
      
      /**
      * Create a new environment
      * @param emf the EntityManagerFactory
      * @return new Environment
      * @throws NamingException
      */
      protected static Environment createEnvironment(EntityManagerFactory emf) throws NamingException {
          Environment env = KnowledgeBaseFactory.newEnvironment();
          env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
          env.set(EnvironmentName.TRANSACTION_MANAGER, getTransactionManager());
          return env;
      }
      
      
      

       

      So my question is: (line 17): how to get the EntityManager uwed by JBPM to persist the wsession object?

       

      Thanks,

      Michiel