diff --git a/spring-framework-reference/src/scheduling.xml b/spring-framework-reference/src/scheduling.xml index c25d60d68c..4ab19b9445 100644 --- a/spring-framework-reference/src/scheduling.xml +++ b/spring-framework-reference/src/scheduling.xml @@ -3,7 +3,7 @@ "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> - Asynchronous Execution, Scheduling, and Thread Pooling + Task Execution and Scheduling
Introduction @@ -479,9 +479,9 @@ public class TaskExecutorExample { enumeration of values available for the 'rejection-policy' attribute on the 'executor' element. ]]> + size="5-25" + queue-capacity="100" + rejection-policy="CALLER_RUNS"/>]]>
@@ -496,10 +496,10 @@ public class TaskExecutorExample { the name of a method to be invoked on that object. Here is a simple example. - - + + - ]]> +]]> As you can see, the scheduler is referenced by the outer element, and each individual task includes the configuration of its trigger @@ -508,15 +508,109 @@ public class TaskExecutorExample { "fixed-rate", or for more control, a "cron" attribute could be provided instead. Here's an example featuring these other options. - - - + + + - ]]> +]]>
-
+
+ Annotation Support for Scheduling and Asynchronous Execution + + Spring 3.0 also adds annotation support for both task scheduling + and asynchronous method execution. + +
+ The @Scheduled Annotation + + The @Scheduled annotation can be added to a method along with + trigger metadata. For example, the following method would be invoked + every 5 seconds with a fixed delay, meaning that the period will be + measured from the completion time of each preceding invocation. + + If a fixed rate execution is desired, simply change the property + name specified within the annotation. The following would be executed + every 5 seconds measured between the successive start times of each + invocation. + + If simple periodic scheduling is not expressive enough, then a + cron expression may be provided. For example, the following will only + execute on weekdays. + + Notice that the methods to be scheduled must have void returns + and must not expect any arguments. If the method needs to interact with + other objects from the Application Context, then those would typically + have been provided through dependency injection. +
+ +
+ The @Async Annotation + + The @Async annotation can be provided on a method so that + invocation of that method will occur asynchronously. In other words, + the caller will return immediately upon invocation and the actual + execution of the method will occur in a task that has been submitted to + a Spring TaskExecutor. In the simplest case, the annotation may be + applied to a void-returning method. + + + + Unlike the methods annotated with the @Scheduled annotation, + these methods can expect arguments, because they will be invoked + in the "normal" way by callers at runtime rather than from a + scheduled task being managed by the container. For example, the + following is a legitimate application of the @Async annotation. + + + + Even methods that return a value can be invoked asynchronously. + However, such methods are required to have a Future typed return value. + This still provides the benefit of asynchronous execution so that the + caller can perform other tasks prior to calling 'get()' on that Future. + + returnSomething(int i) { + // this will be executed asynchronously +}]]> +
+ +
+ The <annotation-driven> Element + + To enable both @Scheduled and @Async annotations, simply + include the 'annotation-driven' element from the task namespace in + your configuration. + + + + +}]]> + + Notice that an executor reference is provided for handling + those tasks that correspond to methods with the @Async annotation, + and the scheduler reference is provided for managing those methods + annotated with @Scheduled. +
+
+ +
Using the OpenSymphony Quartz Scheduler Quartz uses Trigger, Job and JobDetail objects to realize scheduling of all kinds of jobs.