Fixed XML example for setup of scheduled tasks

Issue: SPR-14145
(cherry picked from commit 550a320)
This commit is contained in:
Juergen Hoeller 2016-04-11 19:27:06 +02:00
parent dc20a32ded
commit a8418d3bcd
1 changed files with 25 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -37,6 +37,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
* @Configuration * @Configuration
* @EnableScheduling * @EnableScheduling
* public class AppConfig { * public class AppConfig {
*
* // various @Bean definitions * // various @Bean definitions
* }</pre> * }</pre>
* *
@ -47,6 +48,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
* package com.myco.tasks; * package com.myco.tasks;
* *
* public class MyTask { * public class MyTask {
*
* &#064;Scheduled(fixedRate=1000) * &#064;Scheduled(fixedRate=1000)
* public void work() { * public void work() {
* // task execution logic * // task execution logic
@ -60,6 +62,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
* &#064;Configuration * &#064;Configuration
* &#064;EnableScheduling * &#064;EnableScheduling
* public class AppConfig { * public class AppConfig {
*
* &#064;Bean * &#064;Bean
* public MyTask task() { * public MyTask task() {
* return new MyTask(); * return new MyTask();
@ -72,6 +75,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
* *
* <pre class="code"> * <pre class="code">
* &#064;Configuration * &#064;Configuration
* &#064;EnableScheduling
* &#064;ComponentScan(basePackages="com.myco.tasks") * &#064;ComponentScan(basePackages="com.myco.tasks")
* public class AppConfig { * public class AppConfig {
* }</pre> * }</pre>
@ -83,6 +87,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
* &#064;Configuration * &#064;Configuration
* &#064;EnableScheduling * &#064;EnableScheduling
* public class AppConfig { * public class AppConfig {
*
* &#064;Scheduled(fixedRate=1000) * &#064;Scheduled(fixedRate=1000)
* public void work() { * public void work() {
* // task execution logic * // task execution logic
@ -100,6 +105,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
* &#064;Configuration * &#064;Configuration
* &#064;EnableScheduling * &#064;EnableScheduling
* public class AppConfig implements SchedulingConfigurer { * public class AppConfig implements SchedulingConfigurer {
*
* &#064;Override * &#064;Override
* public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { * public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
* taskRegistrar.setScheduler(taskExecutor()); * taskRegistrar.setScheduler(taskExecutor());
@ -111,11 +117,11 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
* } * }
* }</pre> * }</pre>
* *
* Note in the example above the use of {@code @Bean(destroyMethod="shutdown")}. This * <p>Note in the example above the use of {@code @Bean(destroyMethod="shutdown")}.
* ensures that the task executor is properly shut down when the Spring application * This ensures that the task executor is properly shut down when the Spring
* context itself is closed. * application context itself is closed.
* *
* Implementing {@code SchedulingConfigurer} also allows for fine-grained * <p>Implementing {@code SchedulingConfigurer} also allows for fine-grained
* control over task registration via the {@code ScheduledTaskRegistrar}. * control over task registration via the {@code ScheduledTaskRegistrar}.
* For example, the following configures the execution of a particular bean * For example, the following configures the execution of a particular bean
* method per a custom {@code Trigger} implementation: * method per a custom {@code Trigger} implementation:
@ -124,6 +130,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
* &#064;Configuration * &#064;Configuration
* &#064;EnableScheduling * &#064;EnableScheduling
* public class AppConfig implements SchedulingConfigurer { * public class AppConfig implements SchedulingConfigurer {
*
* &#064;Override * &#064;Override
* public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { * public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
* taskRegistrar.setScheduler(taskScheduler()); * taskRegistrar.setScheduler(taskScheduler());
@ -150,22 +157,32 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
* *
* <p>For reference, the example above can be compared to the following Spring XML * <p>For reference, the example above can be compared to the following Spring XML
* configuration: * configuration:
*
* <pre class="code"> * <pre class="code">
* {@code * {@code
* <beans> * <beans>
*
* <task:annotation-driven scheduler="taskScheduler"/> * <task:annotation-driven scheduler="taskScheduler"/>
*
* <task:scheduler id="taskScheduler" pool-size="42"/> * <task:scheduler id="taskScheduler" pool-size="42"/>
*
* <task:scheduled-tasks scheduler="taskScheduler">
* <task:scheduled ref="myTask" method="work" fixed-rate="1000"/> * <task:scheduled ref="myTask" method="work" fixed-rate="1000"/>
* </task:scheduled-tasks>
*
* <bean id="myTask" class="com.foo.MyTask"/> * <bean id="myTask" class="com.foo.MyTask"/>
*
* </beans> * </beans>
* }</pre> * }</pre>
* the examples are equivalent save that in XML a <em>fixed-rate</em> period is used *
* The examples are equivalent save that in XML a <em>fixed-rate</em> period is used
* instead of a custom <em>{@code Trigger}</em> implementation; this is because the * instead of a custom <em>{@code Trigger}</em> implementation; this is because the
* {@code task:} namespace {@code scheduled} cannot easily expose such support. This is * {@code task:} namespace {@code scheduled} cannot easily expose such support. This is
* but one demonstration how the code-based approach allows for maximum configurability * but one demonstration how the code-based approach allows for maximum configurability
* through direct access to actual componentry.<p> * through direct access to actual componentry.<p>
* *
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller
* @since 3.1 * @since 3.1
* @see Scheduled * @see Scheduled
* @see SchedulingConfiguration * @see SchedulingConfiguration