Skip to content

Commit

Permalink
Introduce configuration to diable @scheduled tasks
Browse files Browse the repository at this point in the history
Prior to this commit, only cron @scheduled tasks can be disabled with the
special cron expression value that indicates a disabled trigger: "-".

This commit enables this configuration to fixedDelay and fixedRate @scheduled
tasks.

Fixes spring-projectsgh-28073
  • Loading branch information
Leewei222 committed Aug 23, 2023
1 parent baf3678 commit 1fb42b8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,15 @@ private void processScheduledTask(Scheduled scheduled, Runnable runnable, Method
if (StringUtils.hasLength(fixedDelayString)) {
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
try {
fixedDelay = toDuration(fixedDelayString, scheduled.timeUnit());
}
catch (RuntimeException ex) {
throw new IllegalArgumentException(
"Invalid fixedDelayString value \"" + fixedDelayString + "\" - cannot parse into long");
if (!Scheduled.CRON_DISABLED.equals(fixedDelayString)) {
try {
fixedDelay = toDuration(fixedDelayString, scheduled.timeUnit());
} catch (RuntimeException ex) {
throw new IllegalArgumentException(
"Invalid fixedDelayString value \"" + fixedDelayString + "\" - cannot parse into long");
}
tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay)));
}
tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay)));
}
}

Expand All @@ -488,14 +489,15 @@ private void processScheduledTask(Scheduled scheduled, Runnable runnable, Method
if (StringUtils.hasLength(fixedRateString)) {
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
try {
fixedRate = toDuration(fixedRateString, scheduled.timeUnit());
}
catch (RuntimeException ex) {
throw new IllegalArgumentException(
"Invalid fixedRateString value \"" + fixedRateString + "\" - cannot parse into long");
if (!Scheduled.CRON_DISABLED.equals(fixedRateString)) {
try {
fixedRate = toDuration(fixedRateString, scheduled.timeUnit());
} catch (RuntimeException ex) {
throw new IllegalArgumentException(
"Invalid fixedRateString value \"" + fixedRateString + "\" - cannot parse into long");
}
tasks.add(this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay)));
}
tasks.add(this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,23 @@ void propertyPlaceholderWithInactiveCron() {
assertThat(postProcessor.getScheduledTasks().isEmpty()).isTrue();
}

@ParameterizedTest
@CsvSource(textBlock = """
InactiveCron
InactiveFixedDelay
InactiveFixedRate
""")
void inactiveTask(@NameToClass Class<?> beanClass) {
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
BeanDefinition targetDefinition = new RootBeanDefinition(beanClass);
context.registerBeanDefinition("postProcessor", processorDefinition);
context.registerBeanDefinition("target", targetDefinition);
context.refresh();

ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
assertThat(postProcessor.getScheduledTasks().isEmpty()).isTrue();
}

@ParameterizedTest
@CsvSource(textBlock = """
PropertyPlaceholderWithFixedDelay, 5000, 1000, 5_000, 1_000
Expand Down Expand Up @@ -1052,4 +1069,25 @@ public Class<?> convert(Object beanClassName, ParameterContext context) throws A
}
}

static class InactiveCron {

@Scheduled(cron = "-")
void inactive() {
}
}

static class InactiveFixedDelay {

@Scheduled(fixedDelayString = "-")
void inactive() {
}
}

static class InactiveFixedRate {

@Scheduled(fixedRateString = "-")
void inactive() {
}
}

}

0 comments on commit 1fb42b8

Please sign in to comment.