本文介绍SpringBoot定时任务的使用,springboot默认已经帮我们实行了,只需要添加相应的注解就可以实现。

1.pom配置文件

pom包里面只需要引入springboot starter包即可。

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

2、启动类启用定时

在启动类上面加上@EnableScheduling即可开启定时。

@SpringBootApplication
@EnableScheduling
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}

3、创建定时任务实现类

import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class SchedulerTask { private int count=;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(cron="*/6 * * * * ?")
private void process(){
System.out.println("this is scheduler task runing "+(count++));
} @Scheduled(fixedRate = )
public void reportCurrentTime() {
System.out.println("现在时间:" + dateFormat.format(new Date()));
}
}

参数说明:

@Scheduled 参数可以接受两种定时的设置,一种是我们常用的cron=”*/6 * * * * ?”,一种是 fixedRate = 6000,两种都表示每隔六秒打印一下内容。
fixedRate 说明
@Scheduled(fixedRate = 6000) :上一次开始执行时间点之后6秒再执行;
@Scheduled(fixedDelay = 6000) :上一次执行完毕时间点之后6秒再执行;
@Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次。

控制台输出:

如果使用static变量,假设 private static boolean isStop=false;
在定时器的方法中首行加一行代码:if(isStop) return;
如果需要停止定时器,你写个页面,控制isStop的值就可以开关定时器了。

⑤SpringBoot之定时任务的更多相关文章

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

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

  2. SpringBoot 配置定时任务

    SpringBoot启用定时任务,其内部集成了成熟的框架,因此我们可以很简单的使用它. 开启定时任务 @SpringBootApplication //设置扫描的组件的包 @ComponentScan ...

  3. SpringBoot - 添加定时任务

    SpringBoot 添加定时任务 EXample1: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spri ...

  4. springboot之定时任务

    定时线程 说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务. 回顾一下定时线程池. public static ScheduledExecutorService newScheduledT ...

  5. SpringBoot整合定时任务和异步任务处理 3节课

    1.SpringBoot定时任务schedule讲解   定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类        ...

  6. 十三、springboot集成定时任务(Scheduling Tasks)

    定时任务(Scheduling Tasks) 在springboot创建定时任务比较简单,只需2步: 1.在程序的入口加上@EnableScheduling注解. 2.在定时方法上加@Schedule ...

  7. SpringBoot创建定时任务

    之前总结过spring+quartz实现定时任务的整合http://www.cnblogs.com/gdpuzxs/p/6663725.html,而springboot创建定时任务则是相当简单. (1 ...

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

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

  9. (入门SpringBoot)SpringBoot结合定时任务task(十)

    SpringBoot整合定时任务task 使用注解EnableScheduling在启动类上. 定义@Component作为组件被容器扫描. 表达式生成地址:http://cron.qqe2.com ...

  10. SpringBoot整合定时任务和异步任务处理

    SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 timer:配置比较麻烦,时间延后问题, ...

随机推荐

  1. 织梦导航 currentstyle 点击li添加class类 样式

    <!--导航开始--> <div class="global_nav_wrap"> <ul class="nav nav-pills&quo ...

  2. PHP学习之路(一)

    先前对PHP的学习研究,今总结写下心得!!

  3. JavaWeb -- Struts1 使用示例: 表单校验 防表单重复提交 表单数据封装到实体

    1. struts 工作流程图 超链接 2. 入门案例 struts入门案例: 1.写一个注册页面,把请求交给 struts处理 <form action="${pageContext ...

  4. oracle 结构化语言查询 DML DDL DCL

    --结构化查询语言 (Structured Query Language),具有定义. --查询.更新和控制等多种功能,是关系数据库的标准语言. --SQL分类: -- 数据操纵语言DML Data ...

  5. Gulp实例(包括环境搭建的自动检测)

    # Gulp实例(包括环境搭建的自动检测) Gulp是一个自动化构建工具,开发者可以使用它在项目开发过程中自动执行常见任务.下面我将完成如下的功能点并且附上源码: CSS文件打包 less文件打包 s ...

  6. mac用ssh连接linux云服务器中文乱码或无法显示解决

    问题1:服务器是ubuntu16.04,用mac自带的ssh连接后无法正常输入中文? 解:这种情况一般是终端和服务器的字符集不匹配,MacOSX下默认的是utf8字符集. 打开编辑 .bashrc 文 ...

  7. 【C#基本功】1》panel的C#用法 panel

    上面截图是panel在labview中的用法,在labview中panel加动态调用技术可以解决很多难题. 对于刚刚接触C#的我来说,如何实现pannel动态加载界面,也是一个必须首要克服的难点. 经 ...

  8. DCGAN、WGAN、WGAN-GP、LSGAN、BEGAN原理总结及对比

    DCGAN.WGAN.WGAN-GP.LSGAN.BEGAN原理总结及对比 from:https://blog.csdn.net/qq_25737169/article/details/7885778 ...

  9. Job for docker.service failed because the control process exited with error code. See "systemctl status do cker.service" and "journalctl -xe" for details.

    问题出现 :入手操作Docker时,安装启动后报了这个错 Job for docker.service failed because the control process exited with e ...

  10. Flask ajax 动态html 的javascript 事件失效

    $('.db_edit').click(function(){ $(".editdbproduct").val($(this).parent().parent().find('.e ...