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

注解类型:EnableScheduling

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

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

使用 @EnableScheduling 注解的类示例:

@Configuration
@EnableScheduling
public 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
@EnableScheduling
public class AppConfig {
    @Bean
    public MyTask task() {
        return new MyTask();
    }
}

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

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

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

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

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

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

@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);
    }
}

注意上面例子中使用的@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>

Spring注解方式实现任务调度的更多相关文章

  1. Spring注解方式实现任务调度【官方文档翻译】

    原文:http://docs.spring.io/spring/docs/4.0.1.BUILD-SNAPSHOT/javadoc-api/ 注解类型:EnableScheduling @Target ...

  2. spring注解方式在一个普通的java类里面注入dao

    spring注解方式在一个普通的java类里面注入dao @Repositorypublic class BaseDaoImpl implements BaseDao {这是我的dao如果在servi ...

  3. (转)使用Spring注解方式管理事务与传播行为详解

    http://blog.csdn.net/yerenyuan_pku/article/details/52885041 使用Spring注解方式管理事务 前面讲解了怎么使用@Transactional ...

  4. Spring 注解方式引入配置文件

    配置文件,我以两种为例,一种是引入Spring的XML文件,另外一种是.properties的键值对文件: 一.引入Spring XML的注解是@ImportResource @Retention(R ...

  5. Spring 注解方式 实现 IOC 和 DI

    注:以下所有测试案例(最后一个除外)的测试代码都是同一个: package cn.tedu.test; import org.junit.Test; import org.springframewor ...

  6. Spring注解方式配置说明

    1.<context:annotation-config/>与<context:component-scan base-package=”XX.XX”/> 在基于主机方式配置S ...

  7. ActiveMQ学习总结(10)——ActiveMQ采用Spring注解方式发送和监听

    对于ActiveMQ消息的发送,原声的api操作繁琐,而且如果不进行二次封装,打开关闭会话以及各种创建操作也是够够的了.那么,Spring提供了一个很方便的去收发消息的框架,spring jms.整合 ...

  8. spring注解方式,异常 'sessionFactory' or 'hibernateTemplate' is required的解决方法

    做单元测试的时候,抛出异常 Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' ...

  9. ehcache整合spring注解方式

    一.简介 在hibernate中就是用到了ehcache 充当缓存.spring对ehcache也提供了支持,使用也比较简单,只需在spring的配置文件中将ehcache的ehcache.xml文件 ...

随机推荐

  1. Windows 机器上面同时安装mysql5.6 和 mysql5.7 的方法

    1. 自己遇到的两个坑: . mysql 登录的时候 需要使用-P 来指定端口号 不然默认走 呢 . mysql 5.6 和 mysql 5.7 更改用户密码的命令不一样.. 我这边浪费了很长时间: ...

  2. Spring 的java 配置方式

    Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.1@Configuration 和 @Bean Spring的Java配置方式是通过 @Configuration 和 @ ...

  3. spring boot 获取bean

    在写测试用例的时候,如果是springboot的应该加上 springboot的标签: @SpringBootTest(classes = ApplicationLoader.class) @Acti ...

  4. linux服务器运维

    1. grep正则匹配 grep -E  "([0-9]{1,3}\.){4}" filepath egrep "([0-9]{1,3}\.){4}"  fil ...

  5. python设计模式第十七天【解释器模式】

    1.应用场景 (1)解释预先定义的文法 2.代码实现 #!/usr/bin/env python #!_*_ coding:UTF-8 _*_ from abc import ABCMeta, abs ...

  6. 转载 --mysql函数大全

    控制流函数 IFNULL(expr1,expr2) 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2.IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境 ...

  7. Spring validator常用注解

    规则: 原版在这里 https://www.cnblogs.com/wjh123/p/8745473.html @AssertFalse Boolean,boolean 验证注解的元素值是false ...

  8. codeforces569B

    Inventory CodeForces - 569B Companies always have a lot of equipment, furniture and other things. Al ...

  9. 51nod 1636

    1636 教育改革 我看过题解了还下了数据,表示很惭愧不想说什么,但还是说两句吧 sol: 因为差值很小只有100,所以对数组下标存的是(选择的数值和左端点的差值) f[i][j][k]即为第i天选了 ...

  10. Spring 使用介绍(十三)—— Bean的生命周期

    一.概述 Spring Bean的完整生命周期从创建Spring容器开始,直到最终Spring容器销毁Bean,生命周期时序图如下: 二.生命周期接口分类 Bean的生命周期经历了多个接口方法的调用, ...