Search in sources :

Example 1 with Timeline

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());
    }
}
Also used : Timeline(org.qi4j.library.scheduler.timeline.Timeline) SchedulerConfiguration(org.qi4j.library.scheduler.SchedulerConfiguration) EntityDeclaration(org.qi4j.bootstrap.EntityDeclaration) ServiceDeclaration(org.qi4j.bootstrap.ServiceDeclaration) TimelineScheduleMixin(org.qi4j.library.scheduler.timeline.TimelineScheduleMixin) ValueDeclaration(org.qi4j.bootstrap.ValueDeclaration)

Example 2 with Timeline

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));
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Timeline(org.qi4j.library.scheduler.timeline.Timeline) Usecase(org.qi4j.api.usecase.Usecase) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.junit.Test)

Aggregations

Timeline (org.qi4j.library.scheduler.timeline.Timeline)2 DateTime (org.joda.time.DateTime)1 Interval (org.joda.time.Interval)1 Test (org.junit.Test)1 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)1 Usecase (org.qi4j.api.usecase.Usecase)1 EntityDeclaration (org.qi4j.bootstrap.EntityDeclaration)1 ServiceDeclaration (org.qi4j.bootstrap.ServiceDeclaration)1 ValueDeclaration (org.qi4j.bootstrap.ValueDeclaration)1 SchedulerConfiguration (org.qi4j.library.scheduler.SchedulerConfiguration)1 TimelineScheduleMixin (org.qi4j.library.scheduler.timeline.TimelineScheduleMixin)1