Search in sources :

Example 11 with TimerService

use of org.drools.core.time.TimerService in project jbpm by kiegroup.

the class GlobalQuartzDBTimerServiceTest method testAbortGlobalTestService.

/**
 * Test that illustrates that jobs are persisted and survives server restart
 * and as soon as GlobalTimerService is active jobs are fired and it loads and aborts the
 * process instance to illustrate jobs are properly removed when isntance is aborted
 * NOTE: this test is disabled by default as it requires real db (not in memory)
 * and test to be executed separately each with new jvm process
 */
@Test
@Ignore
public void testAbortGlobalTestService() throws Exception {
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycle3.bpmn2"), ResourceType.BPMN2).addConfiguration("drools.timerService", "org.jbpm.process.core.timer.impl.RegisteredTimerServiceDelegate").get();
    RuntimeManager manger = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    // build GlobalTimerService instance
    TimerService globalTs = new GlobalTimerService(manger, globalScheduler);
    // and register it in the registry under 'default' key
    TimerServiceRegistry.getInstance().registerTimerService("default", globalTs);
    // 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());
            }
        }
    };
    long id = -1;
    Thread.sleep(5000);
    RuntimeEngine runtime = manger.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    ksession.addEventListener(listener);
    ksession.abortProcessInstance(id);
    ProcessInstance processInstance = ksession.getProcessInstance(id);
    assertNull(processInstance);
    // let's wait to ensure no more timers are expired and triggered
    Thread.sleep(3000);
    ksession.dispose();
}
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) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager) ArrayList(java.util.ArrayList) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) TimerService(org.drools.core.time.TimerService) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with TimerService

use of org.drools.core.time.TimerService 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 TimerService

use of org.drools.core.time.TimerService 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)

Example 14 with TimerService

use of org.drools.core.time.TimerService in project jbpm by kiegroup.

the class GlobalTimerServiceVolumeTest method testRuntimeManagerStrategyWithTimerService.

@Test(timeout = 30000)
public void testRuntimeManagerStrategyWithTimerService() throws Exception {
    // prepare task service with users and groups
    RuntimeEngine engine = manager.getRuntimeEngine(EmptyContext.get());
    TaskService taskService = engine.getTaskService();
    Group grouphr = TaskModelProvider.getFactory().newGroup();
    ((InternalOrganizationalEntity) grouphr).setId("HR");
    Group groupadmins = TaskModelProvider.getFactory().newGroup();
    ((InternalOrganizationalEntity) groupadmins).setId("Administrators");
    User mary = TaskModelProvider.getFactory().newUser();
    ((InternalOrganizationalEntity) mary).setId("mary");
    User john = TaskModelProvider.getFactory().newUser();
    ((InternalOrganizationalEntity) john).setId("john");
    User admin = TaskModelProvider.getFactory().newUser();
    ((InternalOrganizationalEntity) admin).setId("Administrator");
    ((InternalTaskService) taskService).addGroup(grouphr);
    ((InternalTaskService) taskService).addGroup(groupadmins);
    ((InternalTaskService) taskService).addUser(mary);
    ((InternalTaskService) taskService).addUser(john);
    ((InternalTaskService) taskService).addUser(admin);
    manager.disposeRuntimeEngine(engine);
    int counter = numberOfProcesses;
    // start processes until oom
    while (counter > 0) {
        new GlobalTimerServiceVolumeTest.StartProcessPerProcessInstanceRunnable(manager).run();
        counter--;
    }
    Collection<TimerJobInstance> timers = null;
    Map<Long, List<GlobalJobHandle>> jobs = null;
    TimerService timerService = TimerServiceRegistry.getInstance().get(manager.getIdentifier() + TimerServiceRegistry.TIMER_SERVICE_SUFFIX);
    if (timerService != null) {
        if (timerService instanceof GlobalTimerService) {
            jobs = ((GlobalTimerService) timerService).getTimerJobsPerSession();
            timers = ((GlobalTimerService) timerService).getTimerJobFactoryManager().getTimerJobInstances();
        }
    }
    assertNotNull("Jobs should not be null as number of timers have been created", jobs);
    assertEquals("There should be no jobs in the global timer service", 0, jobs.size());
    assertNotNull("Timer instances should not be null as number of timers have been created", timers);
    assertEquals("There should be no timer instances in the global timer service manager", 0, timers.size());
    RuntimeEngine empty = manager.getRuntimeEngine(EmptyContext.get());
    AuditService logService = empty.getAuditService();
    List<? extends ProcessInstanceLog> logs = logService.findActiveProcessInstances("IntermediateCatchEvent");
    assertEquals("Active process instances should be " + numberOfProcesses, numberOfProcesses, logs.size());
    countDownListener.waitTillCompleted();
    List<TaskSummary> tasks = empty.getTaskService().getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertEquals("Number of John's tasks should be " + numberOfProcesses, numberOfProcesses, tasks.size());
    for (TaskSummary task : tasks) {
        RuntimeEngine piEngine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(task.getProcessInstanceId()));
        piEngine.getTaskService().start(task.getId(), "john");
        piEngine.getTaskService().complete(task.getId(), "john", null);
        manager.disposeRuntimeEngine(piEngine);
    }
    logs = logService.findActiveProcessInstances("IntermediateCatchEvent");
    assertEquals("Active process instances should be 0", 0, logs.size());
    logService.dispose();
    manager.disposeRuntimeEngine(empty);
}
Also used : Group(org.kie.api.task.model.Group) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) User(org.kie.api.task.model.User) TimerJobInstance(org.drools.core.time.impl.TimerJobInstance) InternalTaskService(org.kie.internal.task.api.InternalTaskService) TaskService(org.kie.api.task.TaskService) InternalTaskService(org.kie.internal.task.api.InternalTaskService) InternalOrganizationalEntity(org.kie.internal.task.api.model.InternalOrganizationalEntity) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) TimerService(org.drools.core.time.TimerService) TaskSummary(org.kie.api.task.model.TaskSummary) List(java.util.List) ArrayList(java.util.ArrayList) AuditService(org.kie.api.runtime.manager.audit.AuditService) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) Test(org.junit.Test)

Example 15 with TimerService

use of org.drools.core.time.TimerService in project drools by kiegroup.

the class EventFactHandle method unscheduleAllJobs.

public void unscheduleAllJobs(InternalWorkingMemory workingMemory) {
    if (!jobs.isEmpty()) {
        synchronized (jobs) {
            TimerService clock = workingMemory.getTimerService();
            while (!jobs.isEmpty()) {
                JobHandle job = jobs.removeFirst();
                clock.removeJob(job);
            }
        }
    }
}
Also used : JobHandle(org.drools.core.time.JobHandle) TimerService(org.drools.core.time.TimerService)

Aggregations

TimerService (org.drools.core.time.TimerService)23 GlobalTimerService (org.jbpm.process.core.timer.impl.GlobalTimerService)11 Test (org.junit.Test)9 JobHandle (org.drools.core.time.JobHandle)5 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)5 ArrayList (java.util.ArrayList)4 Trigger (org.drools.core.time.Trigger)4 TimerJobInstance (org.drools.core.time.impl.TimerJobInstance)4 KieSession (org.kie.api.runtime.KieSession)4 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)4 UserTransaction (javax.transaction.UserTransaction)3 SessionConfiguration (org.drools.core.SessionConfiguration)3 LeftTuple (org.drools.core.reteoo.LeftTuple)3 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)3 Task (org.kie.api.task.model.Task)3 Disposable (org.kie.internal.runtime.manager.Disposable)3 SessionNotFoundException (org.kie.internal.runtime.manager.SessionNotFoundException)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Properties (java.util.Properties)2