Support for one-time tasks with just @Scheduled(initialDelay=...)

Closes gh-31211
This commit is contained in:
Juergen Hoeller
2023-09-13 16:48:54 +02:00
parent e63e3a6d28
commit 8f6c56fe9a
10 changed files with 321 additions and 79 deletions

View File

@@ -346,7 +346,7 @@ For example, the previous example can also be written as follows.
If you need a fixed-rate execution, you can use the `fixedRate` attribute within the
annotation. The following method is invoked every five seconds (measured between the
successive start times of each invocation).
successive start times of each invocation):
[source,java,indent=0,subs="verbatim,quotes"]
----
@@ -356,9 +356,9 @@ successive start times of each invocation).
}
----
For fixed-delay and fixed-rate tasks, you can specify an initial delay by indicating the
amount of time to wait before the first execution of the method, as the following
`fixedRate` example shows.
For fixed-delay and fixed-rate tasks, you can specify an initial delay by indicating
the amount of time to wait before the first execution of the method, as the following
`fixedRate` example shows:
[source,java,indent=0,subs="verbatim,quotes"]
----
@@ -368,6 +368,17 @@ amount of time to wait before the first execution of the method, as the followin
}
----
For one-time tasks, you can just specify an initial delay by indicating the amount
of time to wait before the intended execution of the method:
[source,java,indent=0,subs="verbatim,quotes"]
----
@Scheduled(initialDelay = 1000)
public void doSomething() {
// something that should run only once
}
----
If simple periodic scheduling is not expressive enough, you can provide a
xref:integration/scheduling.adoc#scheduling-cron-expression[cron expression].
The following example runs only on weekdays: