Like other configurations in Spring, asynchronous execution and scheduling of tasks can be done in following ways, namely XML, Annotation and Java based. We will cover XML and annotation based configuration support provided by Spring in this section of the chapter. For XML-based configuration, we will see the namespace support and for annotation-based configuration, we will see important annotations provided by the Spring Framework.
Spring 3.0 introduced new XML namespace for asynchronous execution and scheduling of tasks in the form of task namespace.
Namespace <task: />
We will now examine various tags in the task namespace in some detail with code snippets so as to understand these tags more clearly.
The “scheduler” defines a ThreadPoolTaskScheduler instance with configurable pool size. Te various attributes in this element is shown in the Table 20-1 below.
Table 20-1. Attributes of element “scheduler”
|Id||Required||String||The bean name for the generated ThreadPoolTaskScheduler instance. It will also be used as the default thread name prefix.|
|pool-size||Optional||String||The size of the ScheduledExecutorService’s thread pool. The default is 1.|
The “executor” element defines a ThreadPoolTaskExecutor instance with configurable pool size, queue-capacity, keep-alive, and rejection-policy values. The various attributes in this element is shown in Table 20-1 below.
Table 20-1. Attributes of element “executor”
|Id||Required||String||The bean name for the generated ThreadPoolTaskExecutor instance. This value will also be used as the thread name prefix which is why it is required even when defining the executor as an inner bean: The executor won’t be directly accessible then but will nevertheless use the specified id as the thread name prefix of the threads that it manages.|
|pool-size||Optional||String||The size of the executor’s thread pool as either a single value or a range (e.g. 5-10). If no bounded queue-capacity value is provided, then a max value has no effect unless the range is specified as 0-n. In that case, the core pool will have a size of n, but the ‘allowCoreThreadTimeout’ flag will be set to true. If a queue-capacity is provided, then the lower bound of a range will map to the core size and the upper bound will map to the max size. If this attribute is not provided, the default core size will be 1, and the default max size will be Integer.MAX_VALUE (i.e. unbounded).|
|queue-capacity||Optional||String||Queue capacity for the ThreadPoolTaskExecutor. If not specified, the default will be Integer.MAX_VALUE (i.e. unbounded).|
|keep-alive||Optional||String||Keep-alive time in seconds. Inactive threads that have been created beyond the core size will timeout after the specified number of seconds elapse. If the executor has an unbounded queue capacity and a size range represented as 0-n, then the core threads will also be configured to timeout when inactive. Otherwise, core threads will not ever timeout.|
|rejection-policy||Optional||The RejectedExecutionHandler type. When a bounded queue cannot accept any additional tasks, this determines the behavior. While the default is ABORT, consider using CALLER_RUNS to throttle inbound tasks. In other words, by forcing the caller to run the task itself, it will not be able to provide another task until after it completes the task at hand. In the meantime, one or more tasks may be removed from the queue. Alternatively, if it is not critical to run every task, consider using DISCARD to drop the current task or DISCARD_OLDEST to drop the task at the head of the queue.|
It is one of the top-level element that contains one or more task sub-elements to be managed by a given TaskScheduler. The sub-element namely “scheduled” is required atleast once and maximum of any number. It has a mandatory element namely “scheduler”, which refers to an instance of org.springframework.scheduling.TaskScheduler to manage the provided tasks. If not specified, the default value will be a wrapper for a single-threaded Executor.
It is the sub-element defining a scheduled method-invoking task and its corresponding trigger. Te various attributes in this element is shown in the Table 20-1 below.
Table 20-1. Attributes of sub-element “scheduled”
|cron||Optional||String||A cron-based trigger. See the org.springframework.scheduling.support.CronSequenceGenerator JavaDoc for example patterns.|
|fixed-delay||Optional||String||An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.|
|fixed-rate||Optional||String||An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.|
|ref||Required||String||Reference to an object that provides a method to be invoked.|
|method||Required||String||The name of the method to be invoked.|
This element enables the detection of @Async and @Scheduled annotations on any Spring-managed object. If present, a proxy will be generated for executing the annotated methods asynchronously. The various attributes in the element “annotation-driven” is shown in Table 20-1 below.
Table 20-1. Attributes of element “annotation-driven”
|executor||Optional||String||Specifies the java.util.Executor instance to use when invoking asynchronous methods. If not provided, an instance of org.springframework.core.task.SimpleAsyncTaskExecutor will be used by default.|
|scheduler||Optional||String||Specifies the org.springframework.scheduling.TaskScheduler or java.util.ScheduledExecutorService instance to use when invoking scheduled methods. If no reference is provided, a TaskScheduler backed by a single thread scheduled executor will be used.|
|mode||Should annotated beans be proxied using Spring’s AOP framework, or should they rather be weaved with an AspectJ async execution aspect? AspectJ weaving requires spring-aspects.jar on the classpath, as well as load-time weaving (or compile-time weaving) enabled. Note: The weaving-based aspect requires the @Async annotation to be defined on the concrete class. Annotations in interfaces will not work in that case (they will rather only work with interface-based proxies). Default value of this attribute is “proxy”. Other value possible is “aspectj”.|
|proxy-target-class||Boolean||Are class-based (CGLIB) proxies to be created? By default, standard Java interface-based proxies are created. Note: Class-based proxies require the @Async annotation to be defined on the concrete class. Annotations in interfaces will not work in that case (they will rather only work with interface-based proxies)|
Page Visitors: 24320