Search in sources :

Example 91 with DefaultProcessEventListener

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

the class PerCaseRuntimeManagerTest method testExecuteReusableSubprocess.

@Test
public void testExecuteReusableSubprocess() {
    final Set<Long> ksessionUsed = new HashSet<Long>();
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-CallActivity.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-CallActivitySubProcess.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

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

                @Override
                public void beforeProcessStarted(ProcessStartedEvent event) {
                    ksessionUsed.add(((KieSession) event.getKieRuntime()).getIdentifier());
                }
            });
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newPerCaseRuntimeManager(environment);
    assertNotNull(manager);
    // 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();
    assertNotNull(ksession);
    long ksession1Id = ksession.getIdentifier();
    assertTrue(ksession1Id == 2);
    ProcessInstance pi1 = ksession.startProcess("ParentProcess");
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    manager.disposeRuntimeEngine(runtime);
    runtime = manager.getRuntimeEngine(CaseContext.get("Case-1"));
    ksession = runtime.getKieSession();
    ksession.getWorkItemManager().completeWorkItem(1, null);
    AuditService logService = runtime.getAuditService();
    List<? extends ProcessInstanceLog> logs = logService.findActiveProcessInstances("ParentProcess");
    assertNotNull(logs);
    assertEquals(0, logs.size());
    logs = logService.findActiveProcessInstances("SubProcess");
    assertNotNull(logs);
    assertEquals(0, logs.size());
    logs = logService.findProcessInstances("ParentProcess");
    assertNotNull(logs);
    assertEquals(1, logs.size());
    String externalId = logs.get(0).getExternalId();
    assertEquals(manager.getIdentifier(), externalId);
    logs = logService.findProcessInstances("SubProcess");
    assertNotNull(logs);
    assertEquals(1, logs.size());
    externalId = logs.get(0).getExternalId();
    assertEquals(manager.getIdentifier(), externalId);
    assertEquals(1, ksessionUsed.size());
    assertEquals(ksession1Id, ksessionUsed.iterator().next().longValue());
    manager.disposeRuntimeEngine(runtime);
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AuditService(org.kie.api.runtime.manager.audit.AuditService) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 92 with DefaultProcessEventListener

use of org.kie.api.event.process.DefaultProcessEventListener 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 93 with DefaultProcessEventListener

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

the class PerProcessInstanceRuntimeManagerTest method testEventSignalingBetweenProcesses.

@Test
public void testEventSignalingBetweenProcesses() {
    final Map<String, Integer> processStates = new HashMap<String, Integer>();
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder().persistence(false).userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("events/throw-an-event.bpmn"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("events/start-on-event.bpmn"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

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

                @Override
                public void afterProcessCompleted(ProcessCompletedEvent event) {
                    processStates.put(event.getProcessInstance().getProcessId(), event.getProcessInstance().getState());
                }

                @Override
                public void beforeProcessStarted(ProcessStartedEvent event) {
                    processStates.put(event.getProcessInstance().getProcessId(), event.getProcessInstance().getState());
                }
            });
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    ksession.startProcess("com.sample.bpmn.hello");
    assertEquals(2, processStates.size());
    assertTrue(processStates.containsKey("com.sample.bpmn.hello"));
    assertTrue(processStates.containsKey("com.sample.bpmn.Second"));
    assertEquals(ProcessInstance.STATE_COMPLETED, processStates.get("com.sample.bpmn.hello").intValue());
    assertEquals(ProcessInstance.STATE_COMPLETED, processStates.get("com.sample.bpmn.Second").intValue());
    manager.disposeRuntimeEngine(runtime);
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) ProcessCompletedEvent(org.kie.api.event.process.ProcessCompletedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 94 with DefaultProcessEventListener

use of org.kie.api.event.process.DefaultProcessEventListener 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)

Example 95 with DefaultProcessEventListener

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

the class PerRequestRuntimeManagerTest method testEventSignalingBetweenProcesses.

@Test
public void testEventSignalingBetweenProcesses() {
    final Map<String, Integer> processStates = new HashMap<String, Integer>();
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder().persistence(false).userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("events/throw-an-event.bpmn"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("events/start-on-event.bpmn"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

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

                @Override
                public void afterProcessCompleted(ProcessCompletedEvent event) {
                    processStates.put(event.getProcessInstance().getProcessId(), event.getProcessInstance().getState());
                }

                @Override
                public void beforeProcessStarted(ProcessStartedEvent event) {
                    processStates.put(event.getProcessInstance().getProcessId(), event.getProcessInstance().getState());
                }
            });
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newPerRequestRuntimeManager(environment);
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    ksession.startProcess("com.sample.bpmn.hello");
    assertEquals(2, processStates.size());
    assertTrue(processStates.containsKey("com.sample.bpmn.hello"));
    assertTrue(processStates.containsKey("com.sample.bpmn.Second"));
    assertEquals(ProcessInstance.STATE_COMPLETED, processStates.get("com.sample.bpmn.hello").intValue());
    assertEquals(ProcessInstance.STATE_COMPLETED, processStates.get("com.sample.bpmn.Second").intValue());
    manager.disposeRuntimeEngine(runtime);
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) ProcessCompletedEvent(org.kie.api.event.process.ProcessCompletedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)98 Test (org.junit.Test)87 ArrayList (java.util.ArrayList)69 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)59 KieBase (org.kie.api.KieBase)57 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)48 ProcessStartedEvent (org.kie.api.event.process.ProcessStartedEvent)48 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)40 KieSession (org.kie.api.runtime.KieSession)38 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)30 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)27 WorkItem (org.kie.api.runtime.process.WorkItem)27 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)26 ProcessNodeLeftEvent (org.kie.api.event.process.ProcessNodeLeftEvent)25 HashMap (java.util.HashMap)24 ProcessNodeTriggeredEvent (org.kie.api.event.process.ProcessNodeTriggeredEvent)19 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)16 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)11 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)9 ProcessCompletedEvent (org.kie.api.event.process.ProcessCompletedEvent)7