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()));
}
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());
}
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());
}
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());
}
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);
}
}
}
Aggregations