Schedule:计划,任务。就是我们常说的定时任务。这在我们做一些系统的时候,是经常需要用到的。比如:定时更新一些数据,定时更新索引,都需要这样的一个功能。

第一:创建一个名为springboot-schedule的maven项目:

pom.xml完整配置如下:

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>springboot-schedule</groupId>
<artifactId>springboot-schedule</artifactId>
<version>1.0.1</version>
<packaging>war</packaging> <name>springboot-schedule</name>
<description>这是springboot定时任务</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath></relativePath>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <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>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--打包跳过测试插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

  第二:创建主启动类:SpringBootApplication

  

 package shenlan;

 import org.springframework.boot.SpringApplication;
import org.springframework.scheduling.annotation.EnableScheduling; /**
* Created by wangwei on 2016/9/2.
*/
@org.springframework.boot.autoconfigure.SpringBootApplication
@EnableScheduling
public class SpringBootApplication {
public static void main(String[] args){
SpringApplication.run(SpringBootApplication.class,args);
}
}
 @EnableScheduling这是任务所必须的一个注解,有了这个注解,我们的项目才能开启定时任务的能力。
最后一步,开启我们的测试吧:
 package shenlan.web;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
* Created by wangwei on 2016/9/2.
*/
@Component
public class Task {
private Logger logger = LoggerFactory.getLogger(this.getClass()); @Scheduled(cron="*/3 * * * * *")
public void reportCurrentTime(){
//每三秒打印一行log
System.out.println("-------------------------------------");
logger.info("======================");
}
}

这里用到了cron技术,不懂的学森可以恶补一下cron的知识哦,很有用,很实用。

启动项目后,能看见以下console输出,就代表你成功啦,恭喜你!

本博客的完整代码:https://github.com/shenlanzhizunjustwangwei/springBoot/tree/master/springboot-schedule

schedule,你值得拥有!

如果你觉得本博客对你有帮助,别忘了请我喝茶哦!

                    

SpringBoot专题2----springboot与schedule的激情相拥的更多相关文章

  1. SpringBoot学习(八)-->SpringBoot之过滤器、监听器

    本文将直接使用@WebFilter和@WebListener的方式,完成一个Filter 和一个 Listener. 过滤器(Filter)和 监听器(Listener)的注册方法和 Servlet ...

  2. SpringBoot基础系列-SpringBoot配置

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9990680.html SpringBoot基础系列-SpringBoot配置 概述 属性 ...

  3. SpringBoot:4.SpringBoot整合Mybatis实现数据库访问

    在公司项目开发中,使用Mybatis居多.在 SpringBoot:3.SpringBoot使用Spring-data-jpa实现数据库访问 中,这种jpa风格的把sql语句和java代码放到一起,总 ...

  4. springboot(一).初识springboot以及基本项目搭建

    初识springboot 以及基本项目搭建 由于新的项目需要搭建后台框架,之前的springmvc架构也使用多次,在我印象中springboot的微服务架构更轻量级更容易搭建,所以想去试试spring ...

  5. SpringBoot(19)---SpringBoot整合Apollo

    SpringBoot(19)---SpringBoot整合Apollo 有关Apollo之前已经写了两篇文章: 1.[Apollo](1)--- Apollo入门介绍篇 2.[Apollo](2)-- ...

  6. SpringBoot | 1.1 SpringBoot简介

    前言 本博客仅为记录与总结SpringBoot的学习笔记,资料来源: 书籍<深入浅出SpringBoot>第三版 B站尚硅谷<雷丰阳2021版SpringBoot2零基础入门> ...

  7. SpringBoot入门教程(九)定时任务Schedule

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

  8. springBoot专题3---->springBoot与多数据源的配置

    最近有点忙,更新有点慢.今天进来说说一说springBoot中如何配置多数据源. 第一,新建一个名为springBoot-mutidata的maven项目,完整的pom.xml配置如下: <?x ...

  9. SpringBoot专题1----springboot与mybatis的完美融合

    springboot大家都知道了,搭建一个spring框架只需要秒秒钟.下面给大家介绍一下springboot与mybatis的完美融合: 首先:创建一个名为springboot-mybatis的ma ...

随机推荐

  1. 点滴积累【JS】---JS小功能(列表页面隔行变色)

    效果: 代码: <head runat="server"> <title></title> <script type="text ...

  2. javascript的弹框

    学习js最先了解到的两种种简单测试手段就是alert("blah");和console.log("blah");了. 除了alert之外,js还有两种弹框 co ...

  3. Oracle Explain plan 使用总结

    Oracle Explain plan使用总结    写多了SQL语句,伴随着数据量的海增,总会遇到性能的问题.在Oracle领域一个不好的习惯,一旦遇到性能问题就推给DBA来做.长期如此,反而对DB ...

  4. flume+kafka+storm打通过程

    0.有的地方我已经整理成脚本了,有的命令是脚本里面截取的 1.启动hadoop和yarn $HADOOP_HOME/sbin/start-dfs.sh;$HADOOP_HOME/sbin/start- ...

  5. Prerender Application Level Middleware - ASP.NET HttpModule

    In the previous post Use Prerender to improve AngularJS SEO, I have explained different solutions at ...

  6. SSH初体验系列--Hibernate--2--crud操作

    Ok,今天比较详细的学习一下hibernate的C(create).R(read).U(update).D(delete) 相关api... 前言 Session: 是Hibernate持久化操作的基 ...

  7. Meter and pixel units in a box2d game - LibGDX

    http://pimentoso.blogspot.com/2013/01/meter-and-pixel-units-in-box2d-game.html 需FQ ————————————————— ...

  8. String, StringBuffer StringBuilder的区别。

    解答:String的长度是不可变的: StringBuffer的长度是可变的,如果你对字符串中的内容经常进行操作,特别是内容要修改时,那么使用StringBuffer,如果最后需要String,那么使 ...

  9. 列出自己常用的jdk包.

    解答:JDK常用的package java.lang: 这个是系统的基础类,比如String等都是这里面的,这个package是唯一一个可以不用import就可以使用的Package java.io: ...

  10. RGB565 转换 BMP24

    今晚有心情,就做完了BMP16(RGB565) 转换成BMP24 的小程序.其中最关键的地方是: // k,WORD类型,是RGB565 的一个点的数据(可以认为是灰度). r=(k & 0x ...