1、pom.xml中导入必要的依赖:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0..RELEASE</version>
</parent> <dependencies>
<!-- SpringBoot 核心组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>

2、写一个springboot的启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling; @ComponentScan(basePackages = { "com.xwj.tasks" })
@EnableScheduling // 开启定时任务
@EnableAutoConfiguration
public class App { public static void main(String[] args) {
SpringApplication.run(App.class, args);
} }

注意这里一定要加上@EnableScheduling注解,用于开启定时任务

3、开始写定时任务:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class ScheduleTask { @Scheduled(fixedRate = )
// @Scheduled(cron = "0 23-25 18 * * ?")
public void testSchedule() {
System.out.println("定时任务:" + System.currentTimeMillis());
} }

解释:

  @Scheduled注解:

    1、fixedRate 以固定速率执行。以上表示每隔1秒执行一次

    2、fixedDelay 以上一个任务开始时间为基准,从上一任务开始执行后再次调用

    3、cron表达式。可以实现定时调用,表达式具体配置可参考corn表达式——用于设置定时任

  

  在使用的过程中,楼主觉得,如果只有一个定时任务,fixedRate与fixedDelay的效果是一样一样的

springboot之定时任务@Scheduled的更多相关文章

  1. SpringBoot执行定时任务@Scheduled

    SpringBoot执行定时任务@Scheduled 在做项目时,需要一个定时任务来接收数据存入数据库,后端再写一个接口来提供该该数据的最新的那一条. 数据保持最新:设计字段sign的值(0,1)来设 ...

  2. SpringBoot整合定时任务----Scheduled注解实现(一个注解全解决)

    一.使用场景 定时任务在开发中还是比较常见的,比如:定时发送邮件,定时发送信息,定时更新资源,定时更新数据等等... 二.准备工作 在Spring Boot程序中不需要引入其他Maven依赖 (因为s ...

  3. 玩转SpringBoot之定时任务@Scheduled线程池配置

    序言 对于定时任务,在SpringBoot中只需要使用@Scheduled 这个注解就能够满足需求,它的出现也给我们带了很大的方便,我们只要加上该注解,并且根据需求设置好就可以使用定时任务了. 但是, ...

  4. Springboot默认定时任务——Scheduled注解

    1.pom配置 <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

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

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

  6. 【使用篇二】SpringBoot定时任务Scheduled(14)

    在日常项目运行中,我们总会有需求在某一时间段周期性的执行某个动作.比如每天在某个时间段导出报表,或者每隔多久统计一次现在在线的用户量.在springboot中可以有很多方案去帮我们完成定时器的工作,有 ...

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

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

  8. SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数

    SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...

  9. 玩转SpringBoot之定时任务详解

    序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...

随机推荐

  1. Asp.Net Web Api中使用Swagger

    关于swagger 设计是API开发的基础.Swagger使API设计变得轻而易举,为开发人员.架构师和产品所有者提供了易于使用的工具. 官方网址:https://swagger.io/solutio ...

  2. js获取select下拉框的value值和text文本值

    介绍一种取下拉框值以及绑定下拉框数据的方法    这里用到的jquery-ui-multiselect插件 1.前台html代码 <span class="ModuleFormFiel ...

  3. Javascript/jQuery常用方法

    //字符串转成时间 function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/ ...

  4. HTML、CSS

    表格标签: 表格标签有:<table> <tr> <th> <td> 让内容居中的标签:<center> 按钮标签:<button&g ...

  5. Java的入门知识和环境配置

    JVM(Java Virtual Machine)Java虚拟机 JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的. JAVA语言非常重要 ...

  6. Union Find-547. Friend Circles

    There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...

  7. ionic的基础学习(第一篇)

    1.ioinc的头部与底部 1.Header 固定在屏幕的顶端的组件,可包含标题,左右的功能按钮.(提供很多颜色的样式,及调用不同的样式名,亦可自定义) bar-light,bar-stable,ba ...

  8. 【LOJ 2542】【PKUWC2018】 随机游走(最值反演 + 树上期望dp)

    哇我太菜啦555555 不妨钦定我们需要访问的点集为$S$,在$S$已知的情况下,我们令$f(x) $表示从$x$走到点集$S$中任意一点的期望步数. 若$x∈S$,则显然$f(x)=0$,否则$f[ ...

  9. 【.NET 4.5】新增的 Prefer 32-bit target platform

    这本来是一个很小的feature并且也没有什么模糊的地方,关键是VS把这个设置成了默认值,当默认为Any CPU的时候,application会被编译成32-bit mode. 下边是我遇到的问题,在 ...

  10. jboss5+EJB3+MDB Queue

    在使用jboss5进行MDB的试验时首先要在jboss5中配置jms 队列. 1)在jboss安装目录下:server\default\deploy\messaging 打开destinations- ...