use of org.kie.api.event.process.DefaultProcessEventListener in project jbpm by kiegroup.
the class TimerUpdateTest method updateTimerBeforeDelayTest.
@Test(timeout = 30000)
public void updateTimerBeforeDelayTest() {
NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener(TIMER_NAME, 1);
// delay is set for 5s
setProcessScenario(TIMER_FILE);
kieSession.addEventListener(countDownListener);
final List<Long> list = new ArrayList<Long>();
kieSession.addEventListener(new DefaultProcessEventListener() {
@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
list.add(event.getProcessInstance().getId());
}
});
Assertions.assertThat(list).isEmpty();
long id = kieSession.startProcess(PROCESS_NAME).getId();
long startTime = System.currentTimeMillis();
Assertions.assertThat(list).isNotEmpty();
// set delay on time that passed -> expected that timer fired immediately
kieSession.execute(new UpdateTimerCommand(id, TIMER_NAME, -5));
countDownListener.waitTillCompleted();
Assertions.assertThat(timerHasFired()).isTrue();
long firedTime = timerFiredTime();
long timeDifference = Math.abs(firedTime - startTime);
logger.info("Start time: " + startTime + ", fired time: " + firedTime + ", difference: " + (firedTime - startTime));
Assertions.assertThat(timeDifference).isLessThan(500);
Assertions.assertThat(kieSession.getProcessInstance(id)).isNull();
}
use of org.kie.api.event.process.DefaultProcessEventListener in project jbpm by kiegroup.
the class EjbTimerServiceIntegrationTest method testProcessWithTimerOverEJBTimerService.
public void testProcessWithTimerOverEJBTimerService(String delay) throws InterruptedException {
cleanupSingletonSessionId();
final List<String> timerExecution = new ArrayList<String>();
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-TimerTask.bpmn2"), ResourceType.BPMN2).schedulerService(new EjbSchedulerService()).registerableItemsFactory(new DefaultRegisterableItemsFactory() {
@Override
public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
listeners.add(new DefaultProcessEventListener() {
@Override
public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
if (event.getNodeInstance().getNodeName().equals("Event")) {
timerExecution.add(event.getNodeInstance().getNodeName());
}
}
});
return listeners;
}
}).get();
manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
assertNotNull(manager);
RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("x", delay);
ProcessInstance instance = ksession.startProcess("IntermediateCatchEvent", parameters);
assertNotNull(instance);
Thread.sleep(3000);
instance = ksession.getProcessInstance(instance.getId());
assertNull(instance);
assertEquals(1, timerExecution.size());
manager.disposeRuntimeEngine(runtime);
// close manager which will close session maintained by the manager
manager.close();
}
use of org.kie.api.event.process.DefaultProcessEventListener in project jbpm by kiegroup.
the class ProcessTest method main.
public static final void main(String[] args) {
try {
// load up the knowledge base
final KieSession ksession = getKieSession();
UIWorkItemHandler handler = new UIWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
handler.setVisible(true);
ksession.getWorkItemManager().registerWorkItemHandler("Email", new WorkItemHandler() {
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
System.out.println("Sending email ...");
manager.completeWorkItem(workItem.getId(), null);
}
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
}
});
KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newFileLogger(ksession, "test");
// start a new process instance
Person person = new Person("john", "John Doe");
person.setAge(20);
Request request = new Request("12345");
request.setPersonId("john");
request.setAmount(1000L);
ksession.insert(person);
ksession.insert(request);
Map<String, Object> params = new HashMap<String, Object>();
params.put("request", request);
WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.startProcess("com.sample.requestHandling", params);
ksession.insert(processInstance);
ksession.fireAllRules();
// signaling to select tasks in the ad-hoc sub-process
ksession.signalEvent("Investigate", null, processInstance.getId());
// dynamically adding a new sub-process
DynamicNodeInstance dynamicNodeInstance = (DynamicNodeInstance) processInstance.getNodeInstances().iterator().next();
DynamicUtils.addDynamicSubProcess(dynamicNodeInstance, ksession, "com.sample.contactCustomer", null);
// event processing to detect too much processes being started
ksession.addEventListener(new DefaultProcessEventListener() {
public void beforeProcessStarted(ProcessStartedEvent event) {
ksession.insert(event);
}
});
Request request2 = new Request("12346");
request2.setPersonId("john");
request2.setAmount(1000L);
params = new HashMap<String, Object>();
params.put("request", request2);
ksession.startProcess("com.sample.requestHandling", params);
ksession.fireAllRules();
Request request3 = new Request("12347");
request3.setPersonId("john");
request3.setAmount(1000L);
params = new HashMap<String, Object>();
params.put("request", request);
ksession.startProcess("com.sample.requestHandling", params);
ksession.fireAllRules();
Request request4 = new Request("12348");
request4.setPersonId("john");
request4.setAmount(1000L);
params = new HashMap<String, Object>();
params.put("request", request4);
ksession.startProcess("com.sample.requestHandling", params);
ksession.fireAllRules();
Request request5 = new Request("12349");
request5.setPersonId("john");
request5.setAmount(1000L);
params = new HashMap<String, Object>();
params.put("request", request5);
ksession.startProcess("com.sample.requestHandling", params);
ksession.fireAllRules();
Request request6 = new Request("12350");
request6.setPersonId("john");
request6.setAmount(1000L);
params = new HashMap<String, Object>();
params.put("request", request6);
processInstance = (WorkflowProcessInstance) ksession.startProcess("com.sample.requestHandling", params);
FactHandle handle = ksession.insert(request6);
ksession.insert(processInstance);
ksession.fireAllRules();
// exception handling when canceling request
request6.setCanceled(true);
ksession.update(handle, request6);
ksession.fireAllRules();
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
use of org.kie.api.event.process.DefaultProcessEventListener in project jbpm by kiegroup.
the class IntermediateEventTest method testEventSubprocessSignalWithStateNode.
@Test
public void testEventSubprocessSignalWithStateNode() throws Exception {
KieBase kbase = createKnowledgeBase("BPMN2-EventSubprocessSignalWithStateNode.bpmn2");
final List<Long> executednodes = new ArrayList<Long>();
ProcessEventListener listener = new DefaultProcessEventListener() {
@Override
public void afterNodeLeft(ProcessNodeLeftEvent event) {
if (event.getNodeInstance().getNodeName().equals("User Task 2")) {
executednodes.add(event.getNodeInstance().getId());
}
}
};
ksession = createKnowledgeSession(kbase);
ksession.addEventListener(listener);
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
ProcessInstance processInstance = ksession.startProcess("BPMN2-EventSubprocessSignal");
assertProcessInstanceActive(processInstance);
ksession = restoreSession(ksession, true);
ksession.addEventListener(listener);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
WorkItem workItemTopProcess = workItemHandler.getWorkItem();
ksession.signalEvent("MySignal", null, processInstance.getId());
assertProcessInstanceActive(processInstance);
WorkItem workItem = workItemHandler.getWorkItem();
assertThat(workItem).isNotNull();
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
ksession.signalEvent("MySignal", null);
assertProcessInstanceActive(processInstance);
workItem = workItemHandler.getWorkItem();
assertThat(workItem).isNotNull();
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
ksession.signalEvent("MySignal", null);
assertProcessInstanceActive(processInstance);
workItem = workItemHandler.getWorkItem();
assertThat(workItem).isNotNull();
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
ksession.signalEvent("MySignal", null);
assertProcessInstanceActive(processInstance);
workItem = workItemHandler.getWorkItem();
assertThat(workItem).isNotNull();
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertThat(workItemTopProcess).isNotNull();
ksession.getWorkItemManager().completeWorkItem(workItemTopProcess.getId(), null);
assertProcessInstanceFinished(processInstance, ksession);
assertNodeTriggered(processInstance.getId(), "start", "User Task 1", "end", "Sub Process 1", "start-sub", "User Task 2", "end-sub");
assertThat(executednodes.size()).isEqualTo(4);
}
use of org.kie.api.event.process.DefaultProcessEventListener in project jbpm by kiegroup.
the class IntermediateEventTest method testEventSubprocessMessage.
@Test
public void testEventSubprocessMessage() throws Exception {
KieBase kbase = createKnowledgeBase("BPMN2-EventSubprocessMessage.bpmn2");
final List<Long> executednodes = new ArrayList<Long>();
ProcessEventListener listener = new DefaultProcessEventListener() {
@Override
public void afterNodeLeft(ProcessNodeLeftEvent event) {
if (event.getNodeInstance().getNodeName().equals("Script Task 1")) {
executednodes.add(event.getNodeInstance().getId());
}
}
};
ksession = createKnowledgeSession(kbase);
ksession.addEventListener(listener);
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
ProcessInstance processInstance = ksession.startProcess("BPMN2-EventSubprocessMessage");
assertProcessInstanceActive(processInstance);
ksession = restoreSession(ksession, true);
ksession.addEventListener(listener);
ksession.signalEvent("Message-HelloMessage", null, processInstance.getId());
ksession.signalEvent("Message-HelloMessage", null);
ksession.signalEvent("Message-HelloMessage", null);
ksession.signalEvent("Message-HelloMessage", null);
ksession.getProcessInstance(processInstance.getId());
ksession.getProcessInstance(processInstance.getId());
ksession.getProcessInstance(processInstance.getId());
ksession.getProcessInstance(processInstance.getId());
WorkItem workItem = workItemHandler.getWorkItem();
assertThat(workItem).isNotNull();
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertProcessInstanceFinished(processInstance, ksession);
assertNodeTriggered(processInstance.getId(), "start", "User Task 1", "end", "Sub Process 1", "start-sub", "Script Task 1", "end-sub");
assertThat(executednodes.size()).isEqualTo(4);
}
Aggregations