Search in sources :

Example 31 with ProcessNodeLeftEvent

use of org.kie.api.event.process.ProcessNodeLeftEvent in project jbpm by kiegroup.

the class PerCaseRuntimeManagerTest method testTimerOnPerCaseManager.

@Test(timeout = 10000)
public void testTimerOnPerCaseManager() throws Exception {
    final Set<Long> ksessionUsed = new HashSet<Long>();
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 3);
    final List<Long> timerExpirations = new ArrayList<Long>();
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(new DefaultProcessEventListener() {

                @Override
                public void afterNodeLeft(ProcessNodeLeftEvent event) {
                    if (event.getNodeInstance().getNodeName().equals("timer")) {
                        timerExpirations.add(event.getProcessInstance().getId());
                        ksessionUsed.add(((KieSession) event.getKieRuntime()).getIdentifier());
                    }
                }
            });
            listeners.add(new DefaultProcessEventListener() {

                @Override
                public void beforeProcessStarted(ProcessStartedEvent event) {
                    ksessionUsed.add(((KieSession) event.getKieRuntime()).getIdentifier());
                }
            });
            listeners.add(countDownListener);
            return listeners;
        }
    }).addAsset(ResourceFactory.newClassPathResource("BPMN2-IntermediateCatchEventTimerCycle3.bpmn2"), ResourceType.BPMN2).get();
    manager = RuntimeManagerFactory.Factory.get().newPerCaseRuntimeManager(environment);
    assertNotNull(manager);
    // ksession for process instance #1
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime = manager.getRuntimeEngine(CaseContext.get("Case-1"));
    KieSession ksession = runtime.getKieSession();
    long ksession1Id = ksession.getIdentifier();
    ProcessInstance pi1 = ksession.startProcess("IntermediateCatchEvent");
    // both processes started
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    manager.disposeRuntimeEngine(runtime);
    // wait a bit for some timers to fire
    countDownListener.waitTillCompleted();
    runtime = manager.getRuntimeEngine(CaseContext.get("Case-1"));
    ksession = runtime.getKieSession();
    ksession.abortProcessInstance(pi1.getId());
    manager.disposeRuntimeEngine(runtime);
    manager.close();
    // there should be only one ksession used
    assertEquals(1, ksessionUsed.size());
    assertEquals(ksession1Id, ksessionUsed.iterator().next().longValue());
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 32 with ProcessNodeLeftEvent

use of org.kie.api.event.process.ProcessNodeLeftEvent in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManagerTest method testRestoreTimersAfterManagerClose.

@Test(timeout = 30000)
public void testRestoreTimersAfterManagerClose() throws Exception {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 2);
    final List<Long> timerExpirations = new ArrayList<Long>();
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(new DefaultProcessEventListener() {

                @Override
                public void afterNodeLeft(ProcessNodeLeftEvent event) {
                    if (event.getNodeInstance().getNodeName().equals("timer")) {
                        timerExpirations.add(event.getProcessInstance().getId());
                    }
                }
            });
            listeners.add(countDownListener);
            return listeners;
        }
    }).addAsset(ResourceFactory.newClassPathResource("BPMN2-IntermediateCatchEventTimerCycle3.bpmn2"), ResourceType.BPMN2).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    // ksession for process instance #1
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    ProcessInstance pi1 = ksession.startProcess("IntermediateCatchEvent");
    // both processes started
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    manager.disposeRuntimeEngine(runtime);
    // wait a bit for some timers to fire
    countDownListener.waitTillCompleted();
    ((AbstractRuntimeManager) manager).close(true);
    int currentNumberOfTriggers = timerExpirations.size();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    countDownListener.reset(2);
    countDownListener.waitTillCompleted();
    runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi1.getId()));
    runtime.getKieSession().abortProcessInstance(pi1.getId());
    manager.disposeRuntimeEngine(runtime);
    manager.close();
    assertTrue(timerExpirations.size() > currentNumberOfTriggers);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) ArrayList(java.util.ArrayList) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Aggregations

ProcessNodeLeftEvent (org.kie.api.event.process.ProcessNodeLeftEvent)32 ArrayList (java.util.ArrayList)29 Test (org.junit.Test)29 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)27 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)25 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)24 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)20 KieSession (org.kie.api.runtime.KieSession)15 KieBase (org.kie.api.KieBase)12 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)12 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)8 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)8 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)8 ProcessStartedEvent (org.kie.api.event.process.ProcessStartedEvent)8 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)7 WorkItem (org.kie.api.runtime.process.WorkItem)7 ProcessCompletedEvent (org.kie.api.event.process.ProcessCompletedEvent)6 ProcessNodeTriggeredEvent (org.kie.api.event.process.ProcessNodeTriggeredEvent)6 ProcessVariableChangedEvent (org.kie.api.event.process.ProcessVariableChangedEvent)6 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5