Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -284,16 +286,31 @@ 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.
* <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)) {
addCronTask(new CronTask(task, expression));
}
}

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.scheduling.config;

import java.time.ZoneId;
import java.util.Collections;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -31,6 +32,7 @@
* @author Tobias Montagna-Hay
* @author Juergen Hoeller
* @author Sam Brannen
* @author Vedran Pavic
* @since 4.2
*/
class ScheduledTaskRegistrarTests {
Expand Down Expand Up @@ -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()
Expand Down