Search in sources :

Example 56 with ProcessEventListener

use of org.kie.api.event.process.ProcessEventListener 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());
}
Also used : ProcessVariableChangedEvent(org.kie.api.event.process.ProcessVariableChangedEvent) ProcessEvent(org.kie.api.event.process.ProcessEvent) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) ProcessCompletedEvent(org.kie.api.event.process.ProcessCompletedEvent) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 57 with ProcessEventListener

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

the class AsyncContinuationSupportTest method testAsyncScriptTask.

@Test(timeout = 10000)
public void testAsyncScriptTask() throws Exception {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("Hello", 1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-AsyncScriptTask.bpmn2"), ResourceType.BPMN2).addEnvironmentEntry("ExecutorService", executorService).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
            Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
            handlers.put("async", new SystemOutWorkItemHandler());
            return handlers;
        }

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(countDownListener);
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    ProcessInstance processInstance = ksession.startProcess("AsyncScriptTask");
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    long processInstanceId = processInstance.getId();
    countDownListener.waitTillCompleted();
    processInstance = runtime.getKieSession().getProcessInstance(processInstanceId);
    assertNull(processInstance);
    List<? extends NodeInstanceLog> logs = runtime.getAuditService().findNodeInstances(processInstanceId);
    assertNotNull(logs);
    assertEquals(8, logs.size());
}
Also used : DefaultRegisterableItemsFactory(org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) NodeTriggeredCountDownProcessEventListener(org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractExecutorBaseTest(org.jbpm.test.util.AbstractExecutorBaseTest) Test(org.junit.Test)

Example 58 with ProcessEventListener

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

the class AsyncContinuationSupportTest method testAsyncServiceTask.

@Test(timeout = 10000)
public void testAsyncServiceTask() throws Exception {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("Hello", 1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-AsyncServiceProcess.bpmn2"), ResourceType.BPMN2).addEnvironmentEntry("ExecutorService", executorService).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
            Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
            handlers.put("async", new SystemOutWorkItemHandler());
            handlers.put("Service Task", new ServiceTaskHandler());
            return handlers;
        }

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(countDownListener);
            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> params = new HashMap<String, Object>();
    params.put("s", "john");
    ProcessInstance processInstance = ksession.startProcess("AsyncServiceProcess", params);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    long processInstanceId = processInstance.getId();
    countDownListener.waitTillCompleted();
    processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
    assertNull(processInstance);
    List<? extends NodeInstanceLog> logs = runtime.getAuditService().findNodeInstances(processInstanceId);
    assertNotNull(logs);
    assertEquals(6, logs.size());
}
Also used : DefaultRegisterableItemsFactory(org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) NodeTriggeredCountDownProcessEventListener(org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) ServiceTaskHandler(org.jbpm.bpmn2.handler.ServiceTaskHandler) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractExecutorBaseTest(org.jbpm.test.util.AbstractExecutorBaseTest) Test(org.junit.Test)

Example 59 with ProcessEventListener

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

the class AsyncContinuationSupportTest method testAsyncMISubProcess.

@Test(timeout = 10000)
public void testAsyncMISubProcess() throws Exception {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("Hello", 3);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-MultiInstanceLoopCharacteristicsProcess.bpmn2"), ResourceType.BPMN2).addEnvironmentEntry("ExecutorService", executorService).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
            Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
            handlers.put("async", new SystemOutWorkItemHandler());
            return handlers;
        }

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(countDownListener);
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    ArrayList<String> items = new ArrayList<String>();
    items.add("one");
    items.add("two");
    items.add("three");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("list", items);
    ProcessInstance processInstance = ksession.startProcess("MultiInstanceLoopCharacteristicsProcess", params);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    long processInstanceId = processInstance.getId();
    countDownListener.waitTillCompleted();
    processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
    assertNull(processInstance);
    List<? extends NodeInstanceLog> logs = runtime.getAuditService().findNodeInstances(processInstanceId);
    assertNotNull(logs);
    assertEquals(26, logs.size());
}
Also used : DefaultRegisterableItemsFactory(org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) NodeTriggeredCountDownProcessEventListener(org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) ArrayList(java.util.ArrayList) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractExecutorBaseTest(org.jbpm.test.util.AbstractExecutorBaseTest) Test(org.junit.Test)

Example 60 with ProcessEventListener

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

the class AsyncContinuationSupportTest method testAsyncModeWithSubProcess.

@Test(timeout = 10000)
public void testAsyncModeWithSubProcess() throws Exception {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("EndProcess", 1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-SubProcess.bpmn2"), ResourceType.BPMN2).addEnvironmentEntry("ExecutorService", executorService).addEnvironmentEntry("AsyncMode", "true").registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(countDownListener);
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    ProcessInstance processInstance = ksession.startProcess("SubProcess");
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    long processInstanceId = processInstance.getId();
    countDownListener.waitTillCompleted();
    processInstance = runtime.getKieSession().getProcessInstance(processInstanceId);
    assertNull(processInstance);
    List<? extends NodeInstanceLog> logs = runtime.getAuditService().findNodeInstances(processInstanceId);
    assertNotNull(logs);
    assertEquals(18, logs.size());
    waitForAllJobsToComplete();
    List<RequestInfo> completed = executorService.getCompletedRequests(new QueryContext());
    // there should be 7 completed commands (subprocess node itself, 3 inner script tasks, subprocess end node, outer script task, process end node)
    assertEquals(7, completed.size());
    Set<String> commands = completed.stream().map(RequestInfo::getCommandName).collect(Collectors.toSet());
    assertEquals(1, commands.size());
    assertEquals(AsyncSignalEventCommand.class.getName(), commands.iterator().next());
}
Also used : DefaultRegisterableItemsFactory(org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) NodeTriggeredCountDownProcessEventListener(org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) QueryContext(org.kie.api.runtime.query.QueryContext) RequestInfo(org.kie.api.executor.RequestInfo) AsyncSignalEventCommand(org.jbpm.process.core.async.AsyncSignalEventCommand) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractExecutorBaseTest(org.jbpm.test.util.AbstractExecutorBaseTest) Test(org.junit.Test)

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