mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Simplify programmatic scheduling of cron tasks with time zone
This commit adds an overloaded `addCronTask()` method to `ScheduledTaskRegistrar` that allows simpler scheduling of cron tasks with non-default time zones. Closes gh-36556 Signed-off-by: Vedran Pavic <vedran@vedranpavic.com>
This commit is contained in:
+18
-1
@@ -18,6 +18,7 @@ package org.springframework.scheduling.config;
|
||||
|
||||
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 @@ import org.springframework.util.CollectionUtils;
|
||||
* @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 class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* <p>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 class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a {@link Runnable} task to be triggered per the given cron {@code expression}
|
||||
* and time zone.
|
||||
* <p>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
|
||||
|
||||
+8
@@ -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 @@ import static org.mockito.Mockito.mock;
|
||||
* @author Tobias Montagna-Hay
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @author Vedran Pavic
|
||||
* @since 4.2
|
||||
*/
|
||||
class ScheduledTaskRegistrarTests {
|
||||
@@ -82,6 +84,12 @@ class ScheduledTaskRegistrarTests {
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user