Search in sources :

Example 6 with IntervalTask

use of org.springframework.scheduling.config.IntervalTask in project arch-playground by BeneStem.

the class JobWatcher method registerFixedDelay.

private void registerFixedDelay(final String jobType, final Duration fixedDelay, final ScheduledTaskRegistrar taskRegistrar) {
    LOG.info("register job {} for scheduling with fixed delay: {}", jobType, fixedDelay);
    taskRegistrar.addFixedDelayTask(new IntervalTask(() -> jobService.startAsyncJob(jobType), fixedDelay.toMillis(), fixedDelay.toMillis()));
}
Also used : IntervalTask(org.springframework.scheduling.config.IntervalTask)

Example 7 with IntervalTask

use of org.springframework.scheduling.config.IntervalTask in project spring-framework by spring-projects.

the class ScheduledAnnotationBeanPostProcessorTests method composedAnnotationWithInitialDelayAndFixedRate.

@Test
public void composedAnnotationWithInitialDelayAndFixedRate() {
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition targetDefinition = new RootBeanDefinition(ComposedAnnotationFixedRateTestBean.class);
    context.registerBeanDefinition("postProcessor", processorDefinition);
    context.registerBeanDefinition("target", targetDefinition);
    context.refresh();
    Object postProcessor = context.getBean("postProcessor");
    Object target = context.getBean("target");
    ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
    @SuppressWarnings("unchecked") List<IntervalTask> fixedRateTasks = (List<IntervalTask>) new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
    assertEquals(1, fixedRateTasks.size());
    IntervalTask task = fixedRateTasks.get(0);
    ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
    Object targetObject = runnable.getTarget();
    Method targetMethod = runnable.getMethod();
    assertEquals(target, targetObject);
    assertEquals("checkForUpdates", targetMethod.getName());
    assertEquals(5000L, task.getInterval());
    assertEquals(1000L, task.getInitialDelay());
}
Also used : ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) IntervalTask(org.springframework.scheduling.config.IntervalTask) ScheduledTaskRegistrar(org.springframework.scheduling.config.ScheduledTaskRegistrar) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) List(java.util.List) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 8 with IntervalTask

use of org.springframework.scheduling.config.IntervalTask in project spring-framework by spring-projects.

the class ScheduledAnnotationBeanPostProcessorTests method fixedRateTaskWithInitialDelay.

@Test
public void fixedRateTaskWithInitialDelay() {
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition targetDefinition = new RootBeanDefinition(FixedRateWithInitialDelayTestBean.class);
    context.registerBeanDefinition("postProcessor", processorDefinition);
    context.registerBeanDefinition("target", targetDefinition);
    context.refresh();
    Object postProcessor = context.getBean("postProcessor");
    Object target = context.getBean("target");
    ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
    @SuppressWarnings("unchecked") List<IntervalTask> fixedRateTasks = (List<IntervalTask>) new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
    assertEquals(1, fixedRateTasks.size());
    IntervalTask task = fixedRateTasks.get(0);
    ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
    Object targetObject = runnable.getTarget();
    Method targetMethod = runnable.getMethod();
    assertEquals(target, targetObject);
    assertEquals("fixedRate", targetMethod.getName());
    assertEquals(1000L, task.getInitialDelay());
    assertEquals(3000L, task.getInterval());
}
Also used : ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) IntervalTask(org.springframework.scheduling.config.IntervalTask) ScheduledTaskRegistrar(org.springframework.scheduling.config.ScheduledTaskRegistrar) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) List(java.util.List) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 9 with IntervalTask

use of org.springframework.scheduling.config.IntervalTask in project spring-framework by spring-projects.

the class ScheduledAnnotationBeanPostProcessorTests method severalFixedRates.

private void severalFixedRates(StaticApplicationContext context, BeanDefinition processorDefinition, BeanDefinition targetDefinition) {
    context.registerBeanDefinition("postProcessor", processorDefinition);
    context.registerBeanDefinition("target", targetDefinition);
    context.refresh();
    Object postProcessor = context.getBean("postProcessor");
    Object target = context.getBean("target");
    ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
    @SuppressWarnings("unchecked") List<IntervalTask> fixedRateTasks = (List<IntervalTask>) new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
    assertEquals(2, fixedRateTasks.size());
    IntervalTask task1 = fixedRateTasks.get(0);
    ScheduledMethodRunnable runnable1 = (ScheduledMethodRunnable) task1.getRunnable();
    Object targetObject = runnable1.getTarget();
    Method targetMethod = runnable1.getMethod();
    assertEquals(target, targetObject);
    assertEquals("fixedRate", targetMethod.getName());
    assertEquals(0, task1.getInitialDelay());
    assertEquals(4000L, task1.getInterval());
    IntervalTask task2 = fixedRateTasks.get(1);
    ScheduledMethodRunnable runnable2 = (ScheduledMethodRunnable) task2.getRunnable();
    targetObject = runnable2.getTarget();
    targetMethod = runnable2.getMethod();
    assertEquals(target, targetObject);
    assertEquals("fixedRate", targetMethod.getName());
    assertEquals(2000L, task2.getInitialDelay());
    assertEquals(4000L, task2.getInterval());
}
Also used : ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) IntervalTask(org.springframework.scheduling.config.IntervalTask) ScheduledTaskRegistrar(org.springframework.scheduling.config.ScheduledTaskRegistrar) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) List(java.util.List) Method(java.lang.reflect.Method)

Example 10 with IntervalTask

use of org.springframework.scheduling.config.IntervalTask in project spring-boot by spring-projects.

the class MetricExporters method configureTasks.

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    for (Entry<String, Exporter> entry : this.exporters.entrySet()) {
        String name = entry.getKey();
        Exporter exporter = entry.getValue();
        TriggerProperties trigger = this.properties.findTrigger(name);
        if (trigger != null) {
            ExportRunner runner = new ExportRunner(exporter);
            IntervalTask task = new IntervalTask(runner, trigger.getDelayMillis(), trigger.getDelayMillis());
            taskRegistrar.addFixedDelayTask(task);
        }
    }
    for (Entry<String, GaugeWriter> entry : this.writers.entrySet()) {
        String name = entry.getKey();
        GaugeWriter writer = entry.getValue();
        TriggerProperties trigger = this.properties.findTrigger(name);
        if (trigger != null) {
            MetricCopyExporter exporter = getExporter(writer, trigger);
            this.exporters.put(name, exporter);
            this.closeables.add(name);
            ExportRunner runner = new ExportRunner(exporter);
            IntervalTask task = new IntervalTask(runner, trigger.getDelayMillis(), trigger.getDelayMillis());
            taskRegistrar.addFixedDelayTask(task);
        }
    }
}
Also used : IntervalTask(org.springframework.scheduling.config.IntervalTask) GaugeWriter(org.springframework.boot.actuate.metrics.writer.GaugeWriter)

Aggregations

IntervalTask (org.springframework.scheduling.config.IntervalTask)10 Method (java.lang.reflect.Method)8 List (java.util.List)8 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)8 ScheduledTaskRegistrar (org.springframework.scheduling.config.ScheduledTaskRegistrar)8 ScheduledMethodRunnable (org.springframework.scheduling.support.ScheduledMethodRunnable)8 Test (org.junit.Test)7 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 Properties (java.util.Properties)2 GaugeWriter (org.springframework.boot.actuate.metrics.writer.GaugeWriter)1