use of org.kie.api.event.process.ProcessStartedEvent in project jbpm by kiegroup.
the class PersistentStatefulSessionTest method testProcessListener.
@Test
public void testProcessListener() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("WorkItemsProcess.rf"), ResourceType.DRF);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
final List<ProcessEvent> events = new ArrayList<ProcessEvent>();
ProcessEventListener listener = new ProcessEventListener() {
public void afterNodeLeft(ProcessNodeLeftEvent event) {
logger.debug("After node left: {}", event.getNodeInstance().getNodeName());
events.add(event);
}
public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
logger.debug("After node triggered: {}", event.getNodeInstance().getNodeName());
events.add(event);
}
public void afterProcessCompleted(ProcessCompletedEvent event) {
logger.debug("After process completed");
events.add(event);
}
public void afterProcessStarted(ProcessStartedEvent event) {
logger.debug("After process started");
events.add(event);
}
public void beforeNodeLeft(ProcessNodeLeftEvent event) {
logger.debug("Before node left: {}", event.getNodeInstance().getNodeName());
events.add(event);
}
public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
logger.debug("Before node triggered: {}", event.getNodeInstance().getNodeName());
events.add(event);
}
public void beforeProcessCompleted(ProcessCompletedEvent event) {
logger.debug("Before process completed");
events.add(event);
}
public void beforeProcessStarted(ProcessStartedEvent event) {
logger.debug("Before process started");
events.add(event);
}
public void afterVariableChanged(ProcessVariableChangedEvent event) {
logger.debug("After Variable Changed");
events.add(event);
}
public void beforeVariableChanged(ProcessVariableChangedEvent event) {
logger.debug("Before Variable Changed");
events.add(event);
}
};
ksession.addEventListener(listener);
ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
logger.debug("Started process instance {}", processInstance.getId());
assertEquals(12, events.size());
assertTrue(events.get(0) instanceof ProcessStartedEvent);
assertTrue(events.get(1) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(2) instanceof ProcessNodeLeftEvent);
assertTrue(events.get(3) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(4) instanceof ProcessNodeLeftEvent);
assertTrue(events.get(5) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(6) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(7) instanceof ProcessNodeLeftEvent);
assertTrue(events.get(8) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(9) instanceof ProcessNodeLeftEvent);
assertTrue(events.get(10) instanceof ProcessNodeTriggeredEvent);
assertTrue(events.get(11) instanceof ProcessStartedEvent);
ksession.removeEventListener(listener);
events.clear();
processInstance = ksession.startProcess("org.drools.test.TestProcess");
logger.debug("Started process instance {}", processInstance.getId());
assertTrue(events.isEmpty());
}
use of org.kie.api.event.process.ProcessStartedEvent in project jbpm by kiegroup.
the class GlobalQuartzDBTimerServiceTest method testTimerStartManagerClose.
@Test(timeout = 20000)
public void testTimerStartManagerClose() throws Exception {
NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("StartProcess", 3);
QuartzSchedulerService additionalCopy = new QuartzSchedulerService();
additionalCopy.initScheduler(null);
// prepare listener to assert results
final List<Long> timerExporations = new ArrayList<Long>();
ProcessEventListener listener = new DefaultProcessEventListener() {
@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
timerExporations.add(event.getProcessInstance().getId());
}
};
environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/TimerStart2.bpmn2"), ResourceType.BPMN2).schedulerService(globalScheduler).registerableItemsFactory(new TestRegisterableItemsFactory(listener, countDownListener)).get();
manager = getManager(environment, false);
RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
KieSession ksession = runtime.getKieSession();
assertEquals(0, timerExporations.size());
countDownListener.waitTillCompleted();
manager.disposeRuntimeEngine(runtime);
int atDispose = timerExporations.size();
assertTrue(atDispose > 0);
((AbstractRuntimeManager) manager).close(true);
countDownListener.reset(1);
countDownListener.waitTillCompleted(3000);
assertEquals(atDispose, timerExporations.size());
additionalCopy.shutdown();
}
use of org.kie.api.event.process.ProcessStartedEvent in project jbpm by kiegroup.
the class GlobalTimerServiceBaseTest method testTimerStart.
@Test(timeout = 20000)
public void testTimerStart() throws Exception {
NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("StartProcess", 5);
// prepare listener to assert results
final List<Long> timerExporations = new ArrayList<Long>();
ProcessEventListener listener = new DefaultProcessEventListener() {
@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
timerExporations.add(event.getProcessInstance().getId());
}
};
environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/TimerStart2.bpmn2"), ResourceType.BPMN2).schedulerService(globalScheduler).registerableItemsFactory(new TestRegisterableItemsFactory(listener, countDownListener)).get();
manager = getManager(environment, false);
RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
KieSession ksession = runtime.getKieSession();
assertEquals(0, timerExporations.size());
countDownListener.waitTillCompleted();
manager.disposeRuntimeEngine(runtime);
assertEquals(5, timerExporations.size());
}
use of org.kie.api.event.process.ProcessStartedEvent in project jbpm by kiegroup.
the class TimerUpdateTest method updateTimerShortherDelayTest.
@Test(timeout = 30000)
public void updateTimerShortherDelayTest() {
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 to 3s
kieSession.execute(new UpdateTimerCommand(id, TIMER_NAME, 3));
countDownListener.waitTillCompleted();
Assertions.assertThat(timerHasFired()).isTrue();
long firedTime = timerFiredTime();
long timeDifference = Math.abs(firedTime - startTime - 3000);
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.ProcessStartedEvent in project jbpm by kiegroup.
the class TimerUpdateTest method updateBoundaryTimerTest.
@Test(timeout = 30000)
public void updateBoundaryTimerTest() {
NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener(BOUNDARY_TIMER_NAME, 1);
// timer is set for long duration (100s)
setProcessScenario(BOUNDARY_TIMER_FILE);
final List<Long> list = new ArrayList<Long>();
kieSession.addEventListener(new DefaultProcessEventListener() {
@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
list.add(event.getProcessInstance().getId());
}
});
kieSession.addEventListener(countDownListener);
Assertions.assertThat(list).isEmpty();
long id = kieSession.startProcess(BOUNDARY_PROCESS_NAME).getId();
long startTime = System.currentTimeMillis();
Assertions.assertThat(list).isNotEmpty();
// set timer delay to 3s
kieSession.execute(new UpdateTimerCommand(id, BOUNDARY_TIMER_ATTACHED_TO_NAME, 3));
countDownListener.waitTillCompleted();
Assertions.assertThat(timerHasFired()).isTrue();
long firedTime = timerFiredTime();
long timeDifference = Math.abs(firedTime - startTime - 3000);
logger.info("Start time: " + startTime + ", fired time: " + firedTime + ", difference: " + (firedTime - startTime));
Assertions.assertThat(timeDifference).isLessThan(1000);
Assertions.assertThat(kieSession.getProcessInstance(id)).isNull();
}
Aggregations