Search in sources :

Example 6 with Trigger

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

the class JDKTimerServiceTest method testRepeatedExecutionJobWithRemove.

@Test
public void testRepeatedExecutionJobWithRemove() throws Exception {
    SessionConfiguration config = SessionConfiguration.newInstance();
    config.setClockType(ClockType.REALTIME_CLOCK);
    TimerService timeService = TimerServiceFactory.getTimerService(config);
    Trigger trigger = new DelayedTrigger(new long[] { 100, 100, 100, 100, 100, 100, 100, 100 });
    HelloWorldJobContext ctx = new HelloWorldJobContext("hello world", timeService);
    ctx.setLimit(3);
    timeService.scheduleJob(new HelloWorldJob(), ctx, trigger);
    Thread.sleep(1000);
    timeService.shutdown();
    assertEquals(5, ctx.getList().size());
}
Also used : Trigger(org.drools.core.time.Trigger) SessionConfiguration(org.drools.core.SessionConfiguration) TimerService(org.drools.core.time.TimerService) Test(org.junit.Test)

Example 7 with Trigger

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

the class TimerManager method internalAddTimer.

public void internalAddTimer(final TimerInstance timer) {
    long delay;
    Date lastTriggered = timer.getLastTriggered();
    if (lastTriggered == null) {
        Date activated = timer.getActivated();
        Date now = new Date();
        long timespan = now.getTime() - activated.getTime();
        delay = timer.getDelay() - timespan;
        if (delay < 0) {
            delay = 0;
        }
    } else {
        Date now = new Date();
        long timespan = now.getTime() - lastTriggered.getTime();
        delay = timespan - timer.getPeriod();
        if (delay < 0) {
            delay = 0;
        }
    }
    Trigger trigger = new IntervalTrigger(timerService.getCurrentTime(), null, null, -1, delay, timer.getPeriod(), null, null);
    ProcessJobContext ctx = new ProcessJobContext(timer, trigger, timer.getProcessInstanceId(), this.kruntime);
    JobHandle jobHandle = this.timerService.scheduleJob(processJob, ctx, trigger);
    timer.setJobHandle(jobHandle);
    timers.put(timer.getId(), timer);
}
Also used : JobHandle(org.drools.core.time.JobHandle) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger) Trigger(org.drools.core.time.Trigger) CronTrigger(org.drools.core.time.impl.CronTrigger) Date(java.util.Date) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger)

Example 8 with Trigger

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

the class TimerManager method registerTimer.

public void registerTimer(final TimerInstance timer, String processId, Map<String, Object> params) {
    try {
        kruntime.startOperation();
        timer.setId(++timerId);
        timer.setProcessInstanceId(-1l);
        timer.setSessionId(((StatefulKnowledgeSession) kruntime).getIdentifier());
        timer.setActivated(new Date());
        Trigger trigger = null;
        if (timer.getCronExpression() != null) {
            Date startTime = new Date(timerService.getCurrentTime() + 1000);
            trigger = new CronTrigger(timerService.getCurrentTime(), startTime, null, -1, timer.getCronExpression(), null, null);
            // cron timers are by nature repeatable
            timer.setPeriod(1);
        } else {
            trigger = new IntervalTrigger(timerService.getCurrentTime(), null, null, timer.getRepeatLimit(), timer.getDelay(), timer.getPeriod(), null, null);
        }
        StartProcessJobContext ctx = new StartProcessJobContext(timer, trigger, processId, params, this.kruntime);
        JobHandle jobHandle = this.timerService.scheduleJob(startProcessJob, ctx, trigger);
        timer.setJobHandle(jobHandle);
        timers.put(timer.getId(), timer);
    } finally {
        kruntime.endOperation();
    }
}
Also used : JobHandle(org.drools.core.time.JobHandle) CronTrigger(org.drools.core.time.impl.CronTrigger) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger) Trigger(org.drools.core.time.Trigger) CronTrigger(org.drools.core.time.impl.CronTrigger) Date(java.util.Date) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger)

Example 9 with Trigger

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

the class TimerManager method registerTimer.

public void registerTimer(final TimerInstance timer, ProcessInstance processInstance) {
    try {
        kruntime.startOperation();
        timer.setId(++timerId);
        timer.setProcessInstanceId(processInstance.getId());
        timer.setSessionId(((KieSession) kruntime).getIdentifier());
        timer.setActivated(new Date());
        Trigger trigger = null;
        if (timer.getCronExpression() != null) {
            Date startTime = new Date(timerService.getCurrentTime() + 1000);
            trigger = new CronTrigger(timerService.getCurrentTime(), startTime, null, -1, timer.getCronExpression(), null, null);
            // cron timers are by nature repeatable
            timer.setPeriod(1);
        } else {
            trigger = new IntervalTrigger(timerService.getCurrentTime(), null, null, timer.getRepeatLimit(), timer.getDelay(), timer.getPeriod(), null, null);
        }
        ProcessJobContext ctx = new ProcessJobContext(timer, trigger, processInstance.getId(), this.kruntime);
        JobHandle jobHandle = this.timerService.scheduleJob(processJob, ctx, trigger);
        timer.setJobHandle(jobHandle);
        timers.put(timer.getId(), timer);
    } finally {
        kruntime.endOperation();
    }
}
Also used : JobHandle(org.drools.core.time.JobHandle) CronTrigger(org.drools.core.time.impl.CronTrigger) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger) Trigger(org.drools.core.time.Trigger) CronTrigger(org.drools.core.time.impl.CronTrigger) Date(java.util.Date) IntervalTrigger(org.drools.core.time.impl.IntervalTrigger)

Example 10 with Trigger

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

the class JDKTimerServiceTest method testRepeatedExecutionJob.

@Test
public void testRepeatedExecutionJob() throws Exception {
    SessionConfiguration config = SessionConfiguration.newInstance();
    config.setClockType(ClockType.REALTIME_CLOCK);
    TimerService timeService = TimerServiceFactory.getTimerService(config);
    Trigger trigger = new DelayedTrigger(new long[] { 100, 100, 100 });
    HelloWorldJobContext ctx = new HelloWorldJobContext("hello world", timeService);
    timeService.scheduleJob(new HelloWorldJob(), ctx, trigger);
    Thread.sleep(500);
    timeService.shutdown();
    assertEquals(3, ctx.getList().size());
}
Also used : Trigger(org.drools.core.time.Trigger) SessionConfiguration(org.drools.core.SessionConfiguration) TimerService(org.drools.core.time.TimerService) Test(org.junit.Test)

Aggregations

Trigger (org.drools.core.time.Trigger)14 Date (java.util.Date)6 IntervalTrigger (org.drools.core.time.impl.IntervalTrigger)6 CronTrigger (org.drools.core.time.impl.CronTrigger)5 Test (org.junit.Test)5 JobHandle (org.drools.core.time.JobHandle)4 TimerService (org.drools.core.time.TimerService)4 SessionConfiguration (org.drools.core.SessionConfiguration)3 ByteString (com.google.protobuf.ByteString)2 TupleKey (org.drools.core.marshalling.TupleKey)2 CompositeMaxDurationTrigger (org.drools.core.time.impl.CompositeMaxDurationTrigger)2 PointInTimeTrigger (org.drools.core.time.impl.PointInTimeTrigger)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 ReteEvaluator (org.drools.core.common.ReteEvaluator)1 TupleKey (org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey)1 Builder (org.drools.core.marshalling.impl.ProtobufMessages.ProcessData.Builder)1 PhreakTimerNode (org.drools.core.phreak.PhreakTimerNode)1 Builder (org.drools.serialization.protobuf.ProtobufMessages.ProcessData.Builder)1 GlobalTimerService (org.jbpm.process.core.timer.impl.GlobalTimerService)1