Search in sources :

Example 51 with RuntimeEnvironment

use of org.kie.api.runtime.manager.RuntimeEnvironment in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManagerTest method testSignalStartMultipleProcesses.

@Test
public void testSignalStartMultipleProcesses() {
    // independent = true
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-SignalMultipleProcessesMain.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-SignalMultipleProcessesOne.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-SignalMultipleProcessesTwo.bpmn2"), ResourceType.BPMN2).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    long ksession1Id = ksession.getIdentifier();
    assertTrue(ksession1Id == 2);
    Map<String, Object> inputParams = new HashMap<String, Object>();
    inputParams.put("processInput", "MyCoolParam");
    ksession.startProcess("main-process", inputParams);
    AuditService auditService = runtime.getAuditService();
    List<? extends ProcessInstanceLog> processInstanceLogs = auditService.findProcessInstances();
    assertEquals(3, processInstanceLogs.size());
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) KieSession(org.kie.api.runtime.KieSession) AuditService(org.kie.api.runtime.manager.audit.AuditService) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 52 with RuntimeEnvironment

use of org.kie.api.runtime.manager.RuntimeEnvironment in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManagerTest method testExecuteCompleteWorkItemOnInvalidSessionWithPersistence.

@Test
public void testExecuteCompleteWorkItemOnInvalidSessionWithPersistence() {
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    // ksession for process instance #1
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    long ksession1Id = ksession.getIdentifier();
    assertTrue(ksession1Id == 2);
    // ksession for process instance #2
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession2 = runtime2.getKieSession();
    assertNotNull(ksession2);
    long ksession2Id = ksession2.getIdentifier();
    assertTrue(ksession2Id == 3);
    ProcessInstance pi1 = ksession.startProcess("UserTask");
    ProcessInstance pi2 = ksession2.startProcess("UserTask");
    // both processes started
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    assertEquals(ProcessInstance.STATE_ACTIVE, pi2.getState());
    runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi1.getId()));
    ksession = runtime.getKieSession();
    assertEquals(ksession1Id, ksession.getIdentifier());
    ksession.getWorkItemManager().completeWorkItem(1, null);
    manager.disposeRuntimeEngine(runtime);
    // since process is completed now session should not be there any more
    try {
        manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi1.getId())).getKieSession();
        fail("Session for this (" + pi1.getId() + ") process instance is no more accessible");
    } catch (RuntimeException e) {
    }
    try {
        ksession.getWorkItemManager().completeWorkItem(2, null);
        fail("Invalid session was used for (" + pi2.getId() + ") process instance");
    } catch (RuntimeException e) {
    }
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 53 with RuntimeEnvironment

use of org.kie.api.runtime.manager.RuntimeEnvironment in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManagerTest method testTimersOnMultiInstanceSubprocess.

@Test(timeout = 20000)
public void testTimersOnMultiInstanceSubprocess() throws Exception {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("MIDelayTimer", 2);
    final List<Long> timerExpirations = new ArrayList<Long>();
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

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

                @Override
                public void afterNodeLeft(ProcessNodeLeftEvent event) {
                    if (event.getNodeInstance().getNodeName().equals("MIDebugScript")) {
                        timerExpirations.add(event.getProcessInstance().getId());
                    }
                }
            });
            listeners.add(countDownListener);
            return listeners;
        }
    }).addAsset(ResourceFactory.newClassPathResource("BPMN2-MultiInstanceProcess.bpmn2"), ResourceType.BPMN2).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    // ksession for process instance #1
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    ProcessInstance pi1 = ksession.startProcess("defaultPackage.MultiInstanceProcess");
    // both processes started
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    manager.disposeRuntimeEngine(runtime);
    // wait a bit for some timers to fire
    countDownListener.waitTillCompleted();
    // now make sure nothing else is triggered
    countDownListener.reset(4);
    countDownListener.waitTillCompleted(3000);
    assertEquals(2, timerExpirations.size());
    runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi1.getId()));
    ksession = runtime.getKieSession();
    pi1 = ksession.getProcessInstance(pi1.getId());
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    ksession.abortProcessInstance(pi1.getId());
    manager.disposeRuntimeEngine(runtime);
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) ArrayList(java.util.ArrayList) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 54 with RuntimeEnvironment

use of org.kie.api.runtime.manager.RuntimeEnvironment in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManagerTest method testCreationOfSessionWithCustomTaskListener.

@Test
public void testCreationOfSessionWithCustomTaskListener() {
    final List<Long> addedTasks = new ArrayList<Long>();
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder().userGroupCallback(userGroupCallback).entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public List<TaskLifeCycleEventListener> getTaskListeners() {
            List<TaskLifeCycleEventListener> listeners = super.getTaskListeners();
            listeners.add(new DefaultTaskEventListener() {

                @Override
                public void afterTaskAddedEvent(TaskEvent event) {
                    addedTasks.add(event.getTask().getId());
                }
            });
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    // ksession for process instance #1
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    long ksession1Id = ksession.getIdentifier();
    assertTrue(ksession1Id == 1);
    // FIXME quick hack to overcome problems with same pi ids when not using persistence
    ksession.startProcess("ScriptTask");
    // ksession for process instance #2
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession2 = runtime2.getKieSession();
    assertNotNull(ksession2);
    long ksession2Id = ksession2.getIdentifier();
    assertTrue(ksession2Id == 2);
    ProcessInstance pi1 = ksession.startProcess("UserTask");
    ProcessInstance pi2 = ksession2.startProcess("UserTask");
    // both processes started
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    assertEquals(ProcessInstance.STATE_ACTIVE, pi2.getState());
    runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi1.getId()));
    ksession = runtime.getKieSession();
    assertEquals(ksession1Id, ksession.getIdentifier());
    runtime2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi2.getId()));
    ksession2 = runtime2.getKieSession();
    assertEquals(ksession2Id, ksession2.getIdentifier());
    assertEquals(2, addedTasks.size());
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) ArrayList(java.util.ArrayList) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) TaskEvent(org.kie.api.task.TaskEvent) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) JPATaskLifeCycleEventListener(org.jbpm.services.task.audit.JPATaskLifeCycleEventListener) TaskLifeCycleEventListener(org.kie.api.task.TaskLifeCycleEventListener) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 55 with RuntimeEnvironment

use of org.kie.api.runtime.manager.RuntimeEnvironment in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManagerTest method testReusableSubprocessWithWaitForCompletionFalse.

@Test(timeout = 10000)
public void testReusableSubprocessWithWaitForCompletionFalse() throws Exception {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("SLATimer", 1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("reusable-subprocess/parentprocess.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("reusable-subprocess/subprocess.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();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    ksession.startProcess("Project01360830.parentprocess");
    countDownListener.waitTillCompleted();
    manager.disposeRuntimeEngine(runtime);
    manager.close();
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Aggregations

RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)177 Test (org.junit.Test)163 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)148 KieSession (org.kie.api.runtime.KieSession)127 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)107 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)101 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)51 AbstractExecutorBaseTest (org.jbpm.test.util.AbstractExecutorBaseTest)49 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)48 HashMap (java.util.HashMap)47 DefaultRegisterableItemsFactory (org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory)42 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)38 ArrayList (java.util.ArrayList)21 TaskSummary (org.kie.api.task.model.TaskSummary)21 NodeTriggeredCountDownProcessEventListener (org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener)20 AuditService (org.kie.api.runtime.manager.audit.AuditService)20 CountDownAsyncJobListener (org.jbpm.executor.test.CountDownAsyncJobListener)19 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)19 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)19 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)17