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();
}
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();
}
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;
}
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);
}
}
}
Aggregations