Search in sources :

Example 1 with DefaultProcessEventListener

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

the class RequestUI method createKieSession.

private KieSession createKieSession() {
    try {
        final KieSession ksession = getKieSession();
        KieServices.Factory.get().getLoggers().newThreadedFileLogger(ksession, "test", 1000);
        UIWorkItemHandler handler = new UIWorkItemHandler();
        ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
        handler.setVisible(true);
        ksession.getWorkItemManager().registerWorkItemHandler("Email", new WorkItemHandler() {

            public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
                System.out.println("Sending email ...");
                manager.completeWorkItem(workItem.getId(), null);
            }

            public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
            }
        });
        Person person = new Person("john", "John Doe");
        person.setAge(40);
        ksession.insert(person);
        person = new Person("krisv", "Kris Verlaenen");
        person.setAge(30);
        ksession.insert(person);
        person = new Person("baby", "Baby");
        person.setAge(1);
        ksession.insert(person);
        ksession.addEventListener(new DefaultProcessEventListener() {

            public void beforeProcessStarted(ProcessStartedEvent event) {
                ksession.insert(event);
            }
        });
        return ksession;
    } catch (Throwable t) {
        throw new RuntimeException("Could not initialize session!", t);
    }
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemManager(org.kie.api.runtime.process.WorkItemManager)

Example 2 with DefaultProcessEventListener

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

the class IntermediateEventTest method testEventSubprocessConditional.

@Test
public void testEventSubprocessConditional() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-EventSubprocessConditional.bpmn2");
    final List<Long> executednodes = new ArrayList<Long>();
    ProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            if (event.getNodeInstance().getNodeName().equals("Script Task 1")) {
                executednodes.add(event.getNodeInstance().getId());
            }
        }
    };
    ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(listener);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    ProcessInstance processInstance = ksession.startProcess("BPMN2-EventSubprocessConditional");
    assertProcessInstanceActive(processInstance);
    Person person = new Person();
    person.setName("john");
    ksession.insert(person);
    WorkItem workItem = workItemHandler.getWorkItem();
    assertThat(workItem).isNotNull();
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);
    assertNodeTriggered(processInstance.getId(), "start", "User Task 1", "end", "Sub Process 1", "start-sub", "Script Task 1", "end-sub");
    assertThat(executednodes.size()).isEqualTo(1);
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) KieBase(org.kie.api.KieBase) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ArrayList(java.util.ArrayList) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) Person(org.jbpm.bpmn2.objects.Person) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test)

Example 3 with DefaultProcessEventListener

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

the class IntermediateEventTest method runTestEventSubprocessSignal.

public void runTestEventSubprocessSignal(String processFile, String[] completedNodes) throws Exception {
    KieBase kbase = createKnowledgeBase(processFile);
    final List<Long> executednodes = new ArrayList<Long>();
    ProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            if (event.getNodeInstance().getNodeName().equals("sub-script")) {
                executednodes.add(event.getNodeInstance().getId());
            }
        }
    };
    ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(listener);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    ProcessInstance processInstance = ksession.startProcess("BPMN2-EventSubprocessSignal");
    assertProcessInstanceActive(processInstance);
    ksession = restoreSession(ksession, true);
    ksession.addEventListener(listener);
    ksession.signalEvent("MySignal", null, processInstance.getId());
    assertProcessInstanceActive(processInstance);
    ksession.signalEvent("MySignal", null);
    assertProcessInstanceActive(processInstance);
    ksession.signalEvent("MySignal", null);
    assertProcessInstanceActive(processInstance);
    ksession.signalEvent("MySignal", null);
    assertProcessInstanceActive(processInstance);
    WorkItem workItem = workItemHandler.getWorkItem();
    assertThat(workItem).isNotNull();
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);
    assertNodeTriggered(processInstance.getId(), completedNodes);
    assertThat(executednodes.size()).isEqualTo(4);
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) KieBase(org.kie.api.KieBase) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ArrayList(java.util.ArrayList) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) WorkItem(org.kie.api.runtime.process.WorkItem)

Example 4 with DefaultProcessEventListener

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

the class SLAComplianceTest method testSLAonProcessViolatedNoTracking.

@Test
public void testSLAonProcessViolatedNoTracking() 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);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(ProcessInstance.SLA_PENDING, 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 5 with DefaultProcessEventListener

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

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