【Spring】Spring高级话题-计划任务-@EnableScheduling
来源:互联网 发布:mac文件批量重命名 编辑:程序博客网 时间:2024/06/06 10:51
进行本示例的演示,需要先配置好Maven和spring哦、
见: http://blog.csdn.net/qq_26525215/article/details/53010442
分析
要实现计划任务,首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,然后在要执行计划任务的方法上注解@Scheduled,声明这是一个计划任务。
Spring通过@Scheduled支持多种类型的计划任务,包含cron、fixDelay、fixRate等。
在本示例中:
使用cron属性可按照指定时间执行,本例写的是每天20点07分执行;
示例
计划任务执行类
在这个类中的方法上需要@Scheduled注解配合@EnableScheduling使用。
package cn.hncu.p3.p3_taskscheduler;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;import java.text.SimpleDateFormat;import java.util.Date;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/22. * Time: 下午 10:25. * Explain:计划任务执行类 */@Servicepublic class ScheduledTaskService { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行 public void reportCurrentTime(){ System.out.println("每隔5秒执行一次 "+dateFormat.format(new Date())); } @Scheduled(cron = "0 07 20 ? * *" ) //使用cron属性可按照指定时间执行,本例指的是每天20点07分执行; //cron是UNIX和类UNIX(Linux)系统下的定时任务 public void fixTimeExecution(){ System.out.println("在指定时间 "+dateFormat.format(new Date())+" 执行"); }}
配置类
通过@EnableScheduling注解开启对计划任务的支持
package cn.hncu.p3.p3_taskscheduler;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/22. * Time: 下午 10:32. * Explain:配置类 */@Configuration@ComponentScan("cn.hncu.p3.p3_taskscheduler")@EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持public class TaskScheduleConfig {}
运行类
package cn.hncu.p3.p3_taskscheduler;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/22. * Time: 下午 10:34. * Explain:运行类 */public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskScheduleConfig.class); }}
运行结果
阅读全文
0 0
- 【Spring】Spring高级话题-计划任务-@EnableScheduling
- 【Spring】Spring高级话题-计划任务-@EnableScheduling
- Spring高级话题-计划任务-@EnableScheduling
- Spring定时任务-@EnableScheduling
- SpringBoot4-spring高级话题-spring aware,多线程,计划任务,条件注解@Conditional
- spring boot 计划任务,定时任务的注解的使用@Scheduled@EnableScheduling
- Spring Boot系列三 Spring @EnableScheduling 定时任务用法总结
- Spring @EnableScheduling 注解解析
- 【Spring】Spring高级话题-测试Spring项目
- Spring高级话题_多线程
- 【Spring】Spring高级话题-条件注解-@Condition
- spring EnableScheduling标签使用详解
- Spring高级运用之浅析多线程与计划任务
- Spring 计划任务
- Spring计划任务注意
- Spring 计划任务
- Spring 计划任务 Schedule
- Spring计划任务(定时任务)
- redis数据库
- iOS 页面传值方法汇总
- 基于深度学习的Person Re-ID(综述)
- 文件过滤器
- topgp登陆不了提示DVM connection timed out
- 【Spring】Spring高级话题-计划任务-@EnableScheduling
- ElasticSearch5.2.2 安装
- Airbnb欺诈预测机器学习模型设计:准确率和召回率的故事 发表于2015-07-15 16:14| 3926次阅读| 来源AirBNB/Data| 1 条评论| 作者Ariana Radianto
- 基于深度学习的Person Re-ID(特征提取)
- sqlserver2008R2数据导入还原的经历
- c++之标准库类型string
- org.hibernate.LazyInitializationException: failed to lazily initialize
- char s[] = "abcdef";"abcdef"是在栈区还是堆区?
- 小记:关于STM32进入低功耗模式后无法烧写程序的解决办法