Search in sources :

Example 1 with JobContext

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

the class PseudoClockSchedulerTest method timerIsResetWhenJobThrowsExceptions.

@Test
public void timerIsResetWhenJobThrowsExceptions() {
    final Date triggerTime = new Date(1000);
    when(mockTrigger_1.hasNextFireTime()).thenReturn(triggerTime, triggerTime, triggerTime, null);
    when(mockTrigger_1.nextFireTime()).thenReturn(triggerTime);
    Job job = new Job() {

        public void execute(JobContext ctx) {
            assertThat(scheduler.getCurrentTime(), is(1000L));
            throw new RuntimeException("for test");
        }
    };
    scheduler.scheduleJob(job, this.mockContext_1, mockTrigger_1);
    scheduler.advanceTime(5000, TimeUnit.MILLISECONDS);
    // The time must be advanced correctly even when the job throws an exception
    assertThat(scheduler.getCurrentTime(), is(5000L));
    verify(mockTrigger_1, atLeast(2)).hasNextFireTime();
    verify(mockTrigger_1, times(1)).nextFireTime();
}
Also used : JobContext(org.drools.core.time.JobContext) Job(org.drools.core.time.Job) Date(java.util.Date) Test(org.junit.Test)

Example 2 with JobContext

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

the class PseudoClockSchedulerTest method timerIsSetToJobTriggerTimeForExecution.

@Test
public void timerIsSetToJobTriggerTimeForExecution() {
    final Date triggerTime = new Date(1000);
    when(mockTrigger_1.hasNextFireTime()).thenReturn(triggerTime, triggerTime, triggerTime, null);
    when(mockTrigger_1.nextFireTime()).thenReturn(triggerTime);
    Job job = new Job() {

        public void execute(JobContext ctx) {
            // Even though the clock has been advanced to 5000, the job should run
            // with the time set its trigger time.
            assertThat(scheduler.getCurrentTime(), is(1000L));
        }
    };
    scheduler.scheduleJob(job, this.mockContext_1, mockTrigger_1);
    scheduler.advanceTime(5000, TimeUnit.MILLISECONDS);
    // Now, after the job has been executed the time should be what it was advanced to
    assertThat(scheduler.getCurrentTime(), is(5000L));
    verify(mockTrigger_1, atLeast(2)).hasNextFireTime();
    verify(mockTrigger_1, times(1)).nextFireTime();
}
Also used : JobContext(org.drools.core.time.JobContext) Job(org.drools.core.time.Job) Date(java.util.Date) Test(org.junit.Test)

Example 3 with JobContext

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

the class ProtobufOutputMarshaller method writeTimers.

private static ProtobufMessages.Timers writeTimers(Collection<TimerJobInstance> timers, MarshallerWriteContext outCtx) {
    if (!timers.isEmpty()) {
        List<TimerJobInstance> sortedTimers = new ArrayList<TimerJobInstance>(timers);
        Collections.sort(sortedTimers, new Comparator<TimerJobInstance>() {

            public int compare(TimerJobInstance o1, TimerJobInstance o2) {
                return (int) (o1.getJobHandle().getId() - o2.getJobHandle().getId());
            }
        });
        ProtobufMessages.Timers.Builder _timers = ProtobufMessages.Timers.newBuilder();
        for (TimerJobInstance timer : sortedTimers) {
            JobContext jctx = ((SelfRemovalJobContext) timer.getJobContext()).getJobContext();
            if (jctx instanceof ObjectTypeNode.ExpireJobContext && !((ObjectTypeNode.ExpireJobContext) jctx).getExpireAction().getFactHandle().isValid()) {
                continue;
            }
            TimersOutputMarshaller writer = outCtx.writersByClass.get(jctx.getClass());
            Timer _timer = writer.serialize(jctx, outCtx);
            if (_timer != null) {
                _timers.addTimer(_timer);
            }
        }
        return _timers.build();
    }
    return null;
}
Also used : TimerJobInstance(org.drools.core.time.impl.TimerJobInstance) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) SelfRemovalJobContext(org.drools.core.time.SelfRemovalJobContext) Timer(org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer) JobContext(org.drools.core.time.JobContext) SelfRemovalJobContext(org.drools.core.time.SelfRemovalJobContext) Timers(org.drools.core.marshalling.impl.ProtobufMessages.Timers)

Example 4 with JobContext

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

the class SlidingTimeWindow method updateNextExpiration.

protected void updateNextExpiration(final InternalFactHandle fact, final InternalWorkingMemory workingMemory, final Behavior.Context context, final int nodeId) {
    TimerService clock = workingMemory.getTimerService();
    if (fact != null) {
        long nextTimestamp = ((EventFactHandle) fact).getStartTimestamp() + getSize();
        if (nextTimestamp < clock.getCurrentTime()) {
            // Past and out-of-order events should not be insert,
            // but the engine silently accepts them anyway, resulting in possibly undesirable behaviors
            workingMemory.queueWorkingMemoryAction(new BehaviorExpireWMAction(nodeId, this, context));
        } else {
            JobContext jobctx = new BehaviorJobContext(nodeId, workingMemory, this, context);
            JobHandle handle = clock.scheduleJob(job, jobctx, new PointInTimeTrigger(nextTimestamp, null, null));
            jobctx.setJobHandle(handle);
        }
    }
}
Also used : JobHandle(org.drools.core.time.JobHandle) JobContext(org.drools.core.time.JobContext) PointInTimeTrigger(org.drools.core.time.impl.PointInTimeTrigger) TimerService(org.drools.core.time.TimerService)

Aggregations

JobContext (org.drools.core.time.JobContext)4 Date (java.util.Date)2 Job (org.drools.core.time.Job)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Timers (org.drools.core.marshalling.impl.ProtobufMessages.Timers)1 Timer (org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1 JobHandle (org.drools.core.time.JobHandle)1 SelfRemovalJobContext (org.drools.core.time.SelfRemovalJobContext)1 TimerService (org.drools.core.time.TimerService)1 PointInTimeTrigger (org.drools.core.time.impl.PointInTimeTrigger)1 TimerJobInstance (org.drools.core.time.impl.TimerJobInstance)1