Merge branch '3.1.x'

Closes gh-36130
pull/35874/head
Andy Wilkinson 1 year ago
commit ea73203219

@ -17,6 +17,7 @@
package org.springframework.boot.actuate.scheduling; package org.springframework.boot.actuate.scheduling;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.time.Duration;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -186,7 +187,8 @@ public class ScheduledTasksEndpoint {
protected IntervalTaskDescriptor(TaskType type, TriggerTask task, PeriodicTrigger trigger) { protected IntervalTaskDescriptor(TaskType type, TriggerTask task, PeriodicTrigger trigger) {
super(type, task.getRunnable()); super(type, task.getRunnable());
this.initialDelay = trigger.getInitialDelayDuration().toMillis(); Duration initialDelayDuration = trigger.getInitialDelayDuration();
this.initialDelay = (initialDelayDuration != null) ? initialDelayDuration.toMillis() : 0;
this.interval = trigger.getPeriodDuration().toMillis(); this.interval = trigger.getPeriodDuration().toMillis();
} }

@ -113,6 +113,20 @@ class ScheduledTasksEndpointTests {
}); });
} }
@Test
void noInitialDelayFixedDelayTriggerIsReported() {
run(NoInitialDelayFixedDelayTriggerTask.class, (tasks) -> {
assertThat(tasks.getCron()).isEmpty();
assertThat(tasks.getFixedRate()).isEmpty();
assertThat(tasks.getCustom()).isEmpty();
assertThat(tasks.getFixedDelay()).hasSize(1);
FixedDelayTaskDescriptor description = (FixedDelayTaskDescriptor) tasks.getFixedDelay().get(0);
assertThat(description.getInitialDelay()).isEqualTo(0);
assertThat(description.getInterval()).isEqualTo(1000);
assertThat(description.getRunnable().getTarget()).isEqualTo(FixedDelayTriggerRunnable.class.getName());
});
}
@Test @Test
void fixedRateScheduledMethodIsReported() { void fixedRateScheduledMethodIsReported() {
run(FixedRateScheduledMethod.class, (tasks) -> { run(FixedRateScheduledMethod.class, (tasks) -> {
@ -142,6 +156,20 @@ class ScheduledTasksEndpointTests {
}); });
} }
@Test
void noInitialDelayFixedRateTriggerIsReported() {
run(NoInitialDelayFixedRateTriggerTask.class, (tasks) -> {
assertThat(tasks.getCron()).isEmpty();
assertThat(tasks.getFixedDelay()).isEmpty();
assertThat(tasks.getCustom()).isEmpty();
assertThat(tasks.getFixedRate()).hasSize(1);
FixedRateTaskDescriptor description = (FixedRateTaskDescriptor) tasks.getFixedRate().get(0);
assertThat(description.getInitialDelay()).isEqualTo(0);
assertThat(description.getInterval()).isEqualTo(2000);
assertThat(description.getRunnable().getTarget()).isEqualTo(FixedRateTriggerRunnable.class.getName());
});
}
@Test @Test
void taskWithCustomTriggerIsReported() { void taskWithCustomTriggerIsReported() {
run(CustomTriggerTask.class, (tasks) -> { run(CustomTriggerTask.class, (tasks) -> {
@ -223,6 +251,16 @@ class ScheduledTasksEndpointTests {
} }
static class NoInitialDelayFixedDelayTriggerTask implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
PeriodicTrigger trigger = new PeriodicTrigger(Duration.ofSeconds(1));
taskRegistrar.addTriggerTask(new FixedDelayTriggerRunnable(), trigger);
}
}
static class FixedRateTriggerTask implements SchedulingConfigurer { static class FixedRateTriggerTask implements SchedulingConfigurer {
@Override @Override
@ -235,6 +273,17 @@ class ScheduledTasksEndpointTests {
} }
static class NoInitialDelayFixedRateTriggerTask implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
PeriodicTrigger trigger = new PeriodicTrigger(Duration.ofSeconds(2));
trigger.setFixedRate(true);
taskRegistrar.addTriggerTask(new FixedRateTriggerRunnable(), trigger);
}
}
static class CronTriggerTask implements SchedulingConfigurer { static class CronTriggerTask implements SchedulingConfigurer {
@Override @Override

Loading…
Cancel
Save