Search in sources :

Example 36 with ProcessEventListener

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

the class GlobalTimerServiceBaseTest method testInterediateTimerWithHTBeforeWithGlobalTestService.

@Test(timeout = 20000)
public void testInterediateTimerWithHTBeforeWithGlobalTestService() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 3);
    // prepare listener to assert results
    final List<Long> timerExporations = new ArrayList<Long>();
    ProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            if (event.getNodeInstance().getNodeName().equals("timer")) {
                timerExporations.add(event.getProcessInstance().getId());
            }
        }
    };
    Properties properties = new Properties();
    properties.setProperty("mary", "HR");
    properties.setProperty("john", "HR");
    UserGroupCallback userGroupCallback = new JBossUserGroupCallbackImpl(properties);
    environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycleWithHT2.bpmn2"), ResourceType.BPMN2).schedulerService(globalScheduler).registerableItemsFactory(new TestRegisterableItemsFactory(listener, countDownListener)).userGroupCallback(userGroupCallback).get();
    manager = getManager(environment, true);
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "R3/PT1S");
    ProcessInstance processInstance = ksession.startProcess("IntermediateCatchEvent", params);
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    // get tasks
    List<Status> statuses = new ArrayList<Status>();
    statuses.add(Status.Reserved);
    List<TaskSummary> tasks = runtime.getTaskService().getTasksAssignedAsPotentialOwnerByStatus("john", statuses, "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    for (TaskSummary task : tasks) {
        runtime.getTaskService().start(task.getId(), "john");
        runtime.getTaskService().complete(task.getId(), "john", null);
    }
    // dispose session to force session to be reloaded on timer expiration
    manager.disposeRuntimeEngine(runtime);
    // now wait for 1 second for first timer to trigger
    countDownListener.waitTillCompleted();
    countDownListener.reset(1);
    try {
        runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtime.getKieSession();
        processInstance = ksession.getProcessInstance(processInstance.getId());
        assertNull(processInstance);
    } catch (SessionNotFoundException e) {
    // expected for PerProcessInstanceManagers since process instance is completed
    }
    // let's wait to ensure no more timers are expired and triggered
    countDownListener.waitTillCompleted(3000);
    manager.disposeRuntimeEngine(runtime);
    assertEquals(3, timerExporations.size());
}
Also used : Status(org.kie.api.task.model.Status) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) JBossUserGroupCallbackImpl(org.jbpm.services.task.identity.JBossUserGroupCallbackImpl) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ArrayList(java.util.ArrayList) Properties(java.util.Properties) UserGroupCallback(org.kie.api.task.UserGroupCallback) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) TaskSummary(org.kie.api.task.model.TaskSummary) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException) Test(org.junit.Test)

Example 37 with ProcessEventListener

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

the class GlobalTimerServiceVolumeTest method setup.

@Before
public void setup() {
    Properties properties = new Properties();
    properties.setProperty("mary", "HR");
    properties.setProperty("john", "HR");
    userGroupCallback = new JBossUserGroupCallbackImpl(properties);
    System.setProperty("org.quartz.properties", "quartz-db.properties");
    testCreateQuartzSchema();
    globalScheduler = new QuartzSchedulerService();
    emf = Persistence.createEntityManagerFactory("org.jbpm.test.persistence");
    countDownListener = new NodeLeftCountDownProcessEventListener("timer", numberOfProcesses);
    // prepare listener to assert results
    final List<Long> timerExporations = new ArrayList<Long>();
    ProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            if (event.getNodeInstance().getNodeName().equals("timer")) {
                timerExporations.add(event.getProcessInstance().getId());
            }
        }
    };
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycleWithHT3.bpmn2"), ResourceType.BPMN2).schedulerService(globalScheduler).registerableItemsFactory(new TestRegisterableItemsFactory(listener, countDownListener)).get();
    if ("processinstance".equals(strategy)) {
        manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment, "first");
    } else if ("request".equals(strategy)) {
        manager = RuntimeManagerFactory.Factory.get().newPerRequestRuntimeManager(environment, "first");
    } else if ("case".equals(strategy)) {
        manager = RuntimeManagerFactory.Factory.get().newPerCaseRuntimeManager(environment, "first");
    } else {
        throw new RuntimeException("Unknow type of runtime strategy");
    }
}
Also used : RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) QuartzSchedulerService(org.jbpm.process.core.timer.impl.QuartzSchedulerService) JBossUserGroupCallbackImpl(org.jbpm.services.task.identity.JBossUserGroupCallbackImpl) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ArrayList(java.util.ArrayList) Properties(java.util.Properties) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) Before(org.junit.Before)

Example 38 with ProcessEventListener

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

the class JbpmJUnitBaseTestCase method createRuntimeManager.

/**
 * Creates default configuration of <code>RuntimeManager</code> with given <code>strategy</code> and all
 * <code>resources</code> being added to knowledge base.
 * <br/>
 * There should be only one <code>RuntimeManager</code> created during single test.
 * @param strategy - selected strategy of those that are supported
 * @param resources - resources that shall be added to knowledge base
 * @param identifier - identifies the runtime manager
 * @return new instance of RuntimeManager
 */
protected RuntimeManager createRuntimeManager(Strategy strategy, Map<String, ResourceType> resources, String identifier) {
    if (manager != null) {
        throw new IllegalStateException("There is already one RuntimeManager active");
    }
    RuntimeEnvironmentBuilder builder = null;
    if (!setupDataSource) {
        builder = RuntimeEnvironmentBuilder.Factory.get().newEmptyBuilder().addConfiguration("drools.processSignalManagerFactory", DefaultSignalManagerFactory.class.getName()).addConfiguration("drools.processInstanceManagerFactory", DefaultProcessInstanceManagerFactory.class.getName()).registerableItemsFactory(new SimpleRegisterableItemsFactory() {

            @Override
            public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
                Map<String, WorkItemHandler> handlers = new HashMap<String, WorkItemHandler>();
                handlers.putAll(super.getWorkItemHandlers(runtime));
                handlers.putAll(customHandlers);
                return handlers;
            }

            @Override
            public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
                List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
                listeners.addAll(customProcessListeners);
                return listeners;
            }

            @Override
            public List<AgendaEventListener> getAgendaEventListeners(RuntimeEngine runtime) {
                List<AgendaEventListener> listeners = super.getAgendaEventListeners(runtime);
                listeners.addAll(customAgendaListeners);
                return listeners;
            }

            @Override
            public List<TaskLifeCycleEventListener> getTaskListeners() {
                List<TaskLifeCycleEventListener> listeners = super.getTaskListeners();
                listeners.addAll(customTaskListeners);
                return listeners;
            }
        });
    } else if (sessionPersistence) {
        builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

            @Override
            public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
                Map<String, WorkItemHandler> handlers = new HashMap<String, WorkItemHandler>();
                handlers.putAll(super.getWorkItemHandlers(runtime));
                handlers.putAll(customHandlers);
                return handlers;
            }

            @Override
            public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
                List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
                listeners.addAll(customProcessListeners);
                return listeners;
            }

            @Override
            public List<AgendaEventListener> getAgendaEventListeners(RuntimeEngine runtime) {
                List<AgendaEventListener> listeners = super.getAgendaEventListeners(runtime);
                listeners.addAll(customAgendaListeners);
                return listeners;
            }

            @Override
            public List<TaskLifeCycleEventListener> getTaskListeners() {
                List<TaskLifeCycleEventListener> listeners = super.getTaskListeners();
                listeners.addAll(customTaskListeners);
                return listeners;
            }
        });
    } else {
        builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder().entityManagerFactory(emf).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

            @Override
            public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
                Map<String, WorkItemHandler> handlers = new HashMap<String, WorkItemHandler>();
                handlers.putAll(super.getWorkItemHandlers(runtime));
                handlers.putAll(customHandlers);
                return handlers;
            }

            @Override
            public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
                List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
                listeners.addAll(customProcessListeners);
                return listeners;
            }

            @Override
            public List<AgendaEventListener> getAgendaEventListeners(RuntimeEngine runtime) {
                List<AgendaEventListener> listeners = super.getAgendaEventListeners(runtime);
                listeners.addAll(customAgendaListeners);
                return listeners;
            }

            @Override
            public List<TaskLifeCycleEventListener> getTaskListeners() {
                List<TaskLifeCycleEventListener> listeners = super.getTaskListeners();
                listeners.addAll(customTaskListeners);
                return listeners;
            }
        });
    }
    builder.userGroupCallback(userGroupCallback);
    for (Entry<String, Object> envEntry : customEnvironmentEntries.entrySet()) {
        builder.addEnvironmentEntry(envEntry.getKey(), envEntry.getValue());
    }
    for (Map.Entry<String, ResourceType> entry : resources.entrySet()) {
        builder.addAsset(ResourceFactory.newClassPathResource(entry.getKey()), entry.getValue());
    }
    return createRuntimeManager(strategy, resources, builder.get(), identifier);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) DefaultRegisterableItemsFactory(org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory) HashMap(java.util.HashMap) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) ResourceType(org.kie.api.io.ResourceType) DefaultProcessInstanceManagerFactory(org.jbpm.process.instance.impl.DefaultProcessInstanceManagerFactory) RuntimeEnvironmentBuilder(org.kie.api.runtime.manager.RuntimeEnvironmentBuilder) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) SimpleRegisterableItemsFactory(org.jbpm.runtime.manager.impl.SimpleRegisterableItemsFactory) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) TaskLifeCycleEventListener(org.kie.api.task.TaskLifeCycleEventListener)

Example 39 with ProcessEventListener

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

the class MultipleTimerServicesTest method testGlobalTimerServiceOnIndependentManager.

public void testGlobalTimerServiceOnIndependentManager() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 3);
    NodeLeftCountDownProcessEventListener countDownListener2 = new NodeLeftCountDownProcessEventListener("timer", 3);
    // prepare listener to assert results
    final List<Long> timerExporations = new ArrayList<Long>();
    ProcessEventListener listenerM1 = new DefaultProcessEventListener() {

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            if (event.getNodeInstance().getNodeName().equals("timer")) {
                logger.debug("On manager 1");
                timerExporations.add(event.getProcessInstance().getId());
            }
        }
    };
    final List<Long> timerExporations2 = new ArrayList<Long>();
    ProcessEventListener listenerM2 = new DefaultProcessEventListener() {

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            if (event.getNodeInstance().getNodeName().equals("timer")) {
                logger.debug("On manager 2");
                timerExporations2.add(event.getProcessInstance().getId());
            }
        }
    };
    environmentM1 = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycle3.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new TestRegisterableItemsFactory(listenerM1, countDownListener)).schedulerService(globalScheduler1).get();
    environmentM2 = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf2).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycle3.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new TestRegisterableItemsFactory(listenerM2, countDownListener2)).schedulerService(globalScheduler2).get();
    managerM1 = getManager(environmentM1, "one");
    RuntimeEngine runtimeM1 = managerM1.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksessionM1 = runtimeM1.getKieSession();
    managerM2 = getManager(environmentM2, "two");
    RuntimeEngine runtimeM2 = managerM2.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksessionM2 = runtimeM2.getKieSession();
    ProcessInstance processInstanceM1 = ksessionM1.startProcess("IntermediateCatchEvent");
    assertTrue(processInstanceM1.getState() == ProcessInstance.STATE_ACTIVE);
    ProcessInstance processInstanceM2 = ksessionM2.startProcess("IntermediateCatchEvent");
    assertTrue(processInstanceM2.getState() == ProcessInstance.STATE_ACTIVE);
    // now wait for 1 second for first timer to trigger
    countDownListener.waitTillCompleted(2000);
    countDownListener2.waitTillCompleted(2000);
    // dispose session to force session to be reloaded on timer expiration
    managerM1.disposeRuntimeEngine(runtimeM1);
    managerM2.disposeRuntimeEngine(runtimeM2);
    countDownListener.waitTillCompleted();
    countDownListener2.waitTillCompleted();
    countDownListener.reset(1);
    countDownListener2.reset(1);
    try {
        runtimeM1 = managerM1.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceM1.getId()));
        ksessionM1 = runtimeM1.getKieSession();
        processInstanceM1 = ksessionM1.getProcessInstance(processInstanceM1.getId());
        assertNull(processInstanceM1);
    } catch (SessionNotFoundException e) {
    // expected in PerProcessInstance manager
    }
    try {
        runtimeM2 = managerM2.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceM2.getId()));
        ksessionM2 = runtimeM2.getKieSession();
        processInstanceM2 = ksessionM2.getProcessInstance(processInstanceM2.getId());
        assertNull(processInstanceM2);
    } catch (SessionNotFoundException e) {
    // expected in PerProcessInstance manager
    }
    // let's wait to ensure no more timers are expired and triggered
    countDownListener.waitTillCompleted(3000);
    countDownListener2.waitTillCompleted(3000);
    managerM1.disposeRuntimeEngine(runtimeM1);
    managerM2.disposeRuntimeEngine(runtimeM2);
    assertEquals(3, timerExporations.size());
    assertEquals(3, timerExporations2.size());
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ArrayList(java.util.ArrayList) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException)

Example 40 with ProcessEventListener

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

the class ProcessEventListenerTest method testInternalNodeSignalEvent.

@Test
public void testInternalNodeSignalEvent() {
    Reader source = new StringReader(process);
    builder.addRuleFlow(source);
    KieSession session = createKieSession(builder.getPackages());
    final List<ProcessEvent> processEventList = new ArrayList<ProcessEvent>();
    final ProcessEventListener listener = createProcessEventListener(processEventList);
    ((InternalWorkingMemory) session).getProcessRuntime().addEventListener(listener);
    ProcessInstance processInstance = ((InternalWorkingMemory) session).getProcessRuntime().startProcess("org.drools.core.event");
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    assertEquals("MyValue", ((VariableScopeInstance) ((org.jbpm.process.instance.ProcessInstance) processInstance).getContextInstance(VariableScope.VARIABLE_SCOPE)).getVariable("MyVar"));
    assertEquals(28, processEventList.size());
    for (ProcessEvent e : processEventList) {
        logger.debug(e.toString());
    }
    assertEquals("org.drools.core.event", ((ProcessStartedEvent) processEventList.get(2)).getProcessInstance().getProcessId());
}
Also used : ProcessEvent(org.kie.api.event.process.ProcessEvent) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

ProcessEventListener (org.kie.api.event.process.ProcessEventListener)88 Test (org.junit.Test)77 KieSession (org.kie.api.runtime.KieSession)64 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)64 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)61 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)58 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)49 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)44 ArrayList (java.util.ArrayList)41 DefaultRegisterableItemsFactory (org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory)33 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)30 HashMap (java.util.HashMap)29 AbstractExecutorBaseTest (org.jbpm.test.util.AbstractExecutorBaseTest)29 ProcessNodeLeftEvent (org.kie.api.event.process.ProcessNodeLeftEvent)27 NodeTriggeredCountDownProcessEventListener (org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener)20 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)18 KieBase (org.kie.api.KieBase)18 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)17 ProcessStartedEvent (org.kie.api.event.process.ProcessStartedEvent)17 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)14