diff --git a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java index dcb8826f4507..85f650bfb4d9 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java +++ b/spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java @@ -18,6 +18,7 @@ import java.time.Duration; import java.time.Instant; +import java.time.ZoneId; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -56,6 +57,7 @@ * @author Sam Brannen * @author Arjen Poutsma * @author Brian Clozel + * @author Vedran Pavic * @since 3.0 * @see org.springframework.scheduling.annotation.EnableAsync * @see org.springframework.scheduling.annotation.SchedulingConfigurer @@ -284,9 +286,11 @@ public void addTriggerTask(TriggerTask task) { } /** - * Add a {@link Runnable} task to be triggered per the given cron {@code expression}. + * Add a {@link Runnable} task to be triggered per the given cron {@code expression} + * in the default time zone. *

This method will not register the task if the {@code expression} is * equal to {@link #CRON_DISABLED}. + * @see CronTask */ public void addCronTask(Runnable task, String expression) { if (!CRON_DISABLED.equals(expression)) { @@ -294,6 +298,19 @@ public void addCronTask(Runnable task, String expression) { } } + /** + * Add a {@link Runnable} task to be triggered per the given cron {@code expression} + * and time zone. + *

This method will not register the task if the {@code expression} is + * equal to {@link #CRON_DISABLED}. + * @see CronTask + */ + public void addCronTask(Runnable task, String expression, ZoneId zoneId) { + if (!CRON_DISABLED.equals(expression)) { + addCronTask(new CronTask(task, new CronTrigger(expression, zoneId))); + } + } + /** * Add a {@link CronTask}. * @since 3.2 diff --git a/spring-context/src/test/java/org/springframework/scheduling/config/ScheduledTaskRegistrarTests.java b/spring-context/src/test/java/org/springframework/scheduling/config/ScheduledTaskRegistrarTests.java index 0076bbe0f365..7d79070e9f8e 100644 --- a/spring-context/src/test/java/org/springframework/scheduling/config/ScheduledTaskRegistrarTests.java +++ b/spring-context/src/test/java/org/springframework/scheduling/config/ScheduledTaskRegistrarTests.java @@ -16,6 +16,7 @@ package org.springframework.scheduling.config; +import java.time.ZoneId; import java.util.Collections; import org.junit.jupiter.api.BeforeEach; @@ -31,6 +32,7 @@ * @author Tobias Montagna-Hay * @author Juergen Hoeller * @author Sam Brannen + * @author Vedran Pavic * @since 4.2 */ class ScheduledTaskRegistrarTests { @@ -82,6 +84,12 @@ void addCronTaskWithValidExpression() { assertThat(this.taskRegistrar.getCronTaskList()).hasSize(1); } + @Test + void addCronTaskWithValidExpressionAndZoneId() { + this.taskRegistrar.addCronTask(no_op, "* * * * * ?", ZoneId.of("Europe/London")); + assertThat(this.taskRegistrar.getCronTaskList()).hasSize(1); + } + @Test void addCronTaskWithInvalidExpression() { assertThatIllegalArgumentException()