@Import(value=SchedulingConfiguration.class)
public@interfaceEnableScheduling
Enables Spring's scheduled task execution capability, similar tofunctionality found in Spring's <task:*>
XML namespace. To be used on @Configuration
classes as follows:( 启用Spring调度任务执行功能,类似于Spring<task:*> XML命名空间中的功能。要在@Configuration类中使用如下:)
@Configuration
@EnableScheduling
public class AppConfig {
// various @Bean definitions
}
This enables detection of @Scheduled
annotations on any Spring-managed bean in the container. Forexample, given a class MyTask(
这样可以检测容器中任何Spring管理的bean上的@Scheduled注释。 例如,给定一个类MyTask)
package com.myco.tasks;
public class MyTask {
@Scheduled(fixedRate=1000)
public void work() {
// task execution logic
}
}
the following configuration would ensure that MyTask.work()
is called once every 1000 ms:(以下配置将确保每1000ms调用一次MyTask.work())
@Configuration
@EnableScheduling
public class AppConfig {
@Bean
public MyTask task() {
return new MyTask();
}
}
Alternatively, if MyTask
were annotated with @Component
, the following configuration would ensure thatits @Scheduled
method is invoked at the desired interval:( 或者,如果MyTask用@Component注释,则以下配置将确保以所需间隔调用其@Scheduled方法:)
@Configuration
@EnableScheduling
@ComponentScan(basePackages="com.myco.tasks")
public class AppConfig {
}
Methods annotated with @Scheduled
may even be declared directly within @Configuration
classes: (@Scheduled注释的方法甚至可以直接在@Configuration类中声明:)
@Configuration
@EnableScheduling
public class AppConfig {
@Scheduled(fixedRate=1000)
public void work() {
// task execution logic
}
}
By default, will be searching for an associated scheduler definition:either a unique TaskScheduler
bean in the context, or a TaskScheduler
bean named "taskScheduler" otherwise; the samelookup will also be performed for a ScheduledExecutorService
bean. If neither of the two is resolvable, a localsingle-threaded default scheduler will be created and used within the registrar. 默认情况下,将搜索关联的调度程序定义:上下文中的唯一TaskScheduler bean,否则命名为“taskScheduler”的TaskScheduler bean; 对于ScheduledExecutorService bean也将执行相同的查找。 如果两者都不可解析,则将在注册器中创建并使用本地单线程默认调度程序。
When more control is desired, a @Configuration
class may implement SchedulingConfigurer
. This allows access to the underlying ScheduledTaskRegistrar
instance. For example, the following example demonstrates howto customize the Executor
used to execute scheduled tasks: (当需要更多的控制时,@Configuration类可以实现SchedulingConfigurer。 这允许访问底层的ScheduledTaskRegistrar实例。 例如,以下示例演示了如何自定义用于执行计划任务的执行程序:)
@Configuration
@EnableScheduling
public class AppConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}
}
Note in the example above the use of @Bean(destroyMethod="shutdown")
. This ensures that the task executor is properlyshut down when the Spring application context itself is closed. (注意在上面的例子中使用@Bean(destroyMethod =“shutdown”)。这确保了在Spring应用程序上下文本身关闭时,任务执行程序被正确关闭。)
Implementing SchedulingConfigurer
also allows for fine-grained control over task registrationvia the ScheduledTaskRegistrar
. For example, the following configures theexecution of a particular bean method per a custom Trigger
implementation: (实现SchedulingConfigurer还允许通过ScheduledTaskRegistrar对任务注册进行细粒度的控制。 例如,以下配置每个自定义触发器实现的特定bean方法的执行:)
@Configuration
@EnableScheduling
public class AppConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler());
taskRegistrar.addTriggerTask(
new Runnable() {
public void run() {
myTask().work();
}
},
new CustomTrigger()
);
}
@Bean(destroyMethod="shutdown")
public Executor taskScheduler() {
return Executors.newScheduledThreadPool(42);
}
@Bean
public MyTask myTask() {
return new MyTask();
}
}
For reference, the example above can be compared to the following SpringXML configuration: (作为参考,上述示例可以与以下SpringXML配置进行比较:)
<beans>
<task:annotation-driven scheduler="taskScheduler"/>
<task:scheduler id="taskScheduler" pool-size="42"/>
<task:scheduled-tasks scheduler="taskScheduler">
<task:scheduled ref="myTask" method="work" fixed-rate="1000"/>
</task:scheduled-tasks>
<bean id="myTask" class="com.foo.MyTask"/>
</beans>
The examples are equivalent save that in XML a fixed-rate period is used instead of a custom Trigger
implementation; this is because the task:
namespace scheduled
cannot easily expose such support. This is but onedemonstration how the code-based approach allows for maximum configurabilitythrough direct access to actual componentry. (这些示例是等效的,除了在XML中使用固定速率周期而不是自定义触发器实现; 这是因为任务:命名空间调度不能轻易暴露出这样的支持。这只是一个演示,基于代码的方法如何通过直接访问实际组件来实现最大的可配置性。)
参考资料链接:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableScheduling.html
关注微信公众号和今日头条,精彩文章持续更新中。。。。。