@@ -5,16 +5,11 @@ The Spring Framework provides abstractions for the asynchronous execution and sc
5
5
tasks with the `TaskExecutor` and `TaskScheduler` interfaces, respectively. Spring also
6
6
features implementations of those interfaces that support thread pools or delegation to
7
7
CommonJ within an application server environment. Ultimately, the use of these
8
- implementations behind the common interfaces abstracts away the differences between Java
9
- SE 5, Java SE 6, and Jakarta EE environments.
8
+ implementations behind the common interfaces abstracts away the differences between
9
+ Java SE and Jakarta EE environments.
10
10
11
- Spring also features integration classes to support scheduling with the `Timer`
12
- (part of the JDK since 1.3) and the https://www.quartz-scheduler.org/[Quartz Scheduler].
13
- You can set up both of those schedulers by using a `FactoryBean` with optional references to
14
- `Timer` or `Trigger` instances, respectively. Furthermore, a convenience class for both
15
- the Quartz Scheduler and the `Timer` is available that lets you invoke a method of
16
- an existing target object (analogous to the normal `MethodInvokingFactoryBean`
17
- operation).
11
+ Spring also features integration classes to support scheduling with the
12
+ https://www.quartz-scheduler.org/[Quartz Scheduler].
18
13
19
14
20
15
@@ -271,8 +266,8 @@ execution.
271
266
[[scheduling-enable-annotation-support]]
272
267
=== Enable Scheduling Annotations
273
268
274
- To enable support for `@Scheduled` and `@Async` annotations, you can add `@EnableScheduling` and
275
- `@EnableAsync` to one of your `@Configuration` classes, as the following example shows:
269
+ To enable support for `@Scheduled` and `@Async` annotations, you can add `@EnableScheduling`
270
+ and `@EnableAsync` to one of your `@Configuration` classes, as the following example shows:
276
271
277
272
[source,java,indent=0,subs="verbatim,quotes"]
278
273
----
@@ -552,8 +547,8 @@ to a method that returns `void`, as the following example shows:
552
547
553
548
Unlike the methods annotated with the `@Scheduled` annotation, these methods can expect
554
549
arguments, because they are invoked in the "`normal`" way by callers at runtime rather
555
- than from a scheduled task being managed by the container. For example, the following code is
556
- a legitimate application of the `@Async` annotation:
550
+ than from a scheduled task being managed by the container. For example, the following
551
+ code is a legitimate application of the `@Async` annotation:
557
552
558
553
[source,java,indent=0,subs="verbatim,quotes"]
559
554
----
@@ -577,15 +572,15 @@ that returns a value:
577
572
}
578
573
----
579
574
580
- TIP: `@Async` methods may not only declare a regular `java.util.concurrent.Future` return type
581
- but also Spring's `org.springframework.util.concurrent.ListenableFuture` or, as of Spring
582
- 4.2, JDK 8's `java.util.concurrent.CompletableFuture`, for richer interaction with the
583
- asynchronous task and for immediate composition with further processing steps.
575
+ TIP: `@Async` methods may not only declare a regular `java.util.concurrent.Future` return
576
+ type but also Spring's `org.springframework.util.concurrent.ListenableFuture` or, as of
577
+ Spring 4.2, JDK 8's `java.util.concurrent.CompletableFuture`, for richer interaction with
578
+ the asynchronous task and for immediate composition with further processing steps.
584
579
585
- You can not use `@Async` in conjunction with lifecycle callbacks such as
586
- `@PostConstruct`. To asynchronously initialize Spring beans, you currently have to use
587
- a separate initializing Spring bean that then invokes the `@Async` annotated method on the
588
- target, as the following example shows:
580
+ You can not use `@Async` in conjunction with lifecycle callbacks such as `@PostConstruct`.
581
+ To asynchronously initialize Spring beans, you currently have to use a separate
582
+ initializing Spring bean that then invokes the `@Async` annotated method on the target,
583
+ as the following example shows:
589
584
590
585
[source,java,indent=0,subs="verbatim,quotes"]
591
586
----
@@ -639,8 +634,8 @@ used when executing a given method. The following example shows how to do so:
639
634
----
640
635
641
636
In this case, `"otherExecutor"` can be the name of any `Executor` bean in the Spring
642
- container, or it may be the name of a qualifier associated with any `Executor` (for example, as
643
- specified with the `<qualifier>` element or Spring's `@Qualifier` annotation).
637
+ container, or it may be the name of a qualifier associated with any `Executor` (for example,
638
+ as specified with the `<qualifier>` element or Spring's `@Qualifier` annotation).
644
639
645
640
646
641
[[scheduling-annotation-support-exception]]
@@ -808,14 +803,15 @@ invoked on that object. The following listing shows a simple example:
808
803
----
809
804
810
805
The scheduler is referenced by the outer element, and each individual
811
- task includes the configuration of its trigger metadata. In the preceding example, that
812
- metadata defines a periodic trigger with a fixed delay indicating the number of
806
+ task includes the configuration of its trigger metadata. In the preceding example,
807
+ that metadata defines a periodic trigger with a fixed delay indicating the number of
813
808
milliseconds to wait after each task execution has completed. Another option is
814
809
`fixed-rate`, indicating how often the method should be run regardless of how long
815
- any previous execution takes. Additionally, for both `fixed-delay` and `fixed-rate` tasks, you can specify an
816
- 'initial-delay' parameter, indicating the number of milliseconds to wait
817
- before the first execution of the method. For more control, you can instead provide a `cron` attribute
818
- to provide a xref:integration/scheduling.adoc#scheduling-cron-expression[cron expression].
810
+ any previous execution takes. Additionally, for both `fixed-delay` and `fixed-rate`
811
+ tasks, you can specify an 'initial-delay' parameter, indicating the number of
812
+ milliseconds to wait before the first execution of the method. For more control,
813
+ you can instead provide a `cron` attribute to provide a
814
+ xref:integration/scheduling.adoc#scheduling-cron-expression[cron expression].
819
815
The following example shows these other options:
820
816
821
817
[source,xml,indent=0]
@@ -838,9 +834,8 @@ The following example shows these other options:
838
834
All Spring cron expressions have to conform to the same format, whether you are using them in
839
835
xref:integration/scheduling.adoc#scheduling-annotation-support-scheduled[`@Scheduled` annotations],
840
836
xref:integration/scheduling.adoc#scheduling-task-namespace-scheduled-tasks[`task:scheduled-tasks` elements],
841
- or someplace else.
842
- A well-formed cron expression, such as `* * * * * *`, consists of six space-separated time and date
843
- fields, each with its own range of valid values:
837
+ or someplace else. A well-formed cron expression, such as `* * * * * *`, consists of six
838
+ space-separated time and date fields, each with its own range of valid values:
844
839
845
840
846
841
....
@@ -905,9 +900,10 @@ Here are some examples:
905
900
[[macros]]
906
901
=== Macros
907
902
908
- Expressions such as `0 0 * * * *` are hard for humans to parse and are, therefore, hard to fix in case of bugs.
909
- To improve readability, Spring supports the following macros, which represent commonly used sequences.
910
- You can use these macros instead of the six-digit value, thus: `@Scheduled(cron = "@hourly")`.
903
+ Expressions such as `0 0 * * * *` are hard for humans to parse and are, therefore,
904
+ hard to fix in case of bugs. To improve readability, Spring supports the following
905
+ macros, which represent commonly used sequences. You can use these macros instead
906
+ of the six-digit value, thus: `@Scheduled(cron = "@hourly")`.
911
907
912
908
|===
913
909
|Macro | Meaning
@@ -924,18 +920,18 @@ You can use these macros instead of the six-digit value, thus: `@Scheduled(cron
924
920
[[scheduling-quartz]]
925
921
== Using the Quartz Scheduler
926
922
927
- Quartz uses `Trigger`, `Job`, and `JobDetail` objects to realize scheduling of all kinds
928
- of jobs. For the basic concepts behind Quartz, see the
923
+ Quartz uses `Trigger`, `Job`, and `JobDetail` objects to realize scheduling of all
924
+ kinds of jobs. For the basic concepts behind Quartz, see the
929
925
https://www.quartz-scheduler.org/[Quartz Web site]. For convenience purposes, Spring
930
926
offers a couple of classes that simplify using Quartz within Spring-based applications.
931
927
932
928
933
929
[[scheduling-quartz-jobdetail]]
934
930
=== Using the `JobDetailFactoryBean`
935
931
936
- Quartz `JobDetail` objects contain all the information needed to run a job. Spring provides a
937
- `JobDetailFactoryBean`, which provides bean-style properties for XML configuration purposes.
938
- Consider the following example:
932
+ Quartz `JobDetail` objects contain all the information needed to run a job. Spring
933
+ provides a `JobDetailFactoryBean`, which provides bean-style properties for XML
934
+ configuration purposes. Consider the following example:
939
935
940
936
[source,xml,indent=0,subs="verbatim,quotes"]
941
937
----
@@ -952,9 +948,9 @@ Consider the following example:
952
948
The job detail configuration has all the information it needs to run the job (`ExampleJob`).
953
949
The timeout is specified in the job data map. The job data map is available through the
954
950
`JobExecutionContext` (passed to you at execution time), but the `JobDetail` also gets
955
- its properties from the job data mapped to properties of the job instance. So, in the following example,
956
- the `ExampleJob` contains a bean property named `timeout`, and the `JobDetail`
957
- has it applied automatically:
951
+ its properties from the job data mapped to properties of the job instance. So, in the
952
+ following example, the `ExampleJob` contains a bean property named `timeout`, and the
953
+ `JobDetail` has it applied automatically:
958
954
959
955
[source,java,indent=0,subs="verbatim,quotes",chomp="-packages"]
960
956
----
@@ -1047,8 +1043,8 @@ NOTE: By default, jobs will run in a concurrent fashion.
1047
1043
[[scheduling-quartz-cron]]
1048
1044
=== Wiring up Jobs by Using Triggers and `SchedulerFactoryBean`
1049
1045
1050
- We have created job details and jobs. We have also reviewed the convenience bean that lets
1051
- you invoke a method on a specific object. Of course, we still need to schedule the
1046
+ We have created job details and jobs. We have also reviewed the convenience bean that
1047
+ lets you invoke a method on a specific object. Of course, we still need to schedule the
1052
1048
jobs themselves. This is done by using triggers and a `SchedulerFactoryBean`. Several
1053
1049
triggers are available within Quartz, and Spring offers two Quartz `FactoryBean`
1054
1050
implementations with convenient defaults: `CronTriggerFactoryBean` and
@@ -1079,9 +1075,9 @@ The following listing uses both a `SimpleTriggerFactoryBean` and a `CronTriggerF
1079
1075
</bean>
1080
1076
----
1081
1077
1082
- The preceding example sets up two triggers, one running every 50 seconds with a starting delay of 10
1083
- seconds and one running every morning at 6 AM. To finalize everything, we need to set up the
1084
- `SchedulerFactoryBean`, as the following example shows:
1078
+ The preceding example sets up two triggers, one running every 50 seconds with a starting
1079
+ delay of 10 seconds and one running every morning at 6 AM. To finalize everything,
1080
+ we need to set up the `SchedulerFactoryBean`, as the following example shows:
1085
1081
1086
1082
[source,xml,indent=0,subs="verbatim,quotes"]
1087
1083
----
0 commit comments