Skip to content

Commit 313c89d

Browse files
committed
Overloaded methods with java.time.Instant/Duration parameters
Issue: SPR-14875
1 parent 278a625 commit 313c89d

File tree

1 file changed

+102
-11
lines changed

1 file changed

+102
-11
lines changed

spring-context/src/main/java/org/springframework/scheduling/TaskScheduler.java

+102-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.scheduling;
1818

19+
import java.time.Duration;
20+
import java.time.Instant;
1921
import java.util.Date;
2022
import java.util.concurrent.ScheduledFuture;
2123

@@ -34,9 +36,8 @@
3436
* and adding extended trigger capabilities.
3537
*
3638
* <p>This interface is roughly equivalent to a JSR-236
37-
* {@code ManagedScheduledExecutorService} as supported in Java EE 6
38-
* environments. However, at the time of the Spring 3.0 release, the
39-
* JSR-236 interfaces have not been released in official form yet.
39+
* {@code ManagedScheduledExecutorService} as supported in Java EE 7
40+
* environments but aligned with Spring's {@code TaskExecutor} model.
4041
*
4142
* @author Juergen Hoeller
4243
* @since 3.0
@@ -64,6 +65,23 @@ public interface TaskScheduler {
6465
*/
6566
ScheduledFuture<?> schedule(Runnable task, Trigger trigger);
6667

68+
/**
69+
* Schedule the given {@link Runnable}, invoking it at the specified execution time.
70+
* <p>Execution will end once the scheduler shuts down or the returned
71+
* {@link ScheduledFuture} gets cancelled.
72+
* @param task the Runnable to execute whenever the trigger fires
73+
* @param startTime the desired execution time for the task
74+
* (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
75+
* @return a {@link ScheduledFuture} representing pending completion of the task
76+
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
77+
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
78+
* @since 5.0
79+
* @see #schedule(Runnable, Date)
80+
*/
81+
default ScheduledFuture<?> schedule(Runnable task, Instant startTime) {
82+
return schedule(task, Date.from(startTime));
83+
}
84+
6785
/**
6886
* Schedule the given {@link Runnable}, invoking it at the specified execution time.
6987
* <p>Execution will end once the scheduler shuts down or the returned
@@ -77,6 +95,25 @@ public interface TaskScheduler {
7795
*/
7896
ScheduledFuture<?> schedule(Runnable task, Date startTime);
7997

98+
/**
99+
* Schedule the given {@link Runnable}, invoking it at the specified execution time
100+
* and subsequently with the given period.
101+
* <p>Execution will end once the scheduler shuts down or the returned
102+
* {@link ScheduledFuture} gets cancelled.
103+
* @param task the Runnable to execute whenever the trigger fires
104+
* @param startTime the desired first execution time for the task
105+
* (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
106+
* @param period the interval between successive executions of the task
107+
* @return a {@link ScheduledFuture} representing pending completion of the task
108+
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
109+
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
110+
* @since 5.0
111+
* @see #scheduleAtFixedRate(Runnable, Date, long)
112+
*/
113+
default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period) {
114+
return scheduleAtFixedRate(task, Date.from(startTime), period.toMillis());
115+
}
116+
80117
/**
81118
* Schedule the given {@link Runnable}, invoking it at the specified execution time
82119
* and subsequently with the given period.
@@ -87,11 +124,28 @@ public interface TaskScheduler {
87124
* (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
88125
* @param period the interval between successive executions of the task (in milliseconds)
89126
* @return a {@link ScheduledFuture} representing pending completion of the task
90-
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
127+
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
91128
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
92129
*/
93130
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period);
94131

132+
/**
133+
* Schedule the given {@link Runnable}, starting as soon as possible and
134+
* invoking it with the given period.
135+
* <p>Execution will end once the scheduler shuts down or the returned
136+
* {@link ScheduledFuture} gets cancelled.
137+
* @param task the Runnable to execute whenever the trigger fires
138+
* @param period the interval between successive executions of the task
139+
* @return a {@link ScheduledFuture} representing pending completion of the task
140+
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
141+
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
142+
* @since 5.0
143+
* @see #scheduleAtFixedRate(Runnable, long)
144+
*/
145+
default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period) {
146+
return scheduleAtFixedRate(task, period.toMillis());
147+
}
148+
95149
/**
96150
* Schedule the given {@link Runnable}, starting as soon as possible and
97151
* invoking it with the given period.
@@ -114,22 +168,59 @@ public interface TaskScheduler {
114168
* @param task the Runnable to execute whenever the trigger fires
115169
* @param startTime the desired first execution time for the task
116170
* (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
117-
* @param delay the delay between the completion of one execution and the start
118-
* of the next (in milliseconds)
171+
* @param delay the delay between the completion of one execution and the start of the next
119172
* @return a {@link ScheduledFuture} representing pending completion of the task
120173
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
121174
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
175+
* @since 5.0
176+
* @see #scheduleWithFixedDelay(Runnable, Date, long)
122177
*/
123-
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay);
178+
default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay) {
179+
return scheduleWithFixedDelay(task, Date.from(startTime), delay.toMillis());
180+
}
124181

125182
/**
126-
* Schedule the given {@link Runnable}, starting as soon as possible and
127-
* invoking it with the given delay between the completion of one execution
183+
* Schedule the given {@link Runnable}, invoking it at the specified execution time
184+
* and subsequently with the given delay between the completion of one execution
128185
* and the start of the next.
129186
* <p>Execution will end once the scheduler shuts down or the returned
130187
* {@link ScheduledFuture} gets cancelled.
131188
* @param task the Runnable to execute whenever the trigger fires
132-
* @param delay the interval between successive executions of the task (in milliseconds)
189+
* @param startTime the desired first execution time for the task
190+
* (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
191+
* @param delay the delay between the completion of one execution and the start of the next
192+
* (in milliseconds)
193+
* @return a {@link ScheduledFuture} representing pending completion of the task
194+
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
195+
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
196+
*/
197+
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay);
198+
199+
/**
200+
* Schedule the given {@link Runnable}, starting as soon as possible and invoking it with
201+
* the given delay between the completion of one execution and the start of the next.
202+
* <p>Execution will end once the scheduler shuts down or the returned
203+
* {@link ScheduledFuture} gets cancelled.
204+
* @param task the Runnable to execute whenever the trigger fires
205+
* @param delay the delay between the completion of one execution and the start of the next
206+
* @return a {@link ScheduledFuture} representing pending completion of the task
207+
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
208+
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
209+
* @since 5.0
210+
* @see #scheduleWithFixedDelay(Runnable, long)
211+
*/
212+
default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay) {
213+
return scheduleWithFixedDelay(task, delay.toMillis());
214+
}
215+
216+
/**
217+
* Schedule the given {@link Runnable}, starting as soon as possible and invoking it with
218+
* the given delay between the completion of one execution and the start of the next.
219+
* <p>Execution will end once the scheduler shuts down or the returned
220+
* {@link ScheduledFuture} gets cancelled.
221+
* @param task the Runnable to execute whenever the trigger fires
222+
* @param delay the delay between the completion of one execution and the start of the next
223+
* (in milliseconds)
133224
* @return a {@link ScheduledFuture} representing pending completion of the task
134225
* @throws org.springframework.core.task.TaskRejectedException if the given task was not accepted
135226
* for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)

0 commit comments

Comments
 (0)