@EnableScheduling 开启对定时任务的支持

      其中Scheduled注解中有以下几个参数:

  1.cron是设置定时执行的表达式,如 0 0/5 * * * ?每隔五分钟执行一次 秒 分 时 天 月

  2.zone表示执行时间的时区

  3.fixedDelay 和fixedDelayString 表示一个固定延迟时间执行,上个任务完成后,延迟多长时间执行

  4.fixedRate 和fixedRateString表示一个固定频率执行,上个任务开始后,多长时间后开始执行

  5.initialDelay 和initialDelayString表示一个初始延迟时间,第一次被调用前延迟的时间

配置类

import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@ComponentScan({"com.xingguo.logistics.service.aspect")

@EnableScheduling
public class AopConfig{

}

service类

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class TestService2 {

private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

//初始延迟1秒,每隔2秒
@Scheduled(fixedRateString = "2000",initialDelay = 1000)
public void testFixedRate(){
System.out.println("fixedRateString,当前时间:" +format.format(new Date()));
}

//每次执行完延迟2秒
@Scheduled(fixedDelayString= "2000")
public void testFixedDelay(){
System.out.println("fixedDelayString,当前时间:" +format.format(new Date()));
}

//每隔3秒执行一次
@Scheduled(cron="0/3 * * * * ?")
public void testCron(){
System.out.println("cron,当前时间:" +format.format(new Date()));
}
}

测试类

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestController {

public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);

}
}

@EnableScheduling注解的更多相关文章

  1. Spring boot @EnableScheduling 和 @Scheduled 注解使用例子

    前言 Spring Boot提供了@EnableScheduling和@Scheduled注解,用于支持定时任务的执行,那么接下来就让我们学习下如何使用吧: 假设我们需要每隔10秒执行一个任务,那么我 ...

  2. Spring注解之 @EnableScheduling计划任务注解

    要实现计划任务,首先通过在配置类注解@EnableScheduling来开启对计划任务的支持, 然后在要执行计划任务的方法上注解@Scheduled,声明这是一个计划任务 示例:计划任务执行类 在这个 ...

  3. 【译】Spring 4 基于TaskScheduler实现定时任务(注解)

    前言 译文链接:http://websystique.com/spring/spring-job-scheduling-with-scheduled-enablescheduling-annotati ...

  4. Spring Boot入门(三):使用Scheduled注解实现定时任务

    在程序开发的过程中,经常会使用定时任务来实现一些功能,比如: 系统依赖于外部系统的非核心数据,可以定时同步 系统内部一些非核心数据的统计计算,可以定时计算 系统内部的一些接口,需要间隔几分钟或者几秒执 ...

  5. SpringBoot 常用注解

    @SpringBootApplication  这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置. @Ena ...

  6. Spring注解方式实现任务调度

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

  7. Spring4-@Enable** 注解的实现原理

    背景 在前面的工作中使用SpringBoot的时候,我碰到了很多的使用@Enable***注解的地方,使用上也都是加在@Configuration 类注解的类上面,比如: (1)@EnableAuto ...

  8. SpringBoot中关于@Enable***的注解详解

    出处:http://blog.csdn.net/qq_26525215 @EnableAspectJAutoProxy @EnableAspectJAutoProxy注解 激活Aspect自动代理 & ...

  9. Spring 3.1新特性之二:@Enable*注解的源码,spring源码分析之定时任务Scheduled注解

    分析SpringBoot的自动化配置原理的时候,可以观察下这些@Enable*注解的源码,可以发现所有的注解都有一个@Import注解.@Import注解是用来导入配置类的,这也就是说这些自动开启的实 ...

随机推荐

  1. LAMP分离搭建WordPress

    实验环境:centos6.5 php5.3.6 http2.4.35 yum安装mysql 关闭三台主机的Selinux.iptalbes 配置apache: 解压软件包 安装依赖包:pcre-dev ...

  2. Ubuntu安装之python开发

    Ubuntu安装之python开发   什么??公司要用Ubuntu(乌班图)?不会用??怎么进行python开发??? 乌班图操作系统下载地址:http://releases.ubuntu.com/ ...

  3. 使用机房的网线 连接到自己的电脑 解决Internet没有访问权限问题

    在机房把机子上的网线拔了,插在自己的笔记本上.发现并不能用,能识别Internet,但是没有访问权限. 解决办法: 去查看机房的机子的IP地址和DNS地址,就是那根网线原本连接的那台机.(网线先别拔出 ...

  4. apk的api级别不要低于26

    谷歌要求 api级别 最低26, 在用 xbuliber打包app的时候,可以在 manifest.json增加下面的代码. "plus": { "distribute& ...

  5. mysql插入中文报错的问题

    报错:1366, "Incorrect string value: '\\xE6\\xB7\\xB1\\xE5\\x85\\xA5...' for column ' 由于公司原因之前一直在使 ...

  6. 接口自动化测试遭遇问题,excel中取出来的json串,无法使用requests去请求解决办法

    最近遭遇了一个问题,问题不大不小,想半天没想明白是哪里有问题,今天终于解决了 用python读取了excel用例中,body json字符串内容,然后requests去请求内容,结果一直报错,一直不明 ...

  7. python 保留字符

    False 假的 None 无 True 真的 and 和 as作为 assert 断言 break 打破 class 种类 continue 继续 def del 删除 elif 否则如果 else ...

  8. DD常用命令组合

    管理一个系统经常需要备份磁盘数据,那么在UNIX/Linux系统中如何备份整个分区或整个硬盘的数据呢? dd命令就可以很方便实现这个功能. 1.把一个分区复制到一个文件中 dd if=/dev/sda ...

  9. echarts设置option中的数据对象优化

    if(tab.name == 'first'){ myChart.setOption({ legend: { selected:{ [this.playNumber]:true, [this.cove ...

  10. myeclipse 修改用户名密码

    当在一台公共的电脑上开发程序时,就需要涉及到更改svn用户名和密码,不然直接用本机用户密码提交,有点不妥. win7系统解决方案: C:\Documents and Settings\Administ ...