@@ -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
@@ -261,8 +256,8 @@ execution.
261
256
[[scheduling-enable-annotation-support]]
262
257
=== Enable Scheduling Annotations
263
258
264
- To enable support for `@Scheduled` and `@Async` annotations, you can add `@EnableScheduling` and
265
- `@EnableAsync` to one of your `@Configuration` classes, as the following example shows:
259
+ To enable support for `@Scheduled` and `@Async` annotations, you can add `@EnableScheduling`
260
+ and `@EnableAsync` to one of your `@Configuration` classes, as the following example shows:
266
261
267
262
[source,java,indent=0,subs="verbatim,quotes"]
268
263
----
@@ -336,7 +331,7 @@ For example, the previous example can also be written as follows.
336
331
337
332
If you need a fixed-rate execution, you can use the `fixedRate` attribute within the
338
333
annotation. The following method is invoked every five seconds (measured between the
339
- successive start times of each invocation).
334
+ successive start times of each invocation):
340
335
341
336
[source,java,indent=0,subs="verbatim,quotes"]
342
337
----
@@ -346,9 +341,9 @@ successive start times of each invocation).
346
341
}
347
342
----
348
343
349
- For fixed-delay and fixed-rate tasks, you can specify an initial delay by indicating the
350
- amount of time to wait before the first execution of the method, as the following
351
- `fixedRate` example shows.
344
+ For fixed-delay and fixed-rate tasks, you can specify an initial delay by indicating
345
+ the amount of time to wait before the first execution of the method, as the following
346
+ `fixedRate` example shows:
352
347
353
348
[source,java,indent=0,subs="verbatim,quotes"]
354
349
----
@@ -417,8 +412,8 @@ to a method that returns `void`, as the following example shows:
417
412
418
413
Unlike the methods annotated with the `@Scheduled` annotation, these methods can expect
419
414
arguments, because they are invoked in the "`normal`" way by callers at runtime rather
420
- than from a scheduled task being managed by the container. For example, the following code is
421
- a legitimate application of the `@Async` annotation:
415
+ than from a scheduled task being managed by the container. For example, the following
416
+ code is a legitimate application of the `@Async` annotation:
422
417
423
418
[source,java,indent=0,subs="verbatim,quotes"]
424
419
----
@@ -442,15 +437,15 @@ that returns a value:
442
437
}
443
438
----
444
439
445
- TIP: `@Async` methods may not only declare a regular `java.util.concurrent.Future` return type
446
- but also Spring's `org.springframework.util.concurrent.ListenableFuture` or, as of Spring
447
- 4.2, JDK 8's `java.util.concurrent.CompletableFuture`, for richer interaction with the
448
- asynchronous task and for immediate composition with further processing steps.
440
+ TIP: `@Async` methods may not only declare a regular `java.util.concurrent.Future` return
441
+ type but also Spring's `org.springframework.util.concurrent.ListenableFuture` or, as of
442
+ Spring 4.2, JDK 8's `java.util.concurrent.CompletableFuture`, for richer interaction with
443
+ the asynchronous task and for immediate composition with further processing steps.
449
444
450
- You can not use `@Async` in conjunction with lifecycle callbacks such as
451
- `@PostConstruct`. To asynchronously initialize Spring beans, you currently have to use
452
- a separate initializing Spring bean that then invokes the `@Async` annotated method on the
453
- target, as the following example shows:
445
+ You can not use `@Async` in conjunction with lifecycle callbacks such as `@PostConstruct`.
446
+ To asynchronously initialize Spring beans, you currently have to use a separate
447
+ initializing Spring bean that then invokes the `@Async` annotated method on the target,
448
+ as the following example shows:
454
449
455
450
[source,java,indent=0,subs="verbatim,quotes"]
456
451
----
@@ -504,8 +499,8 @@ used when executing a given method. The following example shows how to do so:
504
499
----
505
500
506
501
In this case, `"otherExecutor"` can be the name of any `Executor` bean in the Spring
507
- container, or it may be the name of a qualifier associated with any `Executor` (for example, as
508
- specified with the `<qualifier>` element or Spring's `@Qualifier` annotation).
502
+ container, or it may be the name of a qualifier associated with any `Executor` (for example,
503
+ as specified with the `<qualifier>` element or Spring's `@Qualifier` annotation).
509
504
510
505
511
506
[[scheduling-annotation-support-exception]]
@@ -673,14 +668,15 @@ invoked on that object. The following listing shows a simple example:
673
668
----
674
669
675
670
The scheduler is referenced by the outer element, and each individual
676
- task includes the configuration of its trigger metadata. In the preceding example, that
677
- metadata defines a periodic trigger with a fixed delay indicating the number of
671
+ task includes the configuration of its trigger metadata. In the preceding example,
672
+ that metadata defines a periodic trigger with a fixed delay indicating the number of
678
673
milliseconds to wait after each task execution has completed. Another option is
679
674
`fixed-rate`, indicating how often the method should be run regardless of how long
680
- any previous execution takes. Additionally, for both `fixed-delay` and `fixed-rate` tasks, you can specify an
681
- 'initial-delay' parameter, indicating the number of milliseconds to wait
682
- before the first execution of the method. For more control, you can instead provide a `cron` attribute
683
- to provide a xref:integration/scheduling.adoc#scheduling-cron-expression[cron expression].
675
+ any previous execution takes. Additionally, for both `fixed-delay` and `fixed-rate`
676
+ tasks, you can specify an 'initial-delay' parameter, indicating the number of
677
+ milliseconds to wait before the first execution of the method. For more control,
678
+ you can instead provide a `cron` attribute to provide a
679
+ xref:integration/scheduling.adoc#scheduling-cron-expression[cron expression].
684
680
The following example shows these other options:
685
681
686
682
[source,xml,indent=0]
@@ -703,9 +699,8 @@ The following example shows these other options:
703
699
All Spring cron expressions have to conform to the same format, whether you are using them in
704
700
xref:integration/scheduling.adoc#scheduling-annotation-support-scheduled[`@Scheduled` annotations],
705
701
xref:integration/scheduling.adoc#scheduling-task-namespace-scheduled-tasks[`task:scheduled-tasks` elements],
706
- or someplace else.
707
- A well-formed cron expression, such as `* * * * * *`, consists of six space-separated time and date
708
- fields, each with its own range of valid values:
702
+ or someplace else. A well-formed cron expression, such as `* * * * * *`, consists of six
703
+ space-separated time and date fields, each with its own range of valid values:
709
704
710
705
711
706
....
@@ -770,9 +765,10 @@ Here are some examples:
770
765
[[macros]]
771
766
=== Macros
772
767
773
- Expressions such as `0 0 * * * *` are hard for humans to parse and are, therefore, hard to fix in case of bugs.
774
- To improve readability, Spring supports the following macros, which represent commonly used sequences.
775
- You can use these macros instead of the six-digit value, thus: `@Scheduled(cron = "@hourly")`.
768
+ Expressions such as `0 0 * * * *` are hard for humans to parse and are, therefore,
769
+ hard to fix in case of bugs. To improve readability, Spring supports the following
770
+ macros, which represent commonly used sequences. You can use these macros instead
771
+ of the six-digit value, thus: `@Scheduled(cron = "@hourly")`.
776
772
777
773
|===
778
774
|Macro | Meaning
@@ -789,18 +785,18 @@ You can use these macros instead of the six-digit value, thus: `@Scheduled(cron
789
785
[[scheduling-quartz]]
790
786
== Using the Quartz Scheduler
791
787
792
- Quartz uses `Trigger`, `Job`, and `JobDetail` objects to realize scheduling of all kinds
793
- of jobs. For the basic concepts behind Quartz, see the
788
+ Quartz uses `Trigger`, `Job`, and `JobDetail` objects to realize scheduling of all
789
+ kinds of jobs. For the basic concepts behind Quartz, see the
794
790
https://www.quartz-scheduler.org/[Quartz Web site]. For convenience purposes, Spring
795
791
offers a couple of classes that simplify using Quartz within Spring-based applications.
796
792
797
793
798
794
[[scheduling-quartz-jobdetail]]
799
795
=== Using the `JobDetailFactoryBean`
800
796
801
- Quartz `JobDetail` objects contain all the information needed to run a job. Spring provides a
802
- `JobDetailFactoryBean`, which provides bean-style properties for XML configuration purposes.
803
- Consider the following example:
797
+ Quartz `JobDetail` objects contain all the information needed to run a job. Spring
798
+ provides a `JobDetailFactoryBean`, which provides bean-style properties for XML
799
+ configuration purposes. Consider the following example:
804
800
805
801
[source,xml,indent=0,subs="verbatim,quotes"]
806
802
----
@@ -817,9 +813,9 @@ Consider the following example:
817
813
The job detail configuration has all the information it needs to run the job (`ExampleJob`).
818
814
The timeout is specified in the job data map. The job data map is available through the
819
815
`JobExecutionContext` (passed to you at execution time), but the `JobDetail` also gets
820
- its properties from the job data mapped to properties of the job instance. So, in the following example,
821
- the `ExampleJob` contains a bean property named `timeout`, and the `JobDetail`
822
- has it applied automatically:
816
+ its properties from the job data mapped to properties of the job instance. So, in the
817
+ following example, the `ExampleJob` contains a bean property named `timeout`, and the
818
+ `JobDetail` has it applied automatically:
823
819
824
820
[source,java,indent=0,subs="verbatim,quotes",chomp="-packages"]
825
821
----
@@ -912,8 +908,8 @@ NOTE: By default, jobs will run in a concurrent fashion.
912
908
[[scheduling-quartz-cron]]
913
909
=== Wiring up Jobs by Using Triggers and `SchedulerFactoryBean`
914
910
915
- We have created job details and jobs. We have also reviewed the convenience bean that lets
916
- you invoke a method on a specific object. Of course, we still need to schedule the
911
+ We have created job details and jobs. We have also reviewed the convenience bean that
912
+ lets you invoke a method on a specific object. Of course, we still need to schedule the
917
913
jobs themselves. This is done by using triggers and a `SchedulerFactoryBean`. Several
918
914
triggers are available within Quartz, and Spring offers two Quartz `FactoryBean`
919
915
implementations with convenient defaults: `CronTriggerFactoryBean` and
@@ -944,9 +940,9 @@ The following listing uses both a `SimpleTriggerFactoryBean` and a `CronTriggerF
944
940
</bean>
945
941
----
946
942
947
- The preceding example sets up two triggers, one running every 50 seconds with a starting delay of 10
948
- seconds and one running every morning at 6 AM. To finalize everything, we need to set up the
949
- `SchedulerFactoryBean`, as the following example shows:
943
+ The preceding example sets up two triggers, one running every 50 seconds with a starting
944
+ delay of 10 seconds and one running every morning at 6 AM. To finalize everything,
945
+ we need to set up the `SchedulerFactoryBean`, as the following example shows:
950
946
951
947
[source,xml,indent=0,subs="verbatim,quotes"]
952
948
----
0 commit comments