use of org.drools.core.time.TimerService 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());
}
use of org.drools.core.time.TimerService in project drools by kiegroup.
the class JDKTimerServiceTest method testSingleExecutionJob.
@Test
public void testSingleExecutionJob() throws Exception {
SessionConfiguration config = SessionConfiguration.newInstance();
config.setClockType(ClockType.REALTIME_CLOCK);
TimerService timeService = TimerServiceFactory.getTimerService(config);
Trigger trigger = new DelayedTrigger(100);
HelloWorldJobContext ctx = new HelloWorldJobContext("hello world", timeService);
timeService.scheduleJob(new HelloWorldJob(), ctx, trigger);
Thread.sleep(500);
timeService.shutdown();
assertEquals(1, ctx.getList().size());
}
use of org.drools.core.time.TimerService in project drools by kiegroup.
the class SlidingTimeWindow method expireFacts.
public void expireFacts(final Object context, final PropagationContext pctx, final InternalWorkingMemory workingMemory) {
TimerService clock = workingMemory.getTimerService();
long currentTime = clock.getCurrentTime();
SlidingTimeWindowContext queue = (SlidingTimeWindowContext) context;
EventFactHandle handle = queue.peek();
while (handle != null && isExpired(currentTime, handle)) {
queue.setExpiringHandle(handle);
queue.remove();
if (handle.isValid()) {
// if not expired yet, expire it
final PropagationContext expiresPctx = createPropagationContextForFact(workingMemory, handle, PropagationContext.Type.EXPIRATION);
ObjectTypeNode.doRetractObject(handle, expiresPctx, workingMemory);
}
queue.setExpiringHandle(null);
handle = queue.peek();
}
// update next expiration time
updateNextExpiration(handle, workingMemory, queue, nodeId);
}
use of org.drools.core.time.TimerService 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);
}
}
}
use of org.drools.core.time.TimerService in project jbpm by kiegroup.
the class TaskDeadlinesServiceImpl method unschedule.
public void unschedule(long taskId, DeadlineType type) {
Task task = persistenceContext.findTask(taskId);
String deploymentId = task.getTaskData().getDeploymentId();
Deadlines deadlines = ((InternalTask) task).getDeadlines();
TimerService timerService = TimerServiceRegistry.getInstance().get(deploymentId + TimerServiceRegistry.TIMER_SERVICE_SUFFIX);
if (timerService != null && timerService instanceof GlobalTimerService) {
if (type == DeadlineType.START) {
List<Deadline> startDeadlines = deadlines.getStartDeadlines();
List<DeadlineSummary> resultList = (List<DeadlineSummary>) persistenceContext.queryWithParametersInTransaction("UnescalatedStartDeadlinesByTaskId", persistenceContext.addParametersToMap("taskId", taskId), ClassUtil.<List<DeadlineSummary>>castClass(List.class));
for (DeadlineSummary summary : resultList) {
TaskDeadlineJob deadlineJob = new TaskDeadlineJob(summary.getTaskId(), summary.getDeadlineId(), DeadlineType.START, deploymentId, task.getTaskData().getProcessInstanceId());
logger.debug("unscheduling timer job for deadline {} {} and task {} using timer service {}", deadlineJob.getId(), summary.getDeadlineId(), taskId, timerService);
JobHandle jobHandle = jobHandles.remove(deadlineJob.getId());
if (jobHandle == null) {
jobHandle = ((GlobalTimerService) timerService).buildJobHandleForContext(new TaskDeadlineJobContext(deadlineJob.getId(), task.getTaskData().getProcessInstanceId(), deploymentId));
}
timerService.removeJob(jobHandle);
// mark the deadlines so they won't be rescheduled again
for (Deadline deadline : startDeadlines) {
if (deadline.getId() == summary.getDeadlineId()) {
deadline.setEscalated(true);
}
}
}
} else if (type == DeadlineType.END) {
List<Deadline> endDeadlines = deadlines.getStartDeadlines();
List<DeadlineSummary> resultList = (List<DeadlineSummary>) persistenceContext.queryWithParametersInTransaction("UnescalatedEndDeadlinesByTaskId", persistenceContext.addParametersToMap("taskId", taskId), ClassUtil.<List<DeadlineSummary>>castClass(List.class));
for (DeadlineSummary summary : resultList) {
TaskDeadlineJob deadlineJob = new TaskDeadlineJob(summary.getTaskId(), summary.getDeadlineId(), DeadlineType.END, deploymentId, task.getTaskData().getProcessInstanceId());
logger.debug("unscheduling timer job for deadline {} and task {} using timer service {}", deadlineJob.getId(), taskId, timerService);
JobHandle jobHandle = jobHandles.remove(deadlineJob.getId());
if (jobHandle == null) {
jobHandle = ((GlobalTimerService) timerService).buildJobHandleForContext(new TaskDeadlineJobContext(deadlineJob.getId(), task.getTaskData().getProcessInstanceId(), deploymentId));
}
timerService.removeJob(jobHandle);
// mark the deadlines so they won't be rescheduled again
for (Deadline deadline : endDeadlines) {
if (deadline.getId() == summary.getDeadlineId()) {
deadline.setEscalated(true);
}
}
}
}
} else {
List<ScheduledFuture<ScheduledTaskDeadline>> knownFutures = null;
if (type == DeadlineType.START) {
knownFutures = startScheduledTaskDeadlines.get(taskId);
} else if (type == DeadlineType.END) {
knownFutures = endScheduledTaskDeadlines.get(taskId);
}
if (knownFutures == null) {
return;
}
Iterator<ScheduledFuture<ScheduledTaskDeadline>> it = knownFutures.iterator();
while (it.hasNext()) {
ScheduledFuture<ScheduledTaskDeadline> scheduled = it.next();
try {
if (!scheduled.isDone() && !scheduled.isCancelled()) {
scheduled.cancel(true);
}
} catch (Exception e) {
logger.error("Error while cancelling scheduled deadline task for Task with id {} -> {}", taskId, e);
}
}
}
}
Aggregations