Search in sources :

Example 11 with SessionNotFoundException

use of org.kie.internal.runtime.manager.SessionNotFoundException in project jbpm by kiegroup.

the class GlobalTimerServiceBaseTest method testInterediateTimerWithGlobalTestServiceSimulateCMT.

@Test(timeout = 20000)
public void testInterediateTimerWithGlobalTestServiceSimulateCMT() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 3);
    // prepare listener to assert results
    final List<Long> timerExporations = new ArrayList<Long>();
    ProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            if (event.getNodeInstance().getNodeName().equals("timer")) {
                timerExporations.add(event.getProcessInstance().getId());
            }
        }
    };
    Properties properties = new Properties();
    properties.setProperty("mary", "HR");
    properties.setProperty("john", "HR");
    UserGroupCallback userGroupCallback = new JBossUserGroupCallbackImpl(properties);
    EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate("org.jbpm.test.persistence");
    TransactionManager tm = new ContainerManagedTransactionManager();
    Environment env = EnvironmentFactory.newEnvironment();
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(EnvironmentName.TRANSACTION_MANAGER, tm);
    environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycleWithHT2.bpmn2"), ResourceType.BPMN2).addEnvironmentEntry(EnvironmentName.TRANSACTION_MANAGER, tm).addEnvironmentEntry(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER, new JpaProcessPersistenceContextManager(env)).addEnvironmentEntry(EnvironmentName.TASK_PERSISTENCE_CONTEXT_MANAGER, new JPATaskPersistenceContextManager(env)).schedulerService(globalScheduler).registerableItemsFactory(new TestRegisterableItemsFactory(listener, countDownListener)).userGroupCallback(userGroupCallback).get();
    RuntimeEngine runtime;
    KieSession ksession;
    ProcessInstance processInstance;
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    try {
        ut.begin();
        manager = getManager(environment, true);
        runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
        ksession = runtime.getKieSession();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("x", "R3/PT1S");
        processInstance = ksession.startProcess("IntermediateCatchEvent", params);
        ut.commit();
    } catch (Exception ex) {
        ut.rollback();
        throw ex;
    }
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    ut = InitialContext.doLookup("java:comp/UserTransaction");
    try {
        ut.begin();
        runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtime.getKieSession();
        // get tasks
        List<Status> statuses = new ArrayList<Status>();
        statuses.add(Status.Reserved);
        List<TaskSummary> tasks = runtime.getTaskService().getTasksAssignedAsPotentialOwnerByStatus("john", statuses, "en-UK");
        assertNotNull(tasks);
        assertEquals(1, tasks.size());
        for (TaskSummary task : tasks) {
            runtime.getTaskService().start(task.getId(), "john");
            runtime.getTaskService().complete(task.getId(), "john", null);
        }
        ut.commit();
    } catch (Exception ex) {
        ut.rollback();
        throw ex;
    }
    // now wait for 1 second for first timer to trigger
    countDownListener.waitTillCompleted();
    countDownListener.reset(1);
    ut = InitialContext.doLookup("java:comp/UserTransaction");
    try {
        ut.begin();
        try {
            runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
            ksession = runtime.getKieSession();
            processInstance = ksession.getProcessInstance(processInstance.getId());
            assertNull(processInstance);
        } catch (SessionNotFoundException e) {
        // expected for PerProcessInstanceManagers since process instance is completed
        }
        ut.commit();
    } catch (Exception ex) {
        ut.rollback();
        throw ex;
    }
    // let's wait to ensure no more timers are expired and triggered
    countDownListener.waitTillCompleted(3000);
    assertEquals(3, timerExporations.size());
}
Also used : HashMap(java.util.HashMap) JBossUserGroupCallbackImpl(org.jbpm.services.task.identity.JBossUserGroupCallbackImpl) ArrayList(java.util.ArrayList) Properties(java.util.Properties) UserGroupCallback(org.kie.api.task.UserGroupCallback) ContainerManagedTransactionManager(org.jbpm.persistence.jta.ContainerManagedTransactionManager) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) JPATaskPersistenceContextManager(org.jbpm.services.task.persistence.JPATaskPersistenceContextManager) KieSession(org.kie.api.runtime.KieSession) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException) UserTransaction(javax.transaction.UserTransaction) Status(org.kie.api.task.model.Status) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) JpaProcessPersistenceContextManager(org.jbpm.persistence.JpaProcessPersistenceContextManager) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException) TransactionManager(org.drools.persistence.api.TransactionManager) ContainerManagedTransactionManager(org.jbpm.persistence.jta.ContainerManagedTransactionManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) TaskSummary(org.kie.api.task.model.TaskSummary) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) Environment(org.kie.api.runtime.Environment) SimpleRuntimeEnvironment(org.jbpm.runtime.manager.impl.SimpleRuntimeEnvironment) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test)

Example 12 with SessionNotFoundException

use of org.kie.internal.runtime.manager.SessionNotFoundException in project jbpm by kiegroup.

the class GlobalTimerServiceBaseTest method testInterediateTimerWithGlobalTestServiceRollback.

@Test(timeout = 20000)
public void testInterediateTimerWithGlobalTestServiceRollback() throws Exception {
    environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycle3.bpmn2"), ResourceType.BPMN2).schedulerService(globalScheduler).get();
    manager = getManager(environment, true);
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    long ksessionId = ksession.getIdentifier();
    ProcessInstance processInstance;
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    try {
        ut.begin();
        processInstance = ksession.startProcess("IntermediateCatchEvent");
    } finally {
        ut.rollback();
    }
    manager.disposeRuntimeEngine(runtime);
    try {
        // two types of checks as different managers will treat it differently
        // per process instance will fail on getting runtime
        runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        // where singleton and per request will return runtime but there should not be process instance
        processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
        assertNull(processInstance);
    } catch (SessionNotFoundException e) {
    }
    TimerService timerService = TimerServiceRegistry.getInstance().get(manager.getIdentifier() + TimerServiceRegistry.TIMER_SERVICE_SUFFIX);
    Collection<TimerJobInstance> timerInstances = timerService.getTimerJobInstances(ksessionId);
    assertNotNull(timerInstances);
    assertEquals(0, timerInstances.size());
    if (runtime != null) {
        manager.disposeRuntimeEngine(runtime);
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) TimerJobInstance(org.drools.core.time.impl.TimerJobInstance) KieSession(org.kie.api.runtime.KieSession) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) TimerService(org.drools.core.time.TimerService) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException) Test(org.junit.Test)

Example 13 with SessionNotFoundException

use of org.kie.internal.runtime.manager.SessionNotFoundException in project jbpm by kiegroup.

the class GlobalTimerServiceBaseTest method testTimerFailureAndRetrigger.

@Test(timeout = 20000)
public void testTimerFailureAndRetrigger() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("Timer_1m", 3);
    final List<Long> timerExporations = new ArrayList<Long>();
    ProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
            if (event.getNodeInstance().getNodeName().equals("Timer_1m")) {
                timerExporations.add(event.getNodeInstance().getId());
            }
        }
    };
    environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/helloretrigger.bpmn2"), ResourceType.BPMN2).schedulerService(globalScheduler).registerableItemsFactory(new TestRegisterableItemsFactory(listener, countDownListener)).get();
    manager = getManager(environment, false);
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    Map<String, Object> params = new HashMap<>();
    ProcessInstance pi = ksession.startProcess("rescheduletimer.helloretrigger", params);
    assertEquals("Process instance should be active", ProcessInstance.STATE_ACTIVE, pi.getState());
    manager.disposeRuntimeEngine(runtime);
    final long processInstanceId = pi.getId();
    // let the timer (every 2 sec) fire three times as third will fail on gateway
    countDownListener.waitTillCompleted(8000);
    assertEquals("There should be only 3 nodes as there third is failing", 3, timerExporations.size());
    runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));
    ksession = runtime.getKieSession();
    ksession.execute(new ExecutableCommand<Void>() {

        @Override
        public Void execute(Context context) {
            KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
            ProcessInstance pi = (ProcessInstance) ksession.getProcessInstance(processInstanceId);
            ((WorkflowProcessInstance) pi).setVariable("fixed", true);
            return null;
        }
    });
    manager.disposeRuntimeEngine(runtime);
    countDownListener.reset(1);
    countDownListener.waitTillCompleted(5000);
    assertEquals("There should be 3 expirations as the failing one should finally proceed", 3, timerExporations.size());
    try {
        runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));
        ksession = runtime.getKieSession();
        pi = ksession.getProcessInstance(processInstanceId);
        assertNull(pi);
    } catch (SessionNotFoundException e) {
    // expected for PerProcessInstanceManagers since process instance is completed
    }
    ((AbstractRuntimeManager) manager).close(true);
}
Also used : InitialContext(javax.naming.InitialContext) RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) ProcessInstanceIdContext(org.kie.internal.runtime.manager.context.ProcessInstanceIdContext) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager) ArrayList(java.util.ArrayList) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) KieSession(org.kie.api.runtime.KieSession) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException) Test(org.junit.Test)

Example 14 with SessionNotFoundException

use of org.kie.internal.runtime.manager.SessionNotFoundException in project jbpm by kiegroup.

the class GlobalTimerServiceBaseTest method testInterediateTimerWithHTBeforeWithGlobalTestService.

@Test(timeout = 20000)
public void testInterediateTimerWithHTBeforeWithGlobalTestService() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 3);
    // prepare listener to assert results
    final List<Long> timerExporations = new ArrayList<Long>();
    ProcessEventListener listener = new DefaultProcessEventListener() {

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            if (event.getNodeInstance().getNodeName().equals("timer")) {
                timerExporations.add(event.getProcessInstance().getId());
            }
        }
    };
    Properties properties = new Properties();
    properties.setProperty("mary", "HR");
    properties.setProperty("john", "HR");
    UserGroupCallback userGroupCallback = new JBossUserGroupCallbackImpl(properties);
    environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycleWithHT2.bpmn2"), ResourceType.BPMN2).schedulerService(globalScheduler).registerableItemsFactory(new TestRegisterableItemsFactory(listener, countDownListener)).userGroupCallback(userGroupCallback).get();
    manager = getManager(environment, true);
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "R3/PT1S");
    ProcessInstance processInstance = ksession.startProcess("IntermediateCatchEvent", params);
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    // get tasks
    List<Status> statuses = new ArrayList<Status>();
    statuses.add(Status.Reserved);
    List<TaskSummary> tasks = runtime.getTaskService().getTasksAssignedAsPotentialOwnerByStatus("john", statuses, "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    for (TaskSummary task : tasks) {
        runtime.getTaskService().start(task.getId(), "john");
        runtime.getTaskService().complete(task.getId(), "john", null);
    }
    // dispose session to force session to be reloaded on timer expiration
    manager.disposeRuntimeEngine(runtime);
    // now wait for 1 second for first timer to trigger
    countDownListener.waitTillCompleted();
    countDownListener.reset(1);
    try {
        runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtime.getKieSession();
        processInstance = ksession.getProcessInstance(processInstance.getId());
        assertNull(processInstance);
    } catch (SessionNotFoundException e) {
    // expected for PerProcessInstanceManagers since process instance is completed
    }
    // let's wait to ensure no more timers are expired and triggered
    countDownListener.waitTillCompleted(3000);
    manager.disposeRuntimeEngine(runtime);
    assertEquals(3, timerExporations.size());
}
Also used : Status(org.kie.api.task.model.Status) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) JBossUserGroupCallbackImpl(org.jbpm.services.task.identity.JBossUserGroupCallbackImpl) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) ArrayList(java.util.ArrayList) Properties(java.util.Properties) UserGroupCallback(org.kie.api.task.UserGroupCallback) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) TaskSummary(org.kie.api.task.model.TaskSummary) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException) Test(org.junit.Test)

Example 15 with SessionNotFoundException

use of org.kie.internal.runtime.manager.SessionNotFoundException in project jbpm by kiegroup.

the class GlobalTimerServiceBaseTest method testInterediateBoundaryTimerWithGlobalTestServiceRollback.

@Test(timeout = 20000)
public void testInterediateBoundaryTimerWithGlobalTestServiceRollback() throws Exception {
    Properties properties = new Properties();
    properties.setProperty("mary", "HR");
    properties.setProperty("john", "HR");
    UserGroupCallback userGroupCallback = new JBossUserGroupCallbackImpl(properties);
    environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/HumanTaskWithBoundaryTimer.bpmn"), ResourceType.BPMN2).schedulerService(globalScheduler).userGroupCallback(userGroupCallback).get();
    manager = getManager(environment, true);
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    long ksessionId = ksession.getIdentifier();
    ProcessInstance processInstance;
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    try {
        ut.begin();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("test", "john");
        processInstance = ksession.startProcess("PROCESS_1", params);
    } finally {
        ut.rollback();
    }
    manager.disposeRuntimeEngine(runtime);
    try {
        // two types of checks as different managers will treat it differently
        // per process instance will fail on getting runtime
        runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        // where singleton and per request will return runtime but there should not be process instance
        processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
        assertNull(processInstance);
    } catch (SessionNotFoundException e) {
    }
    TimerService timerService = TimerServiceRegistry.getInstance().get(manager.getIdentifier() + TimerServiceRegistry.TIMER_SERVICE_SUFFIX);
    Collection<TimerJobInstance> timerInstances = timerService.getTimerJobInstances(ksessionId);
    assertNotNull(timerInstances);
    assertEquals(0, timerInstances.size());
    if (runtime != null) {
        manager.disposeRuntimeEngine(runtime);
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) TimerJobInstance(org.drools.core.time.impl.TimerJobInstance) HashMap(java.util.HashMap) JBossUserGroupCallbackImpl(org.jbpm.services.task.identity.JBossUserGroupCallbackImpl) Properties(java.util.Properties) UserGroupCallback(org.kie.api.task.UserGroupCallback) TimerService(org.drools.core.time.TimerService) KieSession(org.kie.api.runtime.KieSession) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException) Test(org.junit.Test)

Aggregations

SessionNotFoundException (org.kie.internal.runtime.manager.SessionNotFoundException)28 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)27 KieSession (org.kie.api.runtime.KieSession)25 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)15 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)14 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)14 DeploymentNotFoundException (org.jbpm.services.api.DeploymentNotFoundException)13 ProcessInstanceNotFoundException (org.jbpm.services.api.ProcessInstanceNotFoundException)13 DeployedUnit (org.jbpm.services.api.model.DeployedUnit)13 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)8 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)8 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)7 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)6 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)6 HashMap (java.util.HashMap)5 ProcessNodeLeftEvent (org.kie.api.event.process.ProcessNodeLeftEvent)5 TaskSummary (org.kie.api.task.model.TaskSummary)5 WorkItem (org.kie.api.runtime.process.WorkItem)4 Properties (java.util.Properties)3