Search in sources :

Example 6 with TimerInstance

use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.

the class TimerTest method testTimer.

@Test
@Disabled
public void testTimer() {
    KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
    RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance() {

        private static final long serialVersionUID = 510l;

        public void signalEvent(String type, Object event) {
            if ("timerTriggered".equals(type)) {
                TimerInstance timer = (TimerInstance) event;
                logger.info("Timer {} triggered", timer.getId());
                counter++;
            }
        }
    };
    processInstance.setKnowledgeRuntime(((InternalWorkingMemory) kruntime.getKieSession()).getKnowledgeRuntime());
    processInstance.setId("1234");
    InternalProcessRuntime processRuntime = ((InternalProcessRuntime) ((InternalWorkingMemory) kruntime.getKieSession()).getProcessRuntime());
    processRuntime.getProcessInstanceManager().internalAddProcessInstance(processInstance);
    new Thread(() -> kruntime.getKieSession().fireUntilHalt()).start();
    JobsService jobService = new LegacyInMemoryJobService(kruntime, new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()));
    ProcessInstanceJobDescription desc = ProcessInstanceJobDescription.of(ExactExpirationTime.now(), processInstance.getStringId(), "test");
    String jobId = jobService.scheduleProcessInstanceJob(desc);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    counter = 0;
    desc = ProcessInstanceJobDescription.of(DurationExpirationTime.after(500), processInstance.getStringId(), "test");
    jobId = jobService.scheduleProcessInstanceJob(desc);
    assertEquals(0, counter);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    counter = 0;
    desc = ProcessInstanceJobDescription.of(DurationExpirationTime.repeat(500, 300L), processInstance.getStringId(), "test");
    jobId = jobService.scheduleProcessInstanceJob(desc);
    assertEquals(0, counter);
    try {
        Thread.sleep(700);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    // we can't know exactly how many times this will fire as timers are not precise, but should be at least 4
    assertTrue(counter >= 4);
    jobService.cancelJob(jobId);
    int lastCount = counter;
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(lastCount, counter);
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) TimerInstance(org.kie.kogito.timer.TimerInstance) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) JobsService(org.kie.kogito.jobs.JobsService) CollectingUnitOfWorkFactory(org.kie.kogito.services.uow.CollectingUnitOfWorkFactory) ProcessInstanceJobDescription(org.kie.kogito.jobs.ProcessInstanceJobDescription) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) DefaultUnitOfWorkManager(org.kie.kogito.services.uow.DefaultUnitOfWorkManager) LegacyInMemoryJobService(org.kie.kogito.services.jobs.impl.LegacyInMemoryJobService) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

TimerInstance (org.kie.kogito.timer.TimerInstance)6 Date (java.util.Date)4 ProcessInstanceJobDescription (org.kie.kogito.jobs.ProcessInstanceJobDescription)3 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)2 BusinessCalendar (org.jbpm.process.core.timer.BusinessCalendar)2 InternalProcessRuntime (org.jbpm.process.instance.InternalProcessRuntime)2 TimerJobId (org.kie.kogito.jobs.TimerJobId)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Function (java.util.function.Function)1