Search in sources :

Example 6 with BusinessEntity

use of com.salaboy.sessions.patterns.BusinessEntity in project jBPM5-Developer-Guide by Salaboy.

the class SessionsPatternsTestsBase method markBusinessEntityAsCompleted.

/**
     * Sets the 'active' property of the businessEntity as 'false' and persists
     * it into the database.
     * @param businessEntity the buisnessEntity to be marked as not active.
     * @param em the EntityManager to be used.
     */
public void markBusinessEntityAsCompleted(Long businessEntityId, EntityManager em) {
    em.joinTransaction();
    BusinessEntity businessEntity = em.find(BusinessEntity.class, businessEntityId);
    businessEntity.setActive(false);
    System.out.println("Merging Business Entity: " + businessEntity);
    em.merge(businessEntity);
}
Also used : BusinessEntity(com.salaboy.sessions.patterns.BusinessEntity)

Example 7 with BusinessEntity

use of com.salaboy.sessions.patterns.BusinessEntity in project jBPM5-Developer-Guide by Salaboy.

the class SingleSessionPatternsTest method singleSessionPerProcessDefinition.

/**
     * This test starts 2 process instances of the same process definition
     * in the same sessions.
     * @throws Exception 
     */
@Test
public void singleSessionPerProcessDefinition() throws Exception {
    //Creates an entity manager and get the user transaction. We are going
    //to need them later to interact with the business entities persisted
    //by the work item handlers we have configured in our session.
    EntityManager em = getEmf().createEntityManager();
    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    //Initial parameters for process instance #1
    Person person = new Person("Salaboy", 29);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", person);
    //Creates the ksession for process instance #1
    StatefulKnowledgeSession ksession1 = createProcessKnowledgeSession("myProcessDefinitionSession");
    registerWorkItemHandlers(ksession1, null, em);
    //Starts process instance #1
    ksession1.startProcess("com.salaboy.process.AsyncInteractions", params);
    //We don't want to use the ksession anymore so we will dispose it.
    //At this point MockAsyncExternalServiceWorkItemHandler has persisted
    //a business key that we can use later to retireve the session from
    //the database and continue with the execution of the process.
    ksession1.dispose();
    //Let's retrieve the business entity persisted by the work item handler
    //in order to get the session id where the process was running.
    //In this case, we could have just get the session id from ksession1
    //object, but we are emulating a real world situation here.
    List<BusinessEntity> activeBusinessEntities = getActiveBusinessEntities(em);
    Assert.assertEquals(1, activeBusinessEntities.size());
    int ksession1Id = activeBusinessEntities.get(0).getSessionId();
    //In this case we want to start the new process instance in the same 
    //ksession we used before. That is why we first need to retrieve it from
    //the database.
    ksession1 = loadKnowldgeSession(ksession1Id, "myProcessDefinitionSession", null, em);
    assertNotNull(ksession1);
    //Let's prepare a new set of data to start a new process instance of 
    //the same process definition we used before.
    Person person2 = new Person("Salaboy", 29);
    Map<String, Object> params2 = new HashMap<String, Object>();
    params2.put("person", person2);
    //Starts process instance #1
    ksession1.startProcess("com.salaboy.process.AsyncInteractions", params2);
    //We are no longer interested in the session, so we can dispose it.
    ksession1.dispose();
    //Getting the correct work item to finish:
    //If we don't know which workItem do we want to complete we can create 
    //a query to see which are pending work items for a process or for a 
    //more complex business key.
    //If the thread that wants to notify the engine about the completion of 
    //the external interaction is the one which has created the token inside 
    //the WorkItemHandler it can use that unique value to get the related 
    //workItemId.
    BusinessEntity businessEntityByWorkItemId = getBusinessEntityByWorkItemId(1L, em);
    //Before completing the work item we need to reload the session once again.
    ksession1 = loadKnowldgeSession(businessEntityByWorkItemId.getSessionId(), "myProcessDefinitionSession", null, em);
    assertNotNull(ksession1);
    try {
        // This needs to happen in the same transaction in order to be consistent
        ut.begin();
        //complete the pending work item handler
        ksession1.getWorkItemManager().completeWorkItem(businessEntityByWorkItemId.getWorkItemId(), null);
        //mark the BusinessEntity as completed
        markBusinessEntityAsCompleted(businessEntityByWorkItemId.getId(), em);
        ut.commit();
    } catch (Exception e) {
        System.out.println("Rolling back because of: " + e.getMessage());
        ut.rollback();
    }
    //disposes the session
    ksession1.dispose();
    //The only pending workItem related to the processId 2 should be 2
    //We can create queries to find out the pending workItems for a process 
    //instance or to find a process instance related to a business scenario 
    //using this approach.
    List<BusinessEntity> businessEntitiesByProcessId = getBusinessEntitiesProcessId(2L, em);
    assertEquals(1, businessEntitiesByProcessId.size());
    assertEquals(2, businessEntitiesByProcessId.get(0).getWorkItemId());
}
Also used : UserTransaction(javax.transaction.UserTransaction) HashMap(java.util.HashMap) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) BusinessEntity(com.salaboy.sessions.patterns.BusinessEntity) InitialContext(javax.naming.InitialContext) EntityManager(javax.persistence.EntityManager) Person(com.salaboy.model.Person) Test(org.junit.Test)

Aggregations

BusinessEntity (com.salaboy.sessions.patterns.BusinessEntity)7 Person (com.salaboy.model.Person)5 InitialContext (javax.naming.InitialContext)5 EntityManager (javax.persistence.EntityManager)5 UserTransaction (javax.transaction.UserTransaction)5 StatefulKnowledgeSession (org.drools.runtime.StatefulKnowledgeSession)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 Data (com.salaboy.model.Data)2