SpringBoot 学习记录(十): scheduler
来源:互联网 发布:ppt图表无法修改数据 编辑:程序博客网 时间:2024/06/05 04:05
这篇我们来学习如何在spring_boot中启用定时任务。
一,先说明下定时表达式
"0 0 12 * * ?" 每天中午十二点触发"0 15 10 ? * *" 每天早上10:15触发"0 15 10 * * ?" 每天早上10:15触发"0 15 10 * * ? *" 每天早上10:15触发"0 15 10 * * ? 2005" 2005年的每天早上10:15触发“0 * 14 * * ?" 每天从下午2点开始到2点59分每分钟一次触发"0 0/5 14 * * ?" 每天从下午2点开始到2:55分结束每5分钟一次触发"0 0/5 14,18 * * ?" 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发"0 0-5 14 * * ?" 每天14:00至14:05每分钟一次触发"0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44触发"0 15 10 ? * MON-FRI" 每个周一、周二、周三、周四、周五的10:15触发二,新建定时任务
package com.example.scheduler;import java.text.SimpleDateFormat;import java.util.Date;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class SchedulerTask {Logger logger = LoggerFactory.getLogger(this.getClass());private int count = 0;private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");/** * 每秒钟执行一次 */@Scheduled(cron = "*/1 * * * * ?")private void process() {logger.info("======================= this is scheduler task runing " + (count++));}/** * 每5秒执行一次 */ @Scheduled(fixedRate = 1000*5) public void reportCurrentTime() { logger.info("======================= 当前时间:" + dateFormat.format(new Date())); }}启动服务,查看控制台:
2017-04-14 21:22:14.213 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= 当前时间:21:22:142017-04-14 21:22:14.307 INFO 1708 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8088 (http)2017-04-14 21:22:14.310 DEBUG 1708 --- [ main] o.s.w.c.s.StandardServletEnvironment : Adding [server.ports] PropertySource with highest search precedence2017-04-14 21:22:14.318 INFO 1708 --- [ main] com.example.DemoApplication : Started DemoApplication in 11.796 seconds (JVM running for 12.361)2017-04-14 21:22:15.002 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= this is scheduler task runing 02017-04-14 21:22:16.001 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= this is scheduler task runing 12017-04-14 21:22:17.001 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= this is scheduler task runing 22017-04-14 21:22:18.001 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= this is scheduler task runing 32017-04-14 21:22:19.002 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= this is scheduler task runing 42017-04-14 21:22:19.203 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= 当前时间:21:22:192017-04-14 21:22:20.001 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= this is scheduler task runing 52017-04-14 21:22:21.001 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= this is scheduler task runing 62017-04-14 21:22:22.001 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= this is scheduler task runing 72017-04-14 21:22:23.001 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= this is scheduler task runing 82017-04-14 21:22:24.001 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= this is scheduler task runing 92017-04-14 21:22:24.204 INFO 1708 --- [pool-2-thread-1] com.example.scheduler.SchedulerTask : ======================= 当前时间:21:22:24一个简单的定时任务就完成了。
==================================================================================================
由于最近的项目需要配合写前端页面,趁热打铁,接下来想总结一下前端相关知识,下个系列预定为java_web 学习记录
关于spring_boot还有很多需要学习的知识点,这里只是暂时告一段落,
大家可以移步博客继续学习:http://412887952-qq-com.iteye.com/category/356333
最后提供SpringBoot 学习系列源码供大家参考:https://pan.baidu.com/s/1nuRHQw5
0 0
- SpringBoot 学习记录(十): scheduler
- springboot学习笔记(十) RabbitMQ
- springboot学习记录一、SpringBoot简介
- SpringBoot学习记录1------启动
- Springboot学习记录4------profile
- SpringBoot学习记录5------logback
- SpringBoot 学习记录(二): mybaits
- SpringBoot 学习记录(三): jpa
- SpringBoot 学习记录(六): Exception
- SpringBoot 学习记录(七): Redis
- SpringBoot 学习记录(九): Email
- springBoot 学习记录(一)
- SpringBoot 学习记录(五): aop记录日志
- SpringBoot学习-(十)SpringBoot日志处理
- SpringBoot学习记录2------定时器的使用
- SpringBoot学习记录3------外部配置文件yml
- SpringBoot 学习记录(一): 简单web项目
- SpringBoot 学习记录(四): slf4j+logback
- hdu 2319
- 判断文件是否为二进制
- numpy找出array中的最大值,最小值
- Hibernate 未联网情况下不能根据dtd解析hbm.xml
- F
- SpringBoot 学习记录(十): scheduler
- 第七届 山东省ACM Proxy(SPFA+反向建边)
- strcat标准库函数
- 性能优化十六之Wake_Lock唤醒锁以及JobScheduler使用
- POJ2395Out of Hay(最小生成树Kruskal模板)
- Spring访问数据的方式
- PL/SQL 循环语句
- Reconstruct Itinerary
- 键盘操作