use of com.salaboy.model.Person in project jBPM5-Developer-Guide by Salaboy.
the class ProcessAndEventIntegrationTest method processEventsWithListenerTest.
@Test
public void processEventsWithListenerTest() throws InterruptedException {
// Let's create a kbuilder, kbase and ksession for running the process
final StatefulKnowledgeSession processKsession = createProcessSession();
// Let's create a kbuilder, kbase and ksession for analyzing the process events
final StatefulKnowledgeSession eventsKsession = createEventsSession();
final TaskSpeed taskSpeed = new TaskSpeed(1000L);
eventsKsession.setGlobal("taskSpeed", taskSpeed);
processKsession.getWorkItemManager().registerWorkItemHandler("Human Task", new WorkItemHandler() {
public void executeWorkItem(WorkItem wi, WorkItemManager wim) {
try {
System.out.println(" >>> Working on Task! it will take: " + taskSpeed.getAmount() / 1000 + " seconds.");
Thread.sleep(taskSpeed.getAmount());
} catch (InterruptedException ex) {
Logger.getLogger(ProcessAndEventIntegrationTest.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(" >>> Completing Task! -> " + wi.getName() + " - id: " + wi.getId());
wim.completeWorkItem(wi.getId(), null);
}
public void abortWorkItem(WorkItem wi, WorkItemManager wim) {
// do nothing
}
});
processKsession.addEventListener(new DefaultProcessEventListener() {
@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
System.out.println(" >>> Before Process Started: " + event.getProcessInstance().getProcessName());
eventsKsession.insert(event);
}
@Override
public void afterProcessCompleted(ProcessCompletedEvent event) {
System.out.println(" >>> After Process Completed: " + event.getProcessInstance().getProcessName());
eventsKsession.insert(event);
}
@Override
public void beforeNodeLeft(org.drools.event.process.ProcessNodeLeftEvent event) {
System.out.println(" >>> Before Node Left: " + event.getNodeInstance().getNodeName());
eventsKsession.insert(event);
}
});
for (int i = 0; i < 20; i++) {
final int id = i;
Person person = new Person("Salaboy-" + id, id);
Map<String, Object> params = new HashMap<String, Object>();
params.put("person", person);
ProcessInstance processInstance = processKsession.createProcessInstance("com.salaboy.process.SimpleProcess", params);
assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
FactHandle processtHandle = processKsession.insert(processInstance);
System.out.println("Starting Process Instance: " + processInstance.getId());
processKsession.startProcessInstance(processInstance.getId());
assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
processKsession.retract(processtHandle);
}
}
use of com.salaboy.model.Person in project jBPM5-Developer-Guide by Salaboy.
the class ProcessAndEventMultiThreadIntegrationTest method processEventsWithListenerTest.
@Test
public void processEventsWithListenerTest() throws InterruptedException {
// Let's create a kbuilder, kbase and ksession for running the process
final StatefulKnowledgeSession processKsession = createProcessSession();
// Let's create a kbuilder, kbase and ksession for analyzing the process events
final StatefulKnowledgeSession eventsKsession = createEventsSession();
final TaskSpeed taskSpeed = new TaskSpeed(1000L);
eventsKsession.setGlobal("taskSpeed", taskSpeed);
processKsession.getWorkItemManager().registerWorkItemHandler("Human Task", new WorkItemHandler() {
public void executeWorkItem(WorkItem wi, WorkItemManager wim) {
try {
System.out.println(" >>> Working on Task! it will take: " + taskSpeed.getAmount() / 1000 + " seconds.");
Thread.sleep(taskSpeed.getAmount());
} catch (InterruptedException ex) {
Logger.getLogger(ProcessAndEventMultiThreadIntegrationTest.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(" >>> Completing Task! -> " + wi.getName() + " - id: " + wi.getId());
wim.completeWorkItem(wi.getId(), null);
}
public void abortWorkItem(WorkItem wi, WorkItemManager wim) {
// do nothing
}
});
((StatefulKnowledgeSessionImpl) processKsession).addEventListener(new DefaultProcessEventListener() {
@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
System.out.println(" >>> Before Process Started: " + event.getProcessInstance().getProcessName());
eventsKsession.insert(event);
}
@Override
public void afterProcessCompleted(ProcessCompletedEvent event) {
System.out.println(" >>> After Process Completed: " + event.getProcessInstance().getProcessName());
eventsKsession.insert(event);
}
@Override
public void beforeNodeLeft(org.drools.event.process.ProcessNodeLeftEvent event) {
System.out.println(" >>> Before Node Left: " + event.getNodeInstance().getNodeName());
eventsKsession.insert(event);
}
});
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
final int id1 = i;
final int id2 = j;
new Thread(new Runnable() {
public void run() {
Person person = new Person("Salaboy-" + id1 + "_" + id2, id2);
Map<String, Object> params = new HashMap<String, Object>();
params.put("person", person);
ProcessInstance processInstance = processKsession.createProcessInstance("com.salaboy.process.SimpleProcess", params);
assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
FactHandle processtHandle = processKsession.insert(processInstance);
System.out.println("Starting Process Instance: " + processInstance.getId());
processKsession.startProcessInstance(processInstance.getId());
assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
processKsession.retract(processtHandle);
}
}).start();
}
Thread.sleep(3000);
}
}
use of com.salaboy.model.Person 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());
}
Aggregations