use of org.qi4j.library.scheduler.timeline.Timeline in project qi4j-sdk by Qi4j.
the class SchedulerAssembler method assemble.
@Override
public void assemble(ModuleAssembly assembly) throws AssemblyException {
assembly.services(ScheduleFactory.class);
assembly.entities(Schedules.class);
EntityDeclaration scheduleEntities = assembly.entities(CronSchedule.class, OnceSchedule.class);
ValueDeclaration scheduleValues = assembly.values(CronSchedule.class, OnceSchedule.class);
ServiceDeclaration schedulerDeclaration = assembly.services(SchedulerService.class).visibleIn(visibility()).instantiateOnStartup();
if (timeline) {
scheduleEntities.withTypes(Timeline.class).withMixins(TimelineScheduleMixin.class).withConcerns(TimelineForScheduleConcern.class);
scheduleValues.withTypes(Timeline.class).withMixins(TimelineScheduleMixin.class).withConcerns(TimelineForScheduleConcern.class);
// Internal
assembly.values(TimelineRecord.class);
schedulerDeclaration.withTypes(Timeline.class).withMixins(TimelineSchedulerServiceMixin.class);
}
if (hasConfig()) {
configModule().entities(SchedulerConfiguration.class).visibleIn(configVisibility());
}
}
use of org.qi4j.library.scheduler.timeline.Timeline in project qi4j-sdk by Qi4j.
the class SchedulerTest method testMinutely.
@Test
public void testMinutely() throws UnitOfWorkCompletionException {
Usecase usecase = UsecaseBuilder.newUsecase("TestMinutely");
DateTime start = new DateTime();
String taskIdentity;
long sleepMillis;
try (UnitOfWork uow = module.newUnitOfWork(usecase)) {
Scheduler scheduler = module.findService(Scheduler.class).get();
FooTask task = createFooTask(uow, usecase.name(), BAZAR);
taskIdentity = task.identity().get();
DateTime expectedRun = start.withMillisOfSecond(0).withSecondOfMinute(0).plusMinutes(1);
scheduler.scheduleCron(task, "@minutely", true);
uow.complete();
sleepMillis = new Interval(start, expectedRun).toDurationMillis();
LOGGER.info("Task scheduled on {} to be run at {}", start.getMillis(), expectedRun.getMillis());
}
await(usecase.name()).atMost(sleepMillis + 5000, MILLISECONDS).until(taskOutput(taskIdentity), equalTo(BAR));
try (UnitOfWork uow = module.newUnitOfWork(usecase)) {
Timeline timeline = module.findService(Timeline.class).get();
DateTime now = new DateTime();
// Queries returning past records
assertThat(count(timeline.getLastRecords(5)), is(2L));
assertThat(count(timeline.getRecords(start.getMillis(), now.getMillis())), is(2L));
// Queries returning future records
assertThat(count(timeline.getNextRecords(4)), is(4L));
assertThat(count(timeline.getRecords(now.getMillis() + 100, now.plusMinutes(5).getMillis())), is(5L));
// Queries returning mixed past and future records
assertThat(count(timeline.getRecords(start.getMillis(), now.plusMinutes(5).getMillis())), is(7L));
}
}
Aggregations