Search in sources :

Example 41 with DefaultProcessEventListener

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

the class TimerUpdateTest method updateTimerBeforeDelayTest.

@Test(timeout = 30000)
public void updateTimerBeforeDelayTest() {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener(TIMER_NAME, 1);
    // delay is set for 5s
    setProcessScenario(TIMER_FILE);
    kieSession.addEventListener(countDownListener);
    final List<Long> list = new ArrayList<Long>();
    kieSession.addEventListener(new DefaultProcessEventListener() {

        @Override
        public void beforeProcessStarted(ProcessStartedEvent event) {
            list.add(event.getProcessInstance().getId());
        }
    });
    Assertions.assertThat(list).isEmpty();
    long id = kieSession.startProcess(PROCESS_NAME).getId();
    long startTime = System.currentTimeMillis();
    Assertions.assertThat(list).isNotEmpty();
    // set delay on time that passed -> expected that timer fired immediately
    kieSession.execute(new UpdateTimerCommand(id, TIMER_NAME, -5));
    countDownListener.waitTillCompleted();
    Assertions.assertThat(timerHasFired()).isTrue();
    long firedTime = timerFiredTime();
    long timeDifference = Math.abs(firedTime - startTime);
    logger.info("Start time: " + startTime + ", fired time: " + firedTime + ", difference: " + (firedTime - startTime));
    Assertions.assertThat(timeDifference).isLessThan(500);
    Assertions.assertThat(kieSession.getProcessInstance(id)).isNull();
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) UpdateTimerCommand(org.jbpm.process.instance.command.UpdateTimerCommand) Test(org.junit.Test)

Example 42 with DefaultProcessEventListener

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

the class EjbTimerServiceIntegrationTest method testProcessWithTimerOverEJBTimerService.

public void testProcessWithTimerOverEJBTimerService(String delay) throws InterruptedException {
    cleanupSingletonSessionId();
    final List<String> timerExecution = new ArrayList<String>();
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-TimerTask.bpmn2"), ResourceType.BPMN2).schedulerService(new EjbSchedulerService()).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

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

                @Override
                public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
                    if (event.getNodeInstance().getNodeName().equals("Event")) {
                        timerExecution.add(event.getNodeInstance().getNodeName());
                    }
                }
            });
            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> parameters = new HashMap<String, Object>();
    parameters.put("x", delay);
    ProcessInstance instance = ksession.startProcess("IntermediateCatchEvent", parameters);
    assertNotNull(instance);
    Thread.sleep(3000);
    instance = ksession.getProcessInstance(instance.getId());
    assertNull(instance);
    assertEquals(1, timerExecution.size());
    manager.disposeRuntimeEngine(runtime);
    // close manager which will close session maintained by the manager
    manager.close();
}
Also used : DefaultRegisterableItemsFactory(org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) EjbSchedulerService(org.jbpm.services.ejb.timer.EjbSchedulerService) HashMap(java.util.HashMap) 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) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 43 with DefaultProcessEventListener

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

the class ProcessTest method main.

public static final void main(String[] args) {
    try {
        // load up the knowledge base
        final KieSession ksession = getKieSession();
        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) {
            }
        });
        KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newFileLogger(ksession, "test");
        // start a new process instance
        Person person = new Person("john", "John Doe");
        person.setAge(20);
        Request request = new Request("12345");
        request.setPersonId("john");
        request.setAmount(1000L);
        ksession.insert(person);
        ksession.insert(request);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("request", request);
        WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.startProcess("com.sample.requestHandling", params);
        ksession.insert(processInstance);
        ksession.fireAllRules();
        // signaling to select tasks in the ad-hoc sub-process
        ksession.signalEvent("Investigate", null, processInstance.getId());
        // dynamically adding a new sub-process
        DynamicNodeInstance dynamicNodeInstance = (DynamicNodeInstance) processInstance.getNodeInstances().iterator().next();
        DynamicUtils.addDynamicSubProcess(dynamicNodeInstance, ksession, "com.sample.contactCustomer", null);
        // event processing to detect too much processes being started
        ksession.addEventListener(new DefaultProcessEventListener() {

            public void beforeProcessStarted(ProcessStartedEvent event) {
                ksession.insert(event);
            }
        });
        Request request2 = new Request("12346");
        request2.setPersonId("john");
        request2.setAmount(1000L);
        params = new HashMap<String, Object>();
        params.put("request", request2);
        ksession.startProcess("com.sample.requestHandling", params);
        ksession.fireAllRules();
        Request request3 = new Request("12347");
        request3.setPersonId("john");
        request3.setAmount(1000L);
        params = new HashMap<String, Object>();
        params.put("request", request);
        ksession.startProcess("com.sample.requestHandling", params);
        ksession.fireAllRules();
        Request request4 = new Request("12348");
        request4.setPersonId("john");
        request4.setAmount(1000L);
        params = new HashMap<String, Object>();
        params.put("request", request4);
        ksession.startProcess("com.sample.requestHandling", params);
        ksession.fireAllRules();
        Request request5 = new Request("12349");
        request5.setPersonId("john");
        request5.setAmount(1000L);
        params = new HashMap<String, Object>();
        params.put("request", request5);
        ksession.startProcess("com.sample.requestHandling", params);
        ksession.fireAllRules();
        Request request6 = new Request("12350");
        request6.setPersonId("john");
        request6.setAmount(1000L);
        params = new HashMap<String, Object>();
        params.put("request", request6);
        processInstance = (WorkflowProcessInstance) ksession.startProcess("com.sample.requestHandling", params);
        FactHandle handle = ksession.insert(request6);
        ksession.insert(processInstance);
        ksession.fireAllRules();
        // exception handling when canceling request
        request6.setCanceled(true);
        ksession.update(handle, request6);
        ksession.fireAllRules();
        logger.close();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) HashMap(java.util.HashMap) FactHandle(org.kie.api.runtime.rule.FactHandle) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance)

Example 44 with DefaultProcessEventListener

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

the class IntermediateEventTest method testEventSubprocessSignalWithStateNode.

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

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            if (event.getNodeInstance().getNodeName().equals("User Task 2")) {
                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.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    WorkItem workItemTopProcess = workItemHandler.getWorkItem();
    ksession.signalEvent("MySignal", null, processInstance.getId());
    assertProcessInstanceActive(processInstance);
    WorkItem workItem = workItemHandler.getWorkItem();
    assertThat(workItem).isNotNull();
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    ksession.signalEvent("MySignal", null);
    assertProcessInstanceActive(processInstance);
    workItem = workItemHandler.getWorkItem();
    assertThat(workItem).isNotNull();
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    ksession.signalEvent("MySignal", null);
    assertProcessInstanceActive(processInstance);
    workItem = workItemHandler.getWorkItem();
    assertThat(workItem).isNotNull();
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    ksession.signalEvent("MySignal", null);
    assertProcessInstanceActive(processInstance);
    workItem = workItemHandler.getWorkItem();
    assertThat(workItem).isNotNull();
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertThat(workItemTopProcess).isNotNull();
    ksession.getWorkItemManager().completeWorkItem(workItemTopProcess.getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);
    assertNodeTriggered(processInstance.getId(), "start", "User Task 1", "end", "Sub Process 1", "start-sub", "User Task 2", "end-sub");
    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) Test(org.junit.Test)

Example 45 with DefaultProcessEventListener

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

the class IntermediateEventTest method testEventSubprocessMessage.

@Test
public void testEventSubprocessMessage() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-EventSubprocessMessage.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-EventSubprocessMessage");
    assertProcessInstanceActive(processInstance);
    ksession = restoreSession(ksession, true);
    ksession.addEventListener(listener);
    ksession.signalEvent("Message-HelloMessage", null, processInstance.getId());
    ksession.signalEvent("Message-HelloMessage", null);
    ksession.signalEvent("Message-HelloMessage", null);
    ksession.signalEvent("Message-HelloMessage", null);
    ksession.getProcessInstance(processInstance.getId());
    ksession.getProcessInstance(processInstance.getId());
    ksession.getProcessInstance(processInstance.getId());
    ksession.getProcessInstance(processInstance.getId());
    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(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) 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