1.在main启动项添加一个注解@EnableScheduling

package com.example.springmybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling
@SpringBootApplication
//@MapperScan("com.example.springmybatis.dao")
public class SpringMybatisApplication { public static void main(String[] args) {
SpringApplication.run(SpringMybatisApplication.class, args);
}
}

2.在类中添加@Component,方法上添加@Scheduled

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date; @Component
public class SchedulerTask {
//想要开启定时任务,必须在启动页加上@EnableScheduling
@Scheduled(cron="30 10 1 * * ?")
public void aTask(){
try {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date())+"*********A任务每20秒执行一次进入测试");
} catch (Exception e) {
e.printStackTrace();
}
}
}

3.cron表达式含义:

 * 0 0 10,14,16 * * ?每天上午10点、下午两点、下午4点整触发
0 0/30 9-17 * * ? 每天朝九晚五内每隔半小时触发
0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
0 0/5 * * * ?每5分钟触发
10 0/5 * * * ?每隔5分钟的第10秒触发(即10:00:10、10:05:10、10:10:10等)
30 * * * * ? 每半分钟触发
30 10 * * * ? 每小时的10分30秒触发
30 10 1 * * ? 每天1点10分30秒触发
30 10 1 20 * ? 每月20号1点10分30秒触发
30 10 1 20 10 ? * 每年10月20号1点10分30秒触发
30 10 1 20 10 ? 2011 2011年10月20号1点10分30秒触发
30 10 1 ? 10 * 2011 2011年10月每天1点10分30秒触发
30 10 1 ? 10 SUN 2011 2011年10月每周日1点10分30秒触发
15,30,45 * * * * ? 每15秒,30秒,45秒时触发
15-45 * * * * ? 15到45秒内,每秒都触发
15/5 * * * * ? 每分钟的每15秒开始触发,每隔5秒触发一次
15-30/5 * * * * ? 每分钟的15秒到30秒之间开始触发,每隔5秒触发一次
0 0/3 * * * ? 每小时的第0分0秒开始,每三分钟触发一次
0 15 10 ? * MON-FRI 星期一到星期五的10点15分0秒触发
0 15 10 L * ? 每个月最后一天的10点15分0秒触发
0 15 10 LW * ? 每个月最后一个工作日的10点15分0秒触发
0 15 10 ? * 5L 每个月最后一个星期四的10点15分0秒触发
0 15 10 ? * 5#3 每个月第三周的星期四的10点15分0秒触发

springboot使用 @EnableScheduling、@Scheduled开启定时任务的更多相关文章

  1. SpringBoot中使用@Scheduled创建定时任务

    SpringBoot中使用@Scheduled创建定时任务 定时任务一般会在很多项目中都会用到,我们往往会间隔性的的去完成某些特定任务来减少服务器和数据库的压力.比较常见的就是金融服务系统推送回调,一 ...

  2. 定时任务-----Springboot中使用Scheduled做定时任务----http://www.cnblogs.com/lirenqing/p/6596557.html

    Springboot中使用Scheduled做定时任务---http://www.cnblogs.com/lirenqing/p/6596557.html 已经验证的方案: pom文件加入依赖 < ...

  3. SpringBoot之使用Scheduled做定时任务

    定时任务有好多开源框架比如Quartz,@Scheduled是Spring的一个定时任务注解,通过注解配置就能够轻量级的定时任务,简单方便. 一.@Scheduled注解介绍 这里先贴上@Schedu ...

  4. Springboot中使用Scheduled做定时任务

    在开发中,定时任务是常见的功能,在spring boot 下开发定时任务其实很简单,具体代码如下: 1.配置依赖包pom.xml 由于默认的maven仓库经常访问不了,这里采用了阿里云的maven仓库 ...

  5. springboot开启定时任务 添加定时任务 推送

    最近在自学Java的springboot框架,要用到定时推送消息.参考了网上的教程,自己调试,终于调好了.下面将网上的教程归纳下,总结复习下.  springboot开启定时任务  在SpringBo ...

  6. SpringBoot使用@Scheduled创建定时任务

    定时任务一般会存在中大型企业级项目中,为了减少服务器.数据库的压力往往会采用时间段性的去完成某些业务逻辑.比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回 ...

  7. SpringBoot与异步任务、定时任务、邮件任务

    异步任务 在需要开启异步的服务加上注解:@Async @Service public class AsyncService { //告诉SpringBoot这是一个异步任务,SpringBoot会自动 ...

  8. SpringBoot系列:Spring Boot定时任务Spring Schedule

    Spring Schedule是Spring提供的定时任务框架,相较于Quartz,Schedule更加简单易用,在中小型应用中,对于大部分需求,Schedule都可以胜任. 一.Spring Sch ...

  9. SpringBoot第十七篇:定时任务

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11076555.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   相信大家对 ...

随机推荐

  1. 数据结构之队列java版

    //java由于泛型的擦除,用起来十分不方便 abstract class BaseQueue<T>{ abstract boolean enQueue(T x); abstract T ...

  2. Linux下安装jupyter

    又是美好的一天     开开心心写代码 1. 安装ipython, jupyter pip install ipython pip install jupyter 2. 生成配置文件[root@50e ...

  3. javascript基础案例解析

    学完了JavaScript基础部分,总结出一些基本案例,以备日后查看! 1.九九乘法口诀表:在控制台中输出九九乘法口诀表!代码如下: <!DOCTYPE html> <html> ...

  4. Elasticsearch实战 | 必要的时候,还得空间换时间!

    1.应用场景 实时数据流通过kafka后,根据业务需求,一部分直接借助kafka-connector入Elasticsearch不同的索引中. 另外一部分,则需要先做聚类.分类处理,将聚合出的分类结果 ...

  5. js动态添加<tr><td>

    <form id="addNewsFormData" name="addNewsFormData" isCheck="true" ac ...

  6. 循环while和for

    1.循环语句的基本操作 #while循环使用,其中break是用来结束当前循环的 count = 0 while True: print(count) count += 1 if count == 3 ...

  7. 性能测试学习第三天-----loadrunner接口测试&中文乱码处理

    loadrunner 接口测试:   get.post(3种参数格式).cookie及token处理.加密接口.webservice.socket.文件上传接口.文件下载接口     &  中 ...

  8. 利用WxJava实现网站集成微信登录功能,核心代码竟然不超过10行

    最近网站PC端集成微信扫码登录,踩了不少坑,在此记录下实现过程和注意事项. 本文目录 一.微信开放平台操作步骤1.创建“网站应用”2.获取AppID和AppSecret二.开发指南三.开发实战1.po ...

  9. python 09 函数

    目录 函数初识 1. 函数定义: 2. 函数调用: 3. 函数的返回值: 4. () 4.1 位置传参: 4.2 关键字传参: 4.3 混合传参: 函数初识 1. 函数定义: def 函数名(): 函 ...

  10. [Python] Django框架入门5——静态文件、中间件、上传图片和分页

    说明: 本文主要描述Django其他的内容,涉及静态文件处理.中间件.上传文件.分页等. 开发环境:win10.Python3.5.Django1.10. 一.静态文件处理 在Django项目的静态文 ...