Search in sources :

Example 21 with ProcessEventListener

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

the class SLAComplianceTest method testSLAonUserTaskViolated.

@Test
public void testSLAonUserTaskViolated() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final ProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void afterSLAViolated(SLAViolatedEvent event) {
            latch.countDown();
        }
    };
    KieBase kbase = createKnowledgeBase("BPMN2-UserTaskWithSLAOnTask.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    ksession.addEventListener(listener);
    ProcessInstance processInstance = ksession.startProcess("UserTask");
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    WorkItem workItem = workItemHandler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("john", workItem.getParameter("ActorId"));
    boolean slaViolated = latch.await(10, TimeUnit.SECONDS);
    assertTrue("SLA was not violated while it is expected", slaViolated);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    Collection<NodeInstance> active = ((WorkflowProcessInstance) processInstance).getNodeInstances();
    assertEquals(1, active.size());
    NodeInstance userTaskNode = active.iterator().next();
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);
    int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(ProcessInstance.SLA_NA, slaCompliance);
    slaCompliance = getSLAComplianceForNodeInstance(processInstance.getId(), (org.jbpm.workflow.instance.NodeInstance) userTaskNode);
    assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
    ksession.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) SLAViolatedEvent(org.kie.api.event.process.SLAViolatedEvent) CountDownLatch(java.util.concurrent.CountDownLatch) WorkItem(org.kie.api.runtime.process.WorkItem) KieBase(org.kie.api.KieBase) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Test(org.junit.Test)

Example 22 with ProcessEventListener

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

the class SLAComplianceTest method testSLAonProcessViolated.

@Test
public void testSLAonProcessViolated() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final ProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void afterSLAViolated(SLAViolatedEvent event) {
            latch.countDown();
        }
    };
    KieBase kbase = createKnowledgeBase("BPMN2-UserTaskWithSLA.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    ksession.addEventListener(listener);
    ProcessInstance processInstance = ksession.startProcess("UserTask");
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    WorkItem workItem = workItemHandler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("john", workItem.getParameter("ActorId"));
    boolean slaViolated = latch.await(10, TimeUnit.SECONDS);
    assertTrue("SLA was not violated while it is expected", slaViolated);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);
    slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
    ksession.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) KieBase(org.kie.api.KieBase) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) SLAViolatedEvent(org.kie.api.event.process.SLAViolatedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) CountDownLatch(java.util.concurrent.CountDownLatch) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test)

Example 23 with ProcessEventListener

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

the class SLAComplianceTest method testSLAonProcessViolatedExternalTracking.

@Test
public void testSLAonProcessViolatedExternalTracking() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final ProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void afterSLAViolated(SLAViolatedEvent event) {
            latch.countDown();
        }
    };
    KieBase kbase = createKnowledgeBase("BPMN2-UserTaskWithSLA.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    ksession.addEventListener(listener);
    ksession.getEnvironment().set("SLATimerMode", "false");
    ProcessInstance processInstance = ksession.startProcess("UserTask");
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    WorkItem workItem = workItemHandler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("john", workItem.getParameter("ActorId"));
    boolean slaViolated = latch.await(5, TimeUnit.SECONDS);
    assertFalse("SLA should not violated by timer", slaViolated);
    // simulate external tracking of sla
    ksession.signalEvent("slaViolation", null, processInstance.getId());
    slaViolated = latch.await(10, TimeUnit.SECONDS);
    assertTrue("SLA was not violated while it is expected", slaViolated);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);
    slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
    ksession.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) KieBase(org.kie.api.KieBase) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) SLAViolatedEvent(org.kie.api.event.process.SLAViolatedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) CountDownLatch(java.util.concurrent.CountDownLatch) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test)

Example 24 with ProcessEventListener

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

the class MultiInstanceCallActivityRuntimeManagerTest method createEnvironment.

private RuntimeEnvironment createEnvironment() {
    countDownListener = new NodeLeftCountDownProcessEventListener("timer", numberOfChildProcesses);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("reusable-subprocess/Parent.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("reusable-subprocess/Child.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(countDownListener);
            return listeners;
        }
    }).get();
    return environment;
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)

Example 25 with ProcessEventListener

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

the class PerCaseRuntimeManagerTest method testEventSignalingBetweenProcessesWithPeristence.

@Test
public void testEventSignalingBetweenProcessesWithPeristence() {
    final Set<Long> ksessionUsed = new HashSet<Long>();
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().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 = 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);
    RuntimeEngine runtime = manager.getRuntimeEngine(CaseContext.get("Case-1"));
    KieSession ksession = runtime.getKieSession();
    long ksession1Id = ksession.getIdentifier();
    assertNotNull(ksession);
    ksession.startProcess("com.sample.bpmn.hello");
    AuditService auditService = runtime.getAuditService();
    List<? extends ProcessInstanceLog> throwProcessLogs = auditService.findProcessInstances("com.sample.bpmn.hello");
    List<? extends ProcessInstanceLog> catchProcessLogs = auditService.findProcessInstances("com.sample.bpmn.Second");
    assertNotNull(throwProcessLogs);
    assertEquals(1, throwProcessLogs.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, throwProcessLogs.get(0).getStatus().intValue());
    assertNotNull(catchProcessLogs);
    assertEquals(1, catchProcessLogs.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, catchProcessLogs.get(0).getStatus().intValue());
    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) AuditService(org.kie.api.runtime.manager.audit.AuditService) HashSet(java.util.HashSet) 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