打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Spring注解方式实现任务调度

原文:http://docs.spring.io/spring/docs/4.0.1.BUILD-SNAPSHOT/javadoc-api/

注解类型:EnableScheduling

@Target(value=TYPE) @Retention(value=RUNTIME)@Import(value=SchedulingConfiguration.class)@Documentedpublic @interface EnableScheduling

使用该注解让Spring可以进行任务调度,功能类似于Spring的xml命名空间<task:*>

使用 @EnableScheduling 注解的类示例:

@Configuration@EnableSchedulingpublic class AppConfig {    // 各种@bean的定义    // various @Bean definitions}

使用@Scheduled注解可以被Spring容器检测。使用示例:

package com.myco.tasks;public class MyTask {     @Scheduled(fixedRate=1000)     public void work() {         // 任务执行逻辑         // task execution logic     } }

下面的配置使MyTask.work()每1000毫秒被执行一次:

@Configuration@EnableSchedulingpublic class AppConfig {    @Bean    public MyTask task() {        return new MyTask();    }}

如果MyTask使用了@Component注解,下面的配置方式也可以让使用了@Scheduled注解的方法在设置的时间间隔里面被调用:

@Configuration@ComponentScan(basePackages="com.myco.tasks")public class AppConfig {}

使用了@Scheduled注解方法也可以在使用了@Configuration注解的类里面使用:

@Configuration@EnableSchedulingpublic class AppConfig {    @Scheduled(fixedRate=1000)    public void work() {        // task execution logic    }}

上面介绍的都是在单线程的情况下执行任务调度的。如果希望进行更多的控制,我们可以让使用@Configuration注解的类实现SchedulingConfigurer接口,这样就可以访问底层的ScheduledRegistrar实例。

下面的例子演示如何定制Executer去执行任务计划:

@Configuration@EnableSchedulingpublic class AppConfig implements SchedulingConfigurer {    @Override    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {        taskRegistrar.setScheduler(taskExecutor());    }    @Bean(destroyMethod="shutdown")    public Executor taskExecutor() {        return Executors.newScheduledThreadPool(100);    }}

注意上面例子中使用的@bean(destroyMethod="shutdown")。这样是为了确保当Spring应用上下文关闭的时候任务执行者也被正确地关闭。实现SchedulingConfigurar接口还允许细粒度控制任务通过ScheduledTaskRegistrar进行登记。

例如,下面的配置使用自定义的Trigger执行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();     } }

作为参考,上面的例子和下面使用XML配置方式的作用是一样的:

<beans>    <task:annotation-driven scheduler="taskScheduler"/>    <task:scheduler id="taskScheduler" pool-size="42"/>    <task:scheduled ref="myTask" method="work" fixed-rate="1000"/>    <bean id="myTask" class="com.foo.MyTask"/></beans>


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
spring定时任务+线池程实现
spring @Bean注解的使用
使用Java方式配置Spring
spring自带的定时任务功能,基于注解和xml配置
Spring 3实现定时任务
Spring 使用注解方式进行事务管理
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服