@EnableScheduling和@Scheduled的使用
来源:互联网 发布:网络 第二办公室 编辑:程序博客网 时间:2024/05/25 08:14
定时任务在配置类上添加@EnableScheduling开启对定时任务的支持,在相应的方法上添加@Scheduled声明需要执行的定时任务。
其中Scheduled注解中有以下几个参数:
- cron
- zone
- fixedDelay和fixedDelayString
- fixedRate和fixedRateString
initialDelay和initialDelayString
1.cron是设置定时执行的表达式,如 0 0/5 * * * ?每隔五分钟执行一次
2.zone表示执行时间的时区
3.fixedDelay 和fixedDelayString 表示一个固定延迟时间执行,上个任务完成后,延迟多长时间执行
4.fixedRate 和fixedRateString表示一个固定频率执行,上个任务开始后,多长时间后开始执行
5.initialDelay 和initialDelayString表示一个初始延迟时间,第一次被调用前延迟的时间
配置类
package com.xingguo.logistics.controller;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")@EnableSchedulingpublic class AopConfig{}
service类
package com.xingguo.logistics.service.aspect;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;@Servicepublic 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())); }}
测试类
package com.xingguo.logistics.controller;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestController { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class); }}
测试结果:
阅读全文
0 0
- @EnableScheduling和@Scheduled的使用
- spring boot 计划任务,定时任务的注解的使用@Scheduled@EnableScheduling
- spingboot @EnableScheduling使用
- spring EnableScheduling标签使用详解
- @Scheduled 使用
- SpringBoot定时任务@EnableScheduling和cron表达式
- Spring @Scheduled定时任务的简单使用
- 使用Spring的@Scheduled实现定时任务
- 使用Spring的@Scheduled实现定时任务
- 使用Spring的@Scheduled实现定时任务
- 使用Spring的@Scheduled实现定时任务
- 使用Spring的@Scheduled实现定时任务
- 使用Spring的@Scheduled实现定时任务
- 使用Spring的@Scheduled实现定时任务
- 使用Spring的@Scheduled实现定时任务
- 使用Spring的@Scheduled实现定时任务
- 使用Spring的@Scheduled实现定时任务
- 使用Spring的@Scheduled实现定时任务
- 有用的技术网站记录
- sql注入 sqlserver篇
- oracle number类型null值的大小验证
- 容器+文件版本的通讯录
- react-native 环境搭建过程
- @EnableScheduling和@Scheduled的使用
- TCP 连接的建立与终止过程
- 阿里云ecs centos vsftpd服务配置深坑向
- NYOJ 括号配对问题(栈)
- 1486: [HNOI2009]最小圈 二分+dfs
- 初识QT
- windows下搭建Vue.js开发环境
- 编译安卓7.0源码出现make: *** [ninja_wrapper] Error 1
- 文件各种上传,离不开的表单