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