原文: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. mysql sql执行计划

    查看Mysql执行计划 使用navicat查看mysql执行计划: 打开profile分析工具: 查看是否生效:show variable like ‘%profil%’; 查看进程:show pro ...

  2. [转帖]Docker的数据管理(volume/bind mount/tmpfs)

    Docker(十五)-Docker的数据管理(volume/bind mount/tmpfs) https://www.cnblogs.com/zhuochong/p/10069719.html do ...

  3. Mybatis测试用例

    package cn.zhangxueliang.mybatis.mapper; import static org.junit.Assert.*; import java.io.InputStrea ...

  4. Java变量类型识别的3种方式

    内容导览 反射方式,成员变量的类型判断 isInstance用法 利用泛型识别类型 测试类: package com.cxyapi.oo; import java.util.Date; import ...

  5. Django的一些操作与视图函数

    一 . Django的安装 pip install django==1.14.6 # 后面的数字是django的版本 二 .  通过命令行(cmd)来创建Django项目 1. 切换到保存项目的文件夹 ...

  6. Python自动化运维之pexpect从入门到精通

    1. 应用场景 模拟ssh, telnet远程登录, 模拟ftp文件上传 2. 安装 参考资料: <pexpect实例分析>https://www.ibm.com/developerwor ...

  7. How to goproxy

    brew install python python "/users/cuthead/desktop/phuslu-goproxy-9087f35/uploader.py" sel ...

  8. 转载 大话pcie

    原文https://blog.csdn.net/abcamus/article/details/76167747 一.PCIe DMA机制 PCIe控制器也提供DMA(Direct Memory ac ...

  9. Introduction to Dynamic SQL

    The idea of using dynamic SQL is to execute SQL that will potentially generate and execute another S ...

  10. Nginx proxy_protocol协议

    L:113