use of org.jbpm.workflow.instance.WorkflowRuntimeException in project jBPM5-Developer-Guide by Salaboy.
the class PersistentProcessTest method processInstancesAndLocalHTPlusFailTest.
@Test
public void processInstancesAndLocalHTPlusFailTest() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("process-async-interactions.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
System.out.println(">>> Error:" + error.getMessage());
}
fail(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
Environment env = EnvironmentFactory.newEnvironment();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.runtime.ht");
env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
// Let's create a Persistence Knowledge Session
System.out.println(" >>> Let's create a Persistent Knowledge Session");
final StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
int sessionId = ksession.getId();
assertNotNull(sessionId);
assertTrue(sessionId != 0);
// We need to register the WorkItems and Listeners that the session will use
TaskService client = createTaskService(emf);
LocalHTWorkItemHandler localHTWorkItemHandler = new LocalHTWorkItemHandler(client, ksession);
MockFaultWorkItemHandler mockFaultWorkItemHandler = new MockFaultWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", localHTWorkItemHandler);
ksession.getWorkItemManager().registerWorkItemHandler("External Service Call", mockFaultWorkItemHandler);
//KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
// Let's create a Process Instance
Person person = new Person("Salaboy", 29);
Map<String, Object> params = new HashMap<String, Object>();
params.put("person", person);
//Each Command will generate an interaction
System.out.println(">>> Let's Create Process Instance");
ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.AsyncInteractions", params);
System.out.println(">>> Let's Start the Process Instance");
UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
ut.begin();
ksession.startProcessInstance(processInstance.getId());
ut.commit();
System.out.println(" >>> Looking for Salaboy's Tasks");
List<TaskSummary> salaboysTasks = client.getTasksAssignedAsPotentialOwner("salaboy", "en-UK");
assertTrue(salaboysTasks.size() == 1);
TaskSummary salaboyTask = salaboysTasks.get(0);
System.out.println(" >>> Starting Salaboy's Task");
client.start(salaboyTask.getId(), "salaboy");
System.out.println(" >>> Completing Salaboy's Task");
ut.begin();
try {
client.complete(salaboyTask.getId(), "salaboy", null);
ut.commit();
} catch (WorkflowRuntimeException e) {
System.out.println("Rolling back transaction" + e);
ut.rollback();
}
System.out.println(" >>> Looking for Salaboy's Tasks");
Task salaboysTask = client.getTask(salaboyTask.getId());
assertNotNull(salaboysTask);
assertEquals("InProgress", salaboysTask.getTaskData().getStatus().name());
// We need to dispose the session, because the reference to this ksession object will no longer be valid
// because another thread could load the same session and execute a different command.
System.out.println(" >>> Disposing Session");
ksession.dispose();
}
use of org.jbpm.workflow.instance.WorkflowRuntimeException in project jBPM5-Developer-Guide by Salaboy.
the class MultiProcessEvaluationTest method testMultiProcessEvaluation.
@Test
public void testMultiProcessEvaluation() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("multi_process_evaluation/resources.drl"), ResourceType.DRL);
kbuilder.add(new ClassPathResource("multi_process_evaluation/multi-process-decision.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
System.out.println(">>> Error:" + error.getMessage());
}
fail(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
// Uncomment to see all the logs
KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
try {
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void activationCreated(ActivationCreatedEvent event) {
System.out.println(">>> Firing All the Rules on Activation Created " + event);
((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
}
});
ksession.addEventListener(new DefaultProcessEventListener() {
@Override
public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
System.out.println(">>> Firing All the Rules before node triggered! " + event);
((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
}
@Override
public void beforeNodeLeft(ProcessNodeLeftEvent event) {
System.out.println(">>> Firing All the Rules before node triggered! " + event);
((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
}
});
Map<String, Object> params = new HashMap<String, Object>();
ksession.insert(new Resources(50));
for (int i = 1; i < 16; i++) {
ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.SimpleDecision", params);
ksession.insert(processInstance);
assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
System.out.println(" ----------------- ##### -----------------");
System.out.println(" ----- Starting Process Number : " + i + " ----");
System.out.println(" ----------------- ##### -----------------");
ksession.startProcessInstance(processInstance.getId());
assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
System.out.println(" ----------------- ##### -----------------");
System.out.println(" ----- Process Number : " + i + " Completed ----");
System.out.println(" ----------------- ##### -----------------");
}
} catch (WorkflowRuntimeException e) {
assertEquals(true, e.getCause().getMessage().contains("No More Resources Available = "));
}
}
use of org.jbpm.workflow.instance.WorkflowRuntimeException in project jBPM5-Developer-Guide by Salaboy.
the class PersistentProcessTest method processInstancesPersistenceFaultTest.
@Test
public void processInstancesPersistenceFaultTest() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("process-async-interactions.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
System.out.println(">>> Error:" + error.getMessage());
}
fail(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
Environment env = EnvironmentFactory.newEnvironment();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.runtime");
env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
// Let's create a Persistence Knowledge Session
System.out.println(" >>> Let's create a Persistent Knowledge Session");
final StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
int sessionId = ksession.getId();
assertNotNull(sessionId);
assertTrue(sessionId != 0);
// We need to register the WorkItems and Listeners that the session will use
MockHTWorkItemHandler mockHTWorkItemHandler = new MockHTWorkItemHandler();
MockFaultWorkItemHandler mockFaultWorkItemHandler = new MockFaultWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", mockHTWorkItemHandler);
ksession.getWorkItemManager().registerWorkItemHandler("External Service Call", mockFaultWorkItemHandler);
//KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
// Let's create a Process Instance
Person person = new Person("Salaboy", 29);
Map<String, Object> params = new HashMap<String, Object>();
params.put("person", person);
//Each Command will generate an interaction
System.out.println(">>> Let's Create Process Instance");
ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.AsyncInteractions", params);
System.out.println(">>> Let's Start the Process Instance");
try {
ksession.startProcessInstance(processInstance.getId());
} catch (Exception e) {
assertTrue(e instanceof WorkflowRuntimeException);
System.out.println(">>> Disposing Session");
// We need to dispose the session, because the reference to this ksession object will no longer be valid
// because another thread could load the same session and execute a different command.
ksession.dispose();
// The startProcess transaction never gets commited.
EntityManager em = emf.createEntityManager();
// The ProcessInstance is in the same state as when it was created
List resultList = em.createQuery("select p from ProcessInstanceInfo p").getResultList();
assertEquals(1, resultList.size());
assertEquals(0, ((WorkflowProcessInstanceImpl) processInstance).getNodeInstances().size());
// No WorkItemInfo was commited.
resultList = em.createQuery("select w from WorkItemInfo w").getResultList();
assertEquals(0, resultList.size());
}
}
use of org.jbpm.workflow.instance.WorkflowRuntimeException in project jBPM5-Developer-Guide by Salaboy.
the class PersistentProcessTest method processInstancesPersistenceFaultRecoveryTest.
@Test
public void processInstancesPersistenceFaultRecoveryTest() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("process-async-interactions.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
System.out.println(">>> Error:" + error.getMessage());
}
fail(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
Environment env = EnvironmentFactory.newEnvironment();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.runtime");
env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
// Let's create a Persistence Knowledge Session
System.out.println(" >>> Let's create a Persistent Knowledge Session");
final StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
int sessionId = ksession.getId();
assertNotNull(sessionId);
assertTrue(sessionId != 0);
// We need to register the WorkItems and Listeners that the session will use
MockAsyncHTWorkItemHandler mockAsyncHTWorkItemHandler = new MockAsyncHTWorkItemHandler();
MockFaultWorkItemHandler mockFaultWorkItemHandler = new MockFaultWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", mockAsyncHTWorkItemHandler);
ksession.getWorkItemManager().registerWorkItemHandler("External Service Call", mockFaultWorkItemHandler);
//KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
// Let's create a Process Instance
Person person = new Person("Salaboy", 29);
Map<String, Object> params = new HashMap<String, Object>();
params.put("person", person);
//Each Command will generate an interaction
System.out.println(">>> Let's Create Process Instance");
ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.AsyncInteractions", params);
System.out.println(">>> Let's Start the Process Instance");
try {
ksession.startProcessInstance(processInstance.getId());
System.out.println(">>> Completing the first Human Interaction");
ksession.getWorkItemManager().completeWorkItem(mockAsyncHTWorkItemHandler.getId(), null);
} catch (Exception e) {
assertTrue(e instanceof WorkflowRuntimeException);
System.out.println(">>> Disposing Session");
// We need to dispose the session, because the reference to this ksession object will no longer be valid
// because another thread could load the same session and execute a different command.
ksession.dispose();
// The startProcess transaction never gets commited.
EntityManager em = emf.createEntityManager();
// The ProcessInstance is in the same state as when it was created
List resultList = em.createQuery("select p from ProcessInstanceInfo p").getResultList();
assertEquals(1, resultList.size());
assertEquals(0, ((WorkflowProcessInstanceImpl) processInstance).getNodeInstances().size());
// No WorkItemInfo was commited.
resultList = em.createQuery("select w from WorkItemInfo w").getResultList();
assertEquals(1, resultList.size());
}
}
Aggregations