Update scheduling package to use java.time

This commit deprecates all methods in org.springframework.scheduling
that use

- Date, in favor of variants that take an Instant.
- long & TimeUnit, in favor of variants that take a Duration.

Closes: gh-28714
This commit is contained in:
Arjen Poutsma
2022-07-05 10:32:47 +02:00
parent 8ccf05adee
commit 9b739a2310
38 changed files with 791 additions and 412 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -89,11 +89,8 @@ public interface TaskScheduler {
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
* @since 5.0
* @see #schedule(Runnable, Date)
*/
default ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
return schedule(task, Date.from(startTime));
}
ScheduledFuture<?> schedule(Runnable task, Instant startTime);
/**
* Schedule the given {@link Runnable}, invoking it at the specified execution time.
@@ -105,8 +102,12 @@ public interface TaskScheduler {
* @return a {@link ScheduledFuture} representing pending completion of the task
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
* @deprecated as of 6.0, in favor of {@link #schedule(Runnable, Instant)}
*/
ScheduledFuture<?> schedule(Runnable task, Date startTime);
@Deprecated
default ScheduledFuture<?> schedule(Runnable task, Date startTime) {
return schedule(task, startTime.toInstant());
}
/**
* Schedule the given {@link Runnable}, invoking it at the specified execution time
@@ -121,11 +122,8 @@ public interface TaskScheduler {
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
* @since 5.0
* @see #scheduleAtFixedRate(Runnable, Date, long)
*/
default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period) {
return scheduleAtFixedRate(task, Date.from(startTime), period.toMillis());
}
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period);
/**
* Schedule the given {@link Runnable}, invoking it at the specified execution time
@@ -139,8 +137,12 @@ public interface TaskScheduler {
* @return a {@link ScheduledFuture} representing pending completion of the task
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
* @deprecated as of 6.0, in favor of {@link #scheduleAtFixedRate(Runnable, Instant, Duration)}
*/
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period);
@Deprecated
default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) {
return scheduleAtFixedRate(task, startTime.toInstant(), Duration.ofMillis(period));
}
/**
* Schedule the given {@link Runnable}, starting as soon as possible and
@@ -153,11 +155,8 @@ public interface TaskScheduler {
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
* @since 5.0
* @see #scheduleAtFixedRate(Runnable, long)
*/
default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
return scheduleAtFixedRate(task, period.toMillis());
}
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period);
/**
* Schedule the given {@link Runnable}, starting as soon as possible and
@@ -169,8 +168,12 @@ public interface TaskScheduler {
* @return a {@link ScheduledFuture} representing pending completion of the task
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
* @deprecated as of 6.0, in favor of {@link #scheduleAtFixedRate(Runnable, Duration)}
*/
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period);
@Deprecated
default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {
return scheduleAtFixedRate(task, Duration.ofMillis(period));
}
/**
* Schedule the given {@link Runnable}, invoking it at the specified execution time
@@ -186,11 +189,8 @@ public interface TaskScheduler {
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
* @since 5.0
* @see #scheduleWithFixedDelay(Runnable, Date, long)
*/
default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay) {
return scheduleWithFixedDelay(task, Date.from(startTime), delay.toMillis());
}
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay);
/**
* Schedule the given {@link Runnable}, invoking it at the specified execution time
@@ -206,8 +206,12 @@ public interface TaskScheduler {
* @return a {@link ScheduledFuture} representing pending completion of the task
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
* @deprecated as of 6.0, in favor of {@link #scheduleWithFixedDelay(Runnable, Instant, Duration)}
*/
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay);
@Deprecated
default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
return scheduleWithFixedDelay(task, startTime.toInstant(), Duration.ofMillis(delay));
}
/**
* Schedule the given {@link Runnable}, starting as soon as possible and invoking it with
@@ -220,11 +224,8 @@ public interface TaskScheduler {
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
* @since 5.0
* @see #scheduleWithFixedDelay(Runnable, long)
*/
default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay) {
return scheduleWithFixedDelay(task, delay.toMillis());
}
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay);
/**
* Schedule the given {@link Runnable}, starting as soon as possible and invoking it with
@@ -237,7 +238,11 @@ public interface TaskScheduler {
* @return a {@link ScheduledFuture} representing pending completion of the task
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
* @deprecated as of 6.0, in favor of {@link #scheduleWithFixedDelay(Runnable, Duration)}
*/
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay);
@Deprecated
default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {
return scheduleWithFixedDelay(task, Duration.ofMillis(delay));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.scheduling;
import java.time.Instant;
import java.util.Date;
import org.springframework.lang.Nullable;
@@ -37,8 +38,24 @@ public interface Trigger {
* and last completion time
* @return the next execution time as defined by the trigger,
* or {@code null} if the trigger won't fire anymore
* @deprecated as of 6.0, in favor of {@link #nextExecution(TriggerContext)}
*/
@Deprecated
@Nullable
default Date nextExecutionTime(TriggerContext triggerContext) {
Instant instant = nextExecution(triggerContext);
return instant != null ? Date.from(instant) : null;
}
/**
* Determine the next execution time according to the given trigger context.
* @param triggerContext context object encapsulating last execution times
* and last completion time
* @return the next execution time as defined by the trigger,
* or {@code null} if the trigger won't fire anymore
* @since 6.0
*/
@Nullable
Date nextExecutionTime(TriggerContext triggerContext);
Instant nextExecution(TriggerContext triggerContext);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.scheduling;
import java.time.Clock;
import java.time.Instant;
import java.util.Date;
import org.springframework.lang.Nullable;
@@ -43,22 +44,59 @@ public interface TriggerContext {
/**
* Return the last <i>scheduled</i> execution time of the task,
* or {@code null} if not scheduled before.
* @deprecated as of 6.0, in favor on {@link #lastScheduledExecution()}
*/
@Nullable
Date lastScheduledExecutionTime();
@Deprecated
default Date lastScheduledExecutionTime() {
Instant instant = lastScheduledExecution();
return instant != null ? Date.from(instant) : null;
}
/**
* Return the last <i>scheduled</i> execution time of the task,
* or {@code null} if not scheduled before.
* @since 6.0
*/
@Nullable
Instant lastScheduledExecution();
/**
* Return the last <i>actual</i> execution time of the task,
* or {@code null} if not scheduled before.
* @deprecated as of 6.0, in favor on {@link #lastActualExecution()}
*/
@Deprecated
@Nullable
default Date lastActualExecutionTime() {
Instant instant = lastActualExecution();
return instant != null ? Date.from(instant) : null;
}
/**
* Return the last <i>actual</i> execution time of the task,
* or {@code null} if not scheduled before.
*/
@Nullable
Date lastActualExecutionTime();
Instant lastActualExecution();
/**
* Return the last completion time of the task,
* or {@code null} if not scheduled before.
* @deprecated as of 6.0, in favor on {@link #lastCompletion()}
*/
@Deprecated
@Nullable
default Date lastCompletionTime() {
Instant instant = lastCompletion();
return instant != null ? Date.from(instant) : null;
}
/**
* Return the last completion time of the task,
* or {@code null} if not scheduled before.
*/
@Nullable
Date lastCompletionTime();
Instant lastCompletion();
}

View File

@@ -405,16 +405,16 @@ public class ScheduledAnnotationBeanPostProcessor
Set<ScheduledTask> tasks = new LinkedHashSet<>(4);
// Determine initial delay
long initialDelay = convertToMillis(scheduled.initialDelay(), scheduled.timeUnit());
Duration initialDelay = toDuration(scheduled.initialDelay(), scheduled.timeUnit());
String initialDelayString = scheduled.initialDelayString();
if (StringUtils.hasText(initialDelayString)) {
Assert.isTrue(initialDelay < 0, "Specify 'initialDelay' or 'initialDelayString', not both");
Assert.isTrue(initialDelay.isNegative(), "Specify 'initialDelay' or 'initialDelayString', not both");
if (this.embeddedValueResolver != null) {
initialDelayString = this.embeddedValueResolver.resolveStringValue(initialDelayString);
}
if (StringUtils.hasLength(initialDelayString)) {
try {
initialDelay = convertToMillis(initialDelayString, scheduled.timeUnit());
initialDelay = toDuration(initialDelayString, scheduled.timeUnit());
}
catch (RuntimeException ex) {
throw new IllegalArgumentException(
@@ -432,7 +432,7 @@ public class ScheduledAnnotationBeanPostProcessor
zone = this.embeddedValueResolver.resolveStringValue(zone);
}
if (StringUtils.hasLength(cron)) {
Assert.isTrue(initialDelay == -1, "'initialDelay' not supported for cron triggers");
Assert.isTrue(initialDelay.isNegative(), "'initialDelay' not supported for cron triggers");
processedSchedule = true;
if (!Scheduled.CRON_DISABLED.equals(cron)) {
TimeZone timeZone;
@@ -448,13 +448,13 @@ public class ScheduledAnnotationBeanPostProcessor
}
// At this point we don't need to differentiate between initial delay set or not anymore
if (initialDelay < 0) {
initialDelay = 0;
if (initialDelay.isNegative()) {
initialDelay = Duration.ZERO;
}
// Check fixed delay
long fixedDelay = convertToMillis(scheduled.fixedDelay(), scheduled.timeUnit());
if (fixedDelay >= 0) {
Duration fixedDelay = toDuration(scheduled.fixedDelay(), scheduled.timeUnit());
if (!fixedDelay.isNegative()) {
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay)));
@@ -469,7 +469,7 @@ public class ScheduledAnnotationBeanPostProcessor
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
try {
fixedDelay = convertToMillis(fixedDelayString, scheduled.timeUnit());
fixedDelay = toDuration(fixedDelayString, scheduled.timeUnit());
}
catch (RuntimeException ex) {
throw new IllegalArgumentException(
@@ -480,8 +480,8 @@ public class ScheduledAnnotationBeanPostProcessor
}
// Check fixed rate
long fixedRate = convertToMillis(scheduled.fixedRate(), scheduled.timeUnit());
if (fixedRate >= 0) {
Duration fixedRate = toDuration(scheduled.fixedRate(), scheduled.timeUnit());
if (!fixedRate.isNegative()) {
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
tasks.add(this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay)));
@@ -495,7 +495,7 @@ public class ScheduledAnnotationBeanPostProcessor
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
try {
fixedRate = convertToMillis(fixedRateString, scheduled.timeUnit());
fixedRate = toDuration(fixedRateString, scheduled.timeUnit());
}
catch (RuntimeException ex) {
throw new IllegalArgumentException(
@@ -535,15 +535,15 @@ public class ScheduledAnnotationBeanPostProcessor
return new ScheduledMethodRunnable(target, invocableMethod);
}
private static long convertToMillis(long value, TimeUnit timeUnit) {
return TimeUnit.MILLISECONDS.convert(value, timeUnit);
private static Duration toDuration(long value, TimeUnit timeUnit) {
return Duration.of(value, timeUnit.toChronoUnit());
}
private static long convertToMillis(String value, TimeUnit timeUnit) {
private static Duration toDuration(String value, TimeUnit timeUnit) {
if (isDurationString(value)) {
return Duration.parse(value).toMillis();
return Duration.parse(value);
}
return convertToMillis(Long.parseLong(value), timeUnit);
return toDuration(Long.parseLong(value), timeUnit);
}
private static boolean isDurationString(String value) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
package org.springframework.scheduling.concurrent;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@@ -32,7 +34,7 @@ import org.springframework.core.task.TaskRejectedException;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.support.SimpleTriggerContext;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.support.TaskUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -58,6 +60,7 @@ import org.springframework.util.ErrorHandler;
*
* @author Juergen Hoeller
* @author Mark Fisher
* @author Arjen Poutsma
* @since 3.0
* @see java.util.concurrent.ScheduledExecutorService
* @see java.util.concurrent.ScheduledThreadPoolExecutor
@@ -206,10 +209,10 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
}
@Override
public ScheduledFuture<?> schedule(Runnable task, Date startTime) {
long initialDelay = startTime.getTime() - this.clock.millis();
public ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
Duration initialDelay = Duration.between(this.clock.instant(), startTime);
try {
return this.scheduledExecutor.schedule(decorateTask(task, false), initialDelay, TimeUnit.MILLISECONDS);
return this.scheduledExecutor.schedule(decorateTask(task, false), initialDelay.toMillis(), TimeUnit.MILLISECONDS);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
@@ -217,10 +220,10 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) {
long initialDelay = startTime.getTime() - this.clock.millis();
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period) {
Duration initialDelay = Duration.between(this.clock.instant(), startTime);
try {
return this.scheduledExecutor.scheduleAtFixedRate(decorateTask(task, true), initialDelay, period, TimeUnit.MILLISECONDS);
return this.scheduledExecutor.scheduleAtFixedRate(decorateTask(task, true), initialDelay.toMillis(), period.toMillis(), TimeUnit.MILLISECONDS);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
@@ -228,9 +231,9 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
try {
return this.scheduledExecutor.scheduleAtFixedRate(decorateTask(task, true), 0, period, TimeUnit.MILLISECONDS);
return this.scheduledExecutor.scheduleAtFixedRate(decorateTask(task, true), 0, period.toMillis(), TimeUnit.MILLISECONDS);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
@@ -238,10 +241,10 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
long initialDelay = startTime.getTime() - this.clock.millis();
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay) {
Duration initialDelay = Duration.between(this.clock.instant(), startTime);
try {
return this.scheduledExecutor.scheduleWithFixedDelay(decorateTask(task, true), initialDelay, delay, TimeUnit.MILLISECONDS);
return this.scheduledExecutor.scheduleWithFixedDelay(decorateTask(task, true), initialDelay.toMillis(), delay.toMillis(), TimeUnit.MILLISECONDS);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
@@ -249,9 +252,9 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay) {
try {
return this.scheduledExecutor.scheduleWithFixedDelay(decorateTask(task, true), 0, delay, TimeUnit.MILLISECONDS);
return this.scheduledExecutor.scheduleWithFixedDelay(decorateTask(task, true), 0, delay.toMillis(), TimeUnit.MILLISECONDS);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + this.scheduledExecutor + "] did not accept task: " + task, ex);
@@ -273,22 +276,66 @@ public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements T
*/
private class EnterpriseConcurrentTriggerScheduler {
public ScheduledFuture<?> schedule(Runnable task, final Trigger trigger) {
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
ManagedScheduledExecutorService executor = (ManagedScheduledExecutorService) scheduledExecutor;
return executor.schedule(task, new jakarta.enterprise.concurrent.Trigger() {
@Override
@Nullable
public Date getNextRunTime(@Nullable LastExecution le, Date taskScheduledTime) {
return (trigger.nextExecutionTime(le != null ?
new SimpleTriggerContext(le.getScheduledStart(), le.getRunStart(), le.getRunEnd()) :
new SimpleTriggerContext()));
}
@Override
public boolean skipRun(LastExecution lastExecution, Date scheduledRunTime) {
return false;
}
});
return executor.schedule(task, new TriggerAdapter(trigger));
}
private static class TriggerAdapter implements jakarta.enterprise.concurrent.Trigger {
private final Trigger adaptee;
public TriggerAdapter(Trigger adaptee) {
this.adaptee = adaptee;
}
@Override
@Nullable
public Date getNextRunTime(@Nullable LastExecution le, Date taskScheduledTime) {
Instant instant = this.adaptee.nextExecution(new LastExecutionAdapter(le));
return instant != null ? Date.from(instant) : null;
}
@Nullable
private static Instant toInstant(@Nullable Date date) {
return date != null ? date.toInstant() : null;
}
@Override
public boolean skipRun(LastExecution lastExecutionInfo, Date scheduledRunTime) {
return false;
}
private static class LastExecutionAdapter implements TriggerContext {
@Nullable
private final LastExecution le;
public LastExecutionAdapter(@Nullable LastExecution le) {
this.le = le;
}
@Override
public Instant lastScheduledExecution() {
return (this.le != null) ? toInstant(this.le.getScheduledStart()) : null;
}
@Override
public Instant lastActualExecution() {
return (this.le != null) ? toInstant(this.le.getRunStart()) : null;
}
@Override
public Instant lastCompletion() {
return (this.le != null) ? toInstant(this.le.getRunEnd()) : null;
}
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,8 @@
package org.springframework.scheduling.concurrent;
import java.time.Clock;
import java.util.Date;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
@@ -56,7 +57,7 @@ class ReschedulingRunnable extends DelegatingErrorHandlingRunnable implements Sc
private ScheduledFuture<?> currentFuture;
@Nullable
private Date scheduledExecutionTime;
private Instant scheduledExecutionTime;
private final Object triggerContextMonitor = new Object();
@@ -74,12 +75,12 @@ class ReschedulingRunnable extends DelegatingErrorHandlingRunnable implements Sc
@Nullable
public ScheduledFuture<?> schedule() {
synchronized (this.triggerContextMonitor) {
this.scheduledExecutionTime = this.trigger.nextExecutionTime(this.triggerContext);
this.scheduledExecutionTime = this.trigger.nextExecution(this.triggerContext);
if (this.scheduledExecutionTime == null) {
return null;
}
long initialDelay = this.scheduledExecutionTime.getTime() - this.triggerContext.getClock().millis();
this.currentFuture = this.executor.schedule(this, initialDelay, TimeUnit.MILLISECONDS);
Duration initialDelay = Duration.between(this.triggerContext.getClock().instant(), this.scheduledExecutionTime);
this.currentFuture = this.executor.schedule(this, initialDelay.toMillis(), TimeUnit.MILLISECONDS);
return this;
}
}
@@ -91,9 +92,9 @@ class ReschedulingRunnable extends DelegatingErrorHandlingRunnable implements Sc
@Override
public void run() {
Date actualExecutionTime = new Date(this.triggerContext.getClock().millis());
Instant actualExecutionTime = this.triggerContext.getClock().instant();
super.run();
Date completionTime = new Date(this.triggerContext.getClock().millis());
Instant completionTime = this.triggerContext.getClock().instant();
synchronized (this.triggerContextMonitor) {
Assert.state(this.scheduledExecutionTime != null, "No scheduled execution");
this.triggerContext.update(this.scheduledExecutionTime, actualExecutionTime, completionTime);

View File

@@ -17,7 +17,8 @@
package org.springframework.scheduling.concurrent;
import java.time.Clock;
import java.util.Date;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
@@ -377,11 +378,11 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
}
@Override
public ScheduledFuture<?> schedule(Runnable task, Date startTime) {
public ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
ScheduledExecutorService executor = getScheduledExecutor();
long initialDelay = startTime.getTime() - this.clock.millis();
Duration initialDelay = Duration.between(this.clock.instant(), startTime);
try {
return executor.schedule(errorHandlingTask(task, false), initialDelay, TimeUnit.MILLISECONDS);
return executor.schedule(errorHandlingTask(task, false), initialDelay.toMillis(), TimeUnit.MILLISECONDS);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
@@ -389,11 +390,11 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) {
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period) {
ScheduledExecutorService executor = getScheduledExecutor();
long initialDelay = startTime.getTime() - this.clock.millis();
Duration initialDelay = Duration.between(this.clock.instant(), startTime);
try {
return executor.scheduleAtFixedRate(errorHandlingTask(task, true), initialDelay, period, TimeUnit.MILLISECONDS);
return executor.scheduleAtFixedRate(errorHandlingTask(task, true), initialDelay.toMillis(), period.toMillis(), TimeUnit.MILLISECONDS);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
@@ -401,10 +402,10 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
ScheduledExecutorService executor = getScheduledExecutor();
try {
return executor.scheduleAtFixedRate(errorHandlingTask(task, true), 0, period, TimeUnit.MILLISECONDS);
return executor.scheduleAtFixedRate(errorHandlingTask(task, true), 0, period.toMillis(), TimeUnit.MILLISECONDS);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
@@ -412,11 +413,11 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay) {
ScheduledExecutorService executor = getScheduledExecutor();
long initialDelay = startTime.getTime() - this.clock.millis();
Duration initialDelay = Duration.between(this.clock.instant(), startTime);
try {
return executor.scheduleWithFixedDelay(errorHandlingTask(task, true), initialDelay, delay, TimeUnit.MILLISECONDS);
return executor.scheduleWithFixedDelay(errorHandlingTask(task, true), initialDelay.toMillis(), delay.toMillis(), TimeUnit.MILLISECONDS);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
@@ -424,10 +425,10 @@ public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay) {
ScheduledExecutorService executor = getScheduledExecutor();
try {
return executor.scheduleWithFixedDelay(errorHandlingTask(task, true), 0, delay, TimeUnit.MILLISECONDS);
return executor.scheduleWithFixedDelay(errorHandlingTask(task, true), 0, delay.toMillis(), TimeUnit.MILLISECONDS);
}
catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,10 +16,13 @@
package org.springframework.scheduling.config;
import java.time.Duration;
/**
* Specialization of {@link IntervalTask} for fixed-delay semantics.
*
* @author Juergen Hoeller
* @author Arjen Poutsma
* @since 5.0.2
* @see org.springframework.scheduling.annotation.Scheduled#fixedDelay()
* @see ScheduledTaskRegistrar#addFixedDelayTask(IntervalTask)
@@ -31,9 +34,26 @@ public class FixedDelayTask extends IntervalTask {
* @param runnable the underlying task to execute
* @param interval how often in milliseconds the task should be executed
* @param initialDelay the initial delay before first execution of the task
* @deprecated as of 6.0, in favor on {@link #FixedDelayTask(Runnable, Duration, Duration)}
*/
@Deprecated
public FixedDelayTask(Runnable runnable, long interval, long initialDelay) {
super(runnable, interval, initialDelay);
}
/**
* Create a new {@code FixedDelayTask}.
* @param runnable the underlying task to execute
* @param interval how often the task should be executed
* @param initialDelay the initial delay before first execution of the task
* @since 6.0
*/
public FixedDelayTask(Runnable runnable, Duration interval, Duration initialDelay) {
super(runnable, interval, initialDelay);
}
FixedDelayTask(IntervalTask task) {
super(task);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,10 +16,13 @@
package org.springframework.scheduling.config;
import java.time.Duration;
/**
* Specialization of {@link IntervalTask} for fixed-rate semantics.
*
* @author Juergen Hoeller
* @author Arjen Poutsma
* @since 5.0.2
* @see org.springframework.scheduling.annotation.Scheduled#fixedRate()
* @see ScheduledTaskRegistrar#addFixedRateTask(IntervalTask)
@@ -31,9 +34,27 @@ public class FixedRateTask extends IntervalTask {
* @param runnable the underlying task to execute
* @param interval how often in milliseconds the task should be executed
* @param initialDelay the initial delay before first execution of the task
* @deprecated as of 6.0, in favor on {@link #FixedRateTask(Runnable, Duration, Duration)}
*/
@Deprecated
public FixedRateTask(Runnable runnable, long interval, long initialDelay) {
super(runnable, interval, initialDelay);
}
/**
* Create a new {@code FixedRateTask}.
* @param runnable the underlying task to execute
* @param interval how often the task should be executed
* @param initialDelay the initial delay before first execution of the task
* @since 6.0
*/
public FixedRateTask(Runnable runnable, Duration interval, Duration initialDelay) {
super(runnable, interval, initialDelay);
}
FixedRateTask(IntervalTask task) {
super(task);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,21 +16,26 @@
package org.springframework.scheduling.config;
import java.time.Duration;
import org.springframework.util.Assert;
/**
* {@link Task} implementation defining a {@code Runnable} to be executed at a given
* millisecond interval which may be treated as fixed-rate or fixed-delay depending on
* context.
*
* @author Chris Beams
* @author Arjen Poutsma
* @since 3.2
* @see ScheduledTaskRegistrar#addFixedRateTask(IntervalTask)
* @see ScheduledTaskRegistrar#addFixedDelayTask(IntervalTask)
*/
public class IntervalTask extends Task {
private final long interval;
private final Duration interval;
private final long initialDelay;
private final Duration initialDelay;
/**
@@ -38,34 +43,95 @@ public class IntervalTask extends Task {
* @param runnable the underlying task to execute
* @param interval how often in milliseconds the task should be executed
* @param initialDelay the initial delay before first execution of the task
* @deprecated as of 6.0, in favor on {@link #IntervalTask(Runnable, Duration, Duration)}
*/
@Deprecated
public IntervalTask(Runnable runnable, long interval, long initialDelay) {
super(runnable);
this.interval = interval;
this.initialDelay = initialDelay;
this(runnable, Duration.ofMillis(interval), Duration.ofMillis(initialDelay));
}
/**
* Create a new {@code IntervalTask} with no initial delay.
* @param runnable the underlying task to execute
* @param interval how often in milliseconds the task should be executed
* @deprecated as of 6.0, in favor on {@link #IntervalTask(Runnable, Duration)}
*/
@Deprecated
public IntervalTask(Runnable runnable, long interval) {
this(runnable, interval, 0);
this(runnable, Duration.ofMillis(interval), Duration.ZERO);
}
/**
* Create a new {@code IntervalTask} with no initial delay.
* @param runnable the underlying task to execute
* @param interval how often the task should be executed
* @since 6.0
*/
public IntervalTask(Runnable runnable, Duration interval) {
this(runnable, interval, Duration.ZERO);
}
/**
* Create a new {@code IntervalTask}.
* @param runnable the underlying task to execute
* @param interval how often the task should be executed
* @param initialDelay the initial delay before first execution of the task
* @since 6.0
*/
public IntervalTask(Runnable runnable, Duration interval, Duration initialDelay) {
super(runnable);
Assert.notNull(interval, "Interval must not be null");
Assert.notNull(initialDelay, "InitialDelay must not be null");
this.interval = interval;
this.initialDelay = initialDelay;
}
/**
* Copy constructor.
*/
IntervalTask(IntervalTask task) {
super(task.getRunnable());
Assert.notNull(task, "IntervalTask must not be null");
this.interval = task.getIntervalDuration();
this.initialDelay = task.getInitialDelayDuration();
}
/**
* Return how often in milliseconds the task should be executed.
* @deprecated as of 6.0, in favor of {@link #getIntervalDuration()}
*/
@Deprecated
public long getInterval() {
return this.interval.toMillis();
}
/**
* Return how often the task should be executed.
* @since 6.0
*/
public Duration getIntervalDuration() {
return this.interval;
}
/**
* Return the initial delay before first execution of the task.
* @deprecated as of 6.0, in favor of {@link #getInitialDelayDuration()}
*/
@Deprecated
public long getInitialDelay() {
return this.initialDelay.toMillis();
}
/**
* Return the initial delay before first execution of the task.
* @since 6.0
*/
public Duration getInitialDelayDuration() {
return this.initialDelay;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,10 @@
package org.springframework.scheduling.config;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -51,6 +52,7 @@ import org.springframework.util.CollectionUtils;
* @author Chris Beams
* @author Tobias Montagna-Hay
* @author Sam Brannen
* @author Arjen Poutsma
* @since 3.0
* @see org.springframework.scheduling.annotation.EnableAsync
* @see org.springframework.scheduling.annotation.SchedulingConfigurer
@@ -189,11 +191,11 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
/**
* Specify triggered tasks as a Map of Runnables (the tasks) and fixed-rate values.
* @see TaskScheduler#scheduleAtFixedRate(Runnable, long)
* @see TaskScheduler#scheduleAtFixedRate(Runnable, Duration)
*/
public void setFixedRateTasks(Map<Runnable, Long> fixedRateTasks) {
this.fixedRateTasks = new ArrayList<>();
fixedRateTasks.forEach(this::addFixedRateTask);
fixedRateTasks.forEach((task, interval) -> addFixedRateTask(task, Duration.ofMillis(interval)));
}
/**
@@ -218,11 +220,11 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
/**
* Specify triggered tasks as a Map of Runnables (the tasks) and fixed-delay values.
* @see TaskScheduler#scheduleWithFixedDelay(Runnable, long)
* @see TaskScheduler#scheduleWithFixedDelay(Runnable, Duration)
*/
public void setFixedDelayTasks(Map<Runnable, Long> fixedDelayTasks) {
this.fixedDelayTasks = new ArrayList<>();
fixedDelayTasks.forEach(this::addFixedDelayTask);
fixedDelayTasks.forEach((task, delay) -> addFixedDelayTask(task, Duration.ofMillis(delay)));
}
/**
@@ -248,7 +250,7 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
/**
* Add a Runnable task to be triggered per the given {@link Trigger}.
* @see TaskScheduler#scheduleAtFixedRate(Runnable, long)
* @see TaskScheduler#scheduleAtFixedRate(Runnable, Duration)
*/
public void addTriggerTask(Runnable task, Trigger trigger) {
addTriggerTask(new TriggerTask(task, trigger));
@@ -257,7 +259,7 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
/**
* Add a {@code TriggerTask}.
* @since 3.2
* @see TaskScheduler#scheduleAtFixedRate(Runnable, long)
* @see TaskScheduler#scheduleAtFixedRate(Runnable, Duration)
*/
public void addTriggerTask(TriggerTask task) {
if (this.triggerTasks == null) {
@@ -290,16 +292,26 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
/**
* Add a {@code Runnable} task to be triggered at the given fixed-rate interval.
* @see TaskScheduler#scheduleAtFixedRate(Runnable, long)
* @deprecated as of 6.0, in favor of {@link #addFixedRateTask(Runnable, Duration)}
*/
@Deprecated
public void addFixedRateTask(Runnable task, long interval) {
addFixedRateTask(new IntervalTask(task, interval, 0));
addFixedRateTask(new IntervalTask(task, Duration.ofMillis(interval)));
}
/**
* Add a {@code Runnable} task to be triggered at the given fixed-rate interval.
* @since 6.0
* @see TaskScheduler#scheduleAtFixedRate(Runnable, Duration)
*/
public void addFixedRateTask(Runnable task, Duration interval) {
addFixedRateTask(new IntervalTask(task, interval));
}
/**
* Add a fixed-rate {@link IntervalTask}.
* @since 3.2
* @see TaskScheduler#scheduleAtFixedRate(Runnable, long)
* @see TaskScheduler#scheduleAtFixedRate(Runnable, Duration)
*/
public void addFixedRateTask(IntervalTask task) {
if (this.fixedRateTasks == null) {
@@ -310,16 +322,26 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
/**
* Add a Runnable task to be triggered with the given fixed delay.
* @see TaskScheduler#scheduleWithFixedDelay(Runnable, long)
* @deprecated as of 6.0, in favor of {@link #addFixedDelayTask(Runnable, Duration)}
*/
@Deprecated
public void addFixedDelayTask(Runnable task, long delay) {
addFixedDelayTask(new IntervalTask(task, delay, 0));
addFixedDelayTask(new IntervalTask(task, Duration.ofMillis(delay)));
}
/**
* Add a Runnable task to be triggered with the given fixed delay.
* @since 6.0
* @see TaskScheduler#scheduleWithFixedDelay(Runnable, Duration)
*/
public void addFixedDelayTask(Runnable task, Duration delay) {
addFixedDelayTask(new IntervalTask(task, delay));
}
/**
* Add a fixed-delay {@link IntervalTask}.
* @since 3.2
* @see TaskScheduler#scheduleWithFixedDelay(Runnable, long)
* @see TaskScheduler#scheduleWithFixedDelay(Runnable, Duration)
*/
public void addFixedDelayTask(IntervalTask task) {
if (this.fixedDelayTasks == null) {
@@ -353,7 +375,6 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
* Schedule all registered tasks against the underlying
* {@linkplain #setTaskScheduler(TaskScheduler) task scheduler}.
*/
@SuppressWarnings("deprecation")
protected void scheduleTasks() {
if (this.taskScheduler == null) {
this.localExecutor = Executors.newSingleThreadScheduledExecutor();
@@ -371,12 +392,22 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
}
if (this.fixedRateTasks != null) {
for (IntervalTask task : this.fixedRateTasks) {
addScheduledTask(scheduleFixedRateTask(task));
if (task instanceof FixedRateTask fixedRateTask) {
addScheduledTask(scheduleFixedRateTask(fixedRateTask));
}
else {
addScheduledTask(scheduleFixedRateTask(new FixedRateTask(task)));
}
}
}
if (this.fixedDelayTasks != null) {
for (IntervalTask task : this.fixedDelayTasks) {
addScheduledTask(scheduleFixedDelayTask(task));
if (task instanceof FixedDelayTask fixedDelayTask) {
addScheduledTask(scheduleFixedDelayTask(fixedDelayTask));
}
else {
addScheduledTask(scheduleFixedDelayTask(new FixedDelayTask(task)));
}
}
}
}
@@ -437,22 +468,6 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
return (newTask ? scheduledTask : null);
}
/**
* Schedule the specified fixed-rate task, either right away if possible
* or on initialization of the scheduler.
* @return a handle to the scheduled task, allowing to cancel it
* (or {@code null} if processing a previously registered task)
* @since 4.3
* @deprecated as of 5.0.2, in favor of {@link #scheduleFixedRateTask(FixedRateTask)}
*/
@Deprecated
@Nullable
public ScheduledTask scheduleFixedRateTask(IntervalTask task) {
FixedRateTask taskToUse = (task instanceof FixedRateTask ? (FixedRateTask) task :
new FixedRateTask(task.getRunnable(), task.getInterval(), task.getInitialDelay()));
return scheduleFixedRateTask(taskToUse);
}
/**
* Schedule the specified fixed-rate task, either right away if possible
* or on initialization of the scheduler.
@@ -469,14 +484,15 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
newTask = true;
}
if (this.taskScheduler != null) {
if (task.getInitialDelay() > 0) {
Date startTime = new Date(this.taskScheduler.getClock().millis() + task.getInitialDelay());
Duration initialDelay = task.getInitialDelayDuration();
if (initialDelay.toMillis() > 0) {
Instant startTime = this.taskScheduler.getClock().instant().plus(initialDelay);
scheduledTask.future =
this.taskScheduler.scheduleAtFixedRate(task.getRunnable(), startTime, task.getInterval());
this.taskScheduler.scheduleAtFixedRate(task.getRunnable(), startTime, task.getIntervalDuration());
}
else {
scheduledTask.future =
this.taskScheduler.scheduleAtFixedRate(task.getRunnable(), task.getInterval());
this.taskScheduler.scheduleAtFixedRate(task.getRunnable(), task.getIntervalDuration());
}
}
else {
@@ -486,22 +502,6 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
return (newTask ? scheduledTask : null);
}
/**
* Schedule the specified fixed-delay task, either right away if possible
* or on initialization of the scheduler.
* @return a handle to the scheduled task, allowing to cancel it
* (or {@code null} if processing a previously registered task)
* @since 4.3
* @deprecated as of 5.0.2, in favor of {@link #scheduleFixedDelayTask(FixedDelayTask)}
*/
@Deprecated
@Nullable
public ScheduledTask scheduleFixedDelayTask(IntervalTask task) {
FixedDelayTask taskToUse = (task instanceof FixedDelayTask ? (FixedDelayTask) task :
new FixedDelayTask(task.getRunnable(), task.getInterval(), task.getInitialDelay()));
return scheduleFixedDelayTask(taskToUse);
}
/**
* Schedule the specified fixed-delay task, either right away if possible
* or on initialization of the scheduler.
@@ -518,14 +518,15 @@ public class ScheduledTaskRegistrar implements ScheduledTaskHolder, Initializing
newTask = true;
}
if (this.taskScheduler != null) {
if (task.getInitialDelay() > 0) {
Date startTime = new Date(this.taskScheduler.getClock().millis() + task.getInitialDelay());
Duration initialDelay = task.getInitialDelayDuration();
if (!initialDelay.isNegative()) {
Instant startTime = this.taskScheduler.getClock().instant().plus(task.getInitialDelayDuration());
scheduledTask.future =
this.taskScheduler.scheduleWithFixedDelay(task.getRunnable(), startTime, task.getInterval());
this.taskScheduler.scheduleWithFixedDelay(task.getRunnable(), startTime, task.getIntervalDuration());
}
else {
scheduledTask.future =
this.taskScheduler.scheduleWithFixedDelay(task.getRunnable(), task.getInterval());
this.taskScheduler.scheduleWithFixedDelay(task.getRunnable(), task.getIntervalDuration());
}
}
else {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,9 @@
package org.springframework.scheduling.support;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.TimeZone;
import org.springframework.lang.Nullable;
@@ -93,23 +93,23 @@ public class CronTrigger implements Trigger {
* previous execution; therefore, overlapping executions won't occur.
*/
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
Date date = triggerContext.lastCompletionTime();
if (date != null) {
Date scheduled = triggerContext.lastScheduledExecutionTime();
if (scheduled != null && date.before(scheduled)) {
public Instant nextExecution(TriggerContext triggerContext) {
Instant instant = triggerContext.lastCompletion();
if (instant != null) {
Instant scheduled = triggerContext.lastScheduledExecution();
if (scheduled != null && instant.isBefore(scheduled)) {
// Previous task apparently executed too early...
// Let's simply use the last calculated execution time then,
// in order to prevent accidental re-fires in the same second.
date = scheduled;
instant = scheduled;
}
}
else {
date = new Date(triggerContext.getClock().millis());
instant = triggerContext.getClock().instant();
}
ZonedDateTime dateTime = ZonedDateTime.ofInstant(date.toInstant(), this.zoneId);
ZonedDateTime dateTime = ZonedDateTime.ofInstant(instant, this.zoneId);
ZonedDateTime next = this.expression.next(dateTime);
return (next != null ? Date.from(next.toInstant()) : null);
return (next != null ? next.toInstant() : null);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,10 @@
package org.springframework.scheduling.support;
import java.util.Date;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.springframework.lang.Nullable;
@@ -46,18 +49,22 @@ import org.springframework.util.Assert;
*/
public class PeriodicTrigger implements Trigger {
private final long period;
private final Duration period;
private final TimeUnit timeUnit;
@Nullable
private final ChronoUnit chronoUnit;
private volatile long initialDelay;
@Nullable
private volatile Duration initialDelay;
private volatile boolean fixedRate;
/**
* Create a trigger with the given period in milliseconds.
* @deprecated as of 6.0, in favor on {@link #PeriodicTrigger(Duration)}
*/
@Deprecated
public PeriodicTrigger(long period) {
this(period, null);
}
@@ -66,44 +73,132 @@ public class PeriodicTrigger implements Trigger {
* Create a trigger with the given period and time unit. The time unit will
* apply not only to the period but also to any 'initialDelay' value, if
* configured on this Trigger later via {@link #setInitialDelay(long)}.
* @deprecated as of 6.0, in favor on {@link #PeriodicTrigger(Duration)}
*/
@Deprecated
public PeriodicTrigger(long period, @Nullable TimeUnit timeUnit) {
Assert.isTrue(period >= 0, "period must not be negative");
this.timeUnit = (timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS);
this.period = this.timeUnit.toMillis(period);
this(toDuration(period, timeUnit), timeUnit);
}
private static Duration toDuration(long amount, @Nullable TimeUnit timeUnit) {
if (timeUnit != null) {
return Duration.of(amount, timeUnit.toChronoUnit());
}
else {
return Duration.ofMillis(amount);
}
}
/**
* Create a trigger with the given period as a duration.
* @since 6.0
*/
public PeriodicTrigger(Duration period) {
this(period, null);
}
private PeriodicTrigger(Duration period, @Nullable TimeUnit timeUnit) {
Assert.notNull(period, "Period must not be null");
Assert.isTrue(!period.isNegative(), "Period must not be negative");
this.period = period;
if (timeUnit != null) {
this.chronoUnit = timeUnit.toChronoUnit();
}
else {
this.chronoUnit = null;
}
}
/**
* Return this trigger's period.
* @since 5.0.2
* @deprecated as of 6.0, in favor on {@link #getPeriodDuration()}
*/
@Deprecated
public long getPeriod() {
if (this.chronoUnit != null) {
return this.period.get(this.chronoUnit);
}
else {
return this.period.toMillis();
}
}
/**
* Return this trigger's period.
* @since 6.0
*/
public Duration getPeriodDuration() {
return this.period;
}
/**
* Return this trigger's time unit (milliseconds by default).
* @since 5.0.2
* @deprecated as of 6.0, with no direct replacement
*/
@Deprecated
public TimeUnit getTimeUnit() {
return this.timeUnit;
if (this.chronoUnit != null) {
return TimeUnit.of(this.chronoUnit);
}
else {
return TimeUnit.MILLISECONDS;
}
}
/**
* Specify the delay for the initial execution. It will be evaluated in
* terms of this trigger's {@link TimeUnit}. If no time unit was explicitly
* provided upon instantiation, the default is milliseconds.
* @deprecated as of 6.0, in favor of {@link #setInitialDelay(Duration)}
*/
@Deprecated
public void setInitialDelay(long initialDelay) {
this.initialDelay = this.timeUnit.toMillis(initialDelay);
if (this.chronoUnit != null) {
this.initialDelay = Duration.of(initialDelay, this.chronoUnit);
}
else {
this.initialDelay = Duration.ofMillis(initialDelay);
}
}
/**
* Specify the delay for the initial execution.
* @since 6.0
*/
public void setInitialDelay(Duration initialDelay) {
this.initialDelay = initialDelay;
}
/**
* Return the initial delay, or 0 if none.
* @since 5.0.2
* @deprecated as of 6.0, in favor on {@link #getInitialDelayDuration()}
*/
@Deprecated
public long getInitialDelay() {
Duration initialDelay = this.initialDelay;
if (initialDelay != null) {
if (this.chronoUnit != null) {
return initialDelay.get(this.chronoUnit);
}
else {
return initialDelay.toMillis();
}
}
else {
return 0;
}
}
/**
* Return the initial delay, or {@code null} if none.
* @since 6.0
*/
@Nullable
public Duration getInitialDelayDuration() {
return this.initialDelay;
}
@@ -130,16 +225,23 @@ public class PeriodicTrigger implements Trigger {
* Returns the time after which a task should run again.
*/
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
Date lastExecution = triggerContext.lastScheduledExecutionTime();
Date lastCompletion = triggerContext.lastCompletionTime();
public Instant nextExecution(TriggerContext triggerContext) {
Instant lastExecution = triggerContext.lastScheduledExecution();
Instant lastCompletion = triggerContext.lastCompletion();
if (lastExecution == null || lastCompletion == null) {
return new Date(triggerContext.getClock().millis() + this.initialDelay);
Instant instant = triggerContext.getClock().instant();
Duration initialDelay = this.initialDelay;
if (initialDelay == null) {
return instant;
}
else {
return instant.plus(initialDelay);
}
}
if (this.fixedRate) {
return new Date(lastExecution.getTime() + this.period);
return lastExecution.plus(this.period);
}
return new Date(lastCompletion.getTime() + this.period);
return lastCompletion.plus(this.period);
}
@@ -151,13 +253,14 @@ public class PeriodicTrigger implements Trigger {
if (!(other instanceof PeriodicTrigger otherTrigger)) {
return false;
}
return (this.fixedRate == otherTrigger.fixedRate && this.initialDelay == otherTrigger.initialDelay &&
this.period == otherTrigger.period);
return (this.fixedRate == otherTrigger.fixedRate &&
this.period.equals(otherTrigger.period) &&
Objects.equals(this.initialDelay, otherTrigger.initialDelay));
}
@Override
public int hashCode() {
return (this.fixedRate ? 17 : 29) + (int) (37 * this.period) + (int) (41 * this.initialDelay);
return this.period.hashCode();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.scheduling.support;
import java.time.Clock;
import java.time.Instant;
import java.util.Date;
import org.springframework.lang.Nullable;
@@ -33,13 +34,13 @@ public class SimpleTriggerContext implements TriggerContext {
private final Clock clock;
@Nullable
private volatile Date lastScheduledExecutionTime;
private volatile Instant lastScheduledExecution;
@Nullable
private volatile Date lastActualExecutionTime;
private volatile Instant lastActualExecution;
@Nullable
private volatile Date lastCompletionTime;
private volatile Instant lastCompletion;
/**
@@ -56,12 +57,34 @@ public class SimpleTriggerContext implements TriggerContext {
* @param lastScheduledExecutionTime last <i>scheduled</i> execution time
* @param lastActualExecutionTime last <i>actual</i> execution time
* @param lastCompletionTime last completion time
* @deprecated as of 6.0, in favor of {@link #SimpleTriggerContext(Instant, Instant, Instant)}
*/
public SimpleTriggerContext(Date lastScheduledExecutionTime, Date lastActualExecutionTime, Date lastCompletionTime) {
@Deprecated
public SimpleTriggerContext(@Nullable Date lastScheduledExecutionTime, @Nullable Date lastActualExecutionTime,
@Nullable Date lastCompletionTime) {
this(toInstant(lastScheduledExecutionTime), toInstant(lastActualExecutionTime), toInstant(lastCompletionTime));
}
@Nullable
private static Instant toInstant(@Nullable Date date) {
return date != null ? date.toInstant() : null;
}
/**
* Create a SimpleTriggerContext with the given time values,
* exposing the system clock for the default time zone.
* @param lastScheduledExecution last <i>scheduled</i> execution time
* @param lastActualExecution last <i>actual</i> execution time
* @param lastCompletion last completion time
*/
public SimpleTriggerContext(@Nullable Instant lastScheduledExecution, @Nullable Instant lastActualExecution,
@Nullable Instant lastCompletion) {
this();
this.lastScheduledExecutionTime = lastScheduledExecutionTime;
this.lastActualExecutionTime = lastActualExecutionTime;
this.lastCompletionTime = lastCompletionTime;
this.lastScheduledExecution = lastScheduledExecution;
this.lastActualExecution = lastActualExecution;
this.lastCompletion = lastCompletion;
}
/**
@@ -69,7 +92,7 @@ public class SimpleTriggerContext implements TriggerContext {
* exposing the given clock.
* @param clock the clock to use for trigger calculation
* @since 5.3
* @see #update(Date, Date, Date)
* @see #update(Instant, Instant, Instant)
*/
public SimpleTriggerContext(Clock clock) {
this.clock = clock;
@@ -81,11 +104,27 @@ public class SimpleTriggerContext implements TriggerContext {
* @param lastScheduledExecutionTime last <i>scheduled</i> execution time
* @param lastActualExecutionTime last <i>actual</i> execution time
* @param lastCompletionTime last completion time
* @deprecated as of 6.0, in favor of {@link #update(Instant, Instant, Instant)}
*/
public void update(Date lastScheduledExecutionTime, Date lastActualExecutionTime, Date lastCompletionTime) {
this.lastScheduledExecutionTime = lastScheduledExecutionTime;
this.lastActualExecutionTime = lastActualExecutionTime;
this.lastCompletionTime = lastCompletionTime;
@Deprecated
public void update(@Nullable Date lastScheduledExecutionTime, @Nullable Date lastActualExecutionTime,
@Nullable Date lastCompletionTime) {
update(toInstant(lastScheduledExecutionTime), toInstant(lastActualExecutionTime), toInstant(lastCompletionTime));
}
/**
* Update this holder's state with the latest time values.
* @param lastScheduledExecution last <i>scheduled</i> execution time
* @param lastActualExecution last <i>actual</i> execution time
* @param lastCompletion last completion time
*/
public void update(@Nullable Instant lastScheduledExecution, @Nullable Instant lastActualExecution,
@Nullable Instant lastCompletion) {
this.lastScheduledExecution = lastScheduledExecution;
this.lastActualExecution = lastActualExecution;
this.lastCompletion = lastCompletion;
}
@@ -96,20 +135,20 @@ public class SimpleTriggerContext implements TriggerContext {
@Override
@Nullable
public Date lastScheduledExecutionTime() {
return this.lastScheduledExecutionTime;
public Instant lastScheduledExecution() {
return this.lastScheduledExecution;
}
@Override
@Nullable
public Date lastActualExecutionTime() {
return this.lastActualExecutionTime;
public Instant lastActualExecution() {
return this.lastActualExecution;
}
@Override
@Nullable
public Date lastCompletionTime() {
return this.lastCompletionTime;
public Instant lastCompletion() {
return this.lastCompletion;
}
}