目录:【持续更新。。。。。】
  
  spring 部分常用注解
  spring boot 学习之路1(简单入门)
  spring boot 学习之路2(注解介绍)
  spring boot 学习之路3( 集成mybatis )
  spring boot 学习之路4(日志输出)
  spring boot 学习之路5(打成war包部署tomcat)
  spring boot 学习之路6(定时任务)
  spring boot 学习之路6(集成durid连接池)
  spring boot 学习之路7(静态页面自动生效问题)
  spring boot 学习之路8 (整合websocket(1))
  spring boot 学习之路9 (项目启动后就执行特定方法)

spring boot做定时很简单,只需要加个注解就ok了 

package com.huhy.web.filters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; /**
* @Author:{huhy}
* @DATE:Created on 2017/10/12 17:54
* @Class 测试spring boot的定时任务:
*/
@Configuration
@EnableScheduling // 启用定时任务
public class SchedulingConfig { private final Logger logger = LoggerFactory.getLogger(getClass()); @Scheduled(cron = "0/5 * * * * ?") // 每20秒执行一次
public void scheduler() {
logger.info("定时任务已启动---------------- ");
} }

运行起来项目:

  


  注意:定时任务启动后只有执行结束上次任务才会继续执行,即使在指定周期内,也不会触发:

    

补充:

  

 cron表达式详解:
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。
按顺序依次为
1 秒(0~59)
2 分钟(0~59)
3 小时(0~23)
4 天(0~31)
5 月(0~11)
6 星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
7.年份(1970-2099)
其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?.
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点
"0 0 12 * * ?" 每天中午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期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
有些子表达式能包含一些范围或列表
例如:子表达式(天(星期))可以为 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”
“*”字符代表所有可能的值
“/”字符用来指定数值的增量
例如:在子表达式(分钟)里的“0/15”表示从第0分钟开始,每15分钟
在子表达式(分钟)里的“3/20”表示从第3分钟开始,每20分钟(它和“3,23,43”)的含义一样
“?”字符仅被用于天(月)和天(星期)两个子表达式,表示不指定值
当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?”
“L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写
如果在“L”前有具体的内容,它就具有其他的含义了。例如:“6L”表示这个月的倒数第6天
注意:在使用“L”参数时,不要指定列表或范围,因为这会导致问题
W 字符代表着平日(Mon-Fri),并且仅能用于日域中。它用来指定离指定日的最近的一个平日。大部分的商业处理都是基于工作周的,所以 W 字符可能是非常重要的。
例如,日域中的 15W 意味着 "离该月15号的最近一个平日。" 假如15号是星期六,那么 trigger 会在14号(星期五)触发,因为星期四比星期一离15号更近。
C:代表“Calendar”的意思。它的意思是计划所关联的日期,如果日期没有被关联,则相当于日历中所有日期。例如5C在日期字段中就相当于日历5日以后的第一天。1C在星期字段中相当于星期日后的第一天。
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /

@Scheduled所支持的参数:

1.cron:cron表达式,指定任务在特定时间执行;
2.fixedDelay:表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;
3.fixedDelayString:与fixedDelay含义一样,只是参数类型变为String;
4.fixedRate:表示按一定的频率执行任务,参数类型为long,单位ms;
5.fixedRateString: 与fixedRate的含义一样,只是将参数类型变为String;
6.initialDelay:表示延迟多久再第一次执行任务,参数类型为long,单位ms;
7.initialDelayString:与initialDelay的含义一样,只是将参数类型变为String;
8.zone:时区,默认为当前时区,一般没有用到。

补充:springboot的多线程定时任务:

定时任务基类:

  

多线程定时任务配置方式:以config配置类进行实现,我在这介绍两种方式:

  方式一: 注解实现:

    

    代码很简单,就不解释了:

  方式二:springboot原生支持方式:

    

代码如下:

  

ScheduledService:

@Slf4j
@Component
//@Async
public class ScheduledService {
@Scheduled(cron = "0/5 * * * * *")
public void scheduled(){
log.info("=====>>>>>使用cron {}",System.currentTimeMillis());
}
@Scheduled(fixedRate = 5000)
public void scheduled1() {
log.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis());
}
@Scheduled(fixedDelay = 5000,initialDelay = 1000)
public void scheduled2() {
log.info("=====>>>>>fixedDelay{}",System.currentTimeMillis());
}
}


AsyncConfig:  使用这种记得在定时类或者方法上加  @Async
@Configuration
@EnableAsync
public class AsyncConfig {
/*
此处成员变量应该使用@Value从配置中读取
*/
private int corePoolSize = 10;
private int maxPoolSize = 200;
private int queueCapacity = 10;
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.initialize();
return executor;
}
}


ScheduleConfig:

@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
}
}

      

springboot 学习之路 6(定时任务)的更多相关文章

  1. springboot 学习之路 8 (整合websocket(1))

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  2. springboot 学习之路 1(简单入门)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  3. springboot 学习之路 3( 集成mybatis )

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  4. springboot 学习之路 9 (项目启动后就执行特定方法)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  5. springboot 学习之路 2(注解介绍)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  6. springboot 学习之路 4(日志输出)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  7. springboot 学习之路 5(打成war包部署tomcat)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  8. springboot 学习之路 6(集成durid连接池)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  9. springboot 学习之路 7(静态页面自动生效问题)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

随机推荐

  1. 华为机试001:字符串最后一个单词的长度(华为OJ001)

    华为机试 字符串最后一个单词的长度 计算字符串最后一个单词的长度,单词以空格隔开. 提交网址: http://www.nowcoder.com/practice/8c949ea5f36f422594b ...

  2. Android切换横竖屏不销毁前台Activity,也不影响后台Activity

    在切换屏幕方向的时候,Activity默认会走销毁->重建的生命周期,而有时候我们不希望如此,就需要做些额外的设置了: 1.在AndroidMainifest.xml中对应的Activity标签 ...

  3. Linux 在文件中查找某字符串

    命令: grep 'word' filename 在多个文件中查找: grep 'word' file1 file2 file3 更多用法参考:https://www.howtoforge.com/t ...

  4. 【原创】深入理解c++的右值引用

    0 左值和右值     一个左值表达式代表的是对象本身,而右值表达式代表的是对象的值:变量也是左值.   1 右值引用作用 为了支持移动操作(包括移动构造函数和移动赋值函数),C++才引入了一种新的引 ...

  5. Java 容器源码分析之 ArrayList

    概览 ArrayList是最常使用的集合类之一了.在JDK文档中对ArrayList的描述是:ArrayList是对list接口的一种基于可变数组的实现.ArrayList类的声明如下: 12 pub ...

  6. php-fpm无法使用系统环境变量的解决方法

    为了防止任意环境变量到达php-fpm进程,默认默认php-fpm是会清空系统环境变量的, 解决办法 修改php-fpm配置的clear_env = no (默认是yes)

  7. C# 锁系列目录

    1.lock.Monitor lock(obj){} 编译之后是如下代码 Monitor.Enter(obj); try { // } finally { Monitor.Exit(obj); } 2 ...

  8. 基于FineUIMVC的代码生成器(传统三层)v1.0-2

    第一篇博客只是粗略说明了一下,其实这个工具真正用话可能大家还要细看下,我今天(连夜)写个例子,截几个图,做一下自定义模板的实例教程,因为代码生成本身是个工具,动画效果都是次要的,主要是工具本身,其中自 ...

  9. 第12章 Linux配置定时任务详解

    12.1 配置定时任务 首先需弄清的概念: (1).crond是一个daemon类程序,路径为/usr/sbin/crond.默认会以后台方式启动,service或systemd方式启动crond默认 ...

  10. SpringBoot之Swagger2的使用

    Swagger是一组围绕OpenAPI规范构建的开源工具,可帮助设计.构建.记录和使用REST API.简单说下,它的出现就是为了方便进行测试后台的restful形式的接口,实现动态的更新,当我们在后 ...