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 ExpireJobContextTimerInputMarshaller method read.
public void read(ProtobufMarshallerReaderContext inCtx) throws IOException, ClassNotFoundException {
InternalFactHandle factHandle = inCtx.getHandles().get(inCtx.readLong());
long nextTimeStamp = inCtx.readLong();
TimerService clock = inCtx.getWorkingMemory().getTimerService();
JobContext jobctx = new ExpireJobContext(new WorkingMemoryReteExpireAction((EventFactHandle) factHandle), inCtx.getWorkingMemory());
JobHandle handle = clock.scheduleJob(job, jobctx, PointInTimeTrigger.createPointInTimeTrigger(nextTimeStamp, 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);
}
}
}
}
use of org.drools.core.time.TimerService in project jbpm by kiegroup.
the class GlobalQuartzDBTimerServiceTest method testContinueGlobalTestService.
/**
* Test that illustrates that jobs are persisted and survives server restart
* and as soon as GlobalTimerService is active jobs are fired
* NOTE: this test is disabled by default as it requires real db (not in memory)
* and test to be executed separately each with new jvm process
*/
@Test
@Ignore
public void testContinueGlobalTestService() throws Exception {
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycle2.bpmn2"), ResourceType.BPMN2).addConfiguration("drools.timerService", "org.jbpm.process.core.timer.impl.RegisteredTimerServiceDelegate").get();
RuntimeManager manger = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
// build GlobalTimerService instance
TimerService globalTs = new GlobalTimerService(manger, globalScheduler);
// and register it in the registry under 'default' key
TimerServiceRegistry.getInstance().registerTimerService("default", globalTs);
// prepare listener to assert results
final List<Long> timerExporations = new ArrayList<Long>();
ProcessEventListener listener = new DefaultProcessEventListener() {
@Override
public void afterNodeLeft(ProcessNodeLeftEvent event) {
if (event.getNodeInstance().getNodeName().equals("timer")) {
timerExporations.add(event.getProcessInstance().getId());
}
}
};
Thread.sleep(5000);
}
Aggregations