SpringBoot专题2----springboot与schedule的激情相拥
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的激情相拥的更多相关文章
- SpringBoot学习(八)-->SpringBoot之过滤器、监听器
本文将直接使用@WebFilter和@WebListener的方式,完成一个Filter 和一个 Listener. 过滤器(Filter)和 监听器(Listener)的注册方法和 Servlet ...
- SpringBoot基础系列-SpringBoot配置
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9990680.html SpringBoot基础系列-SpringBoot配置 概述 属性 ...
- SpringBoot:4.SpringBoot整合Mybatis实现数据库访问
在公司项目开发中,使用Mybatis居多.在 SpringBoot:3.SpringBoot使用Spring-data-jpa实现数据库访问 中,这种jpa风格的把sql语句和java代码放到一起,总 ...
- springboot(一).初识springboot以及基本项目搭建
初识springboot 以及基本项目搭建 由于新的项目需要搭建后台框架,之前的springmvc架构也使用多次,在我印象中springboot的微服务架构更轻量级更容易搭建,所以想去试试spring ...
- SpringBoot(19)---SpringBoot整合Apollo
SpringBoot(19)---SpringBoot整合Apollo 有关Apollo之前已经写了两篇文章: 1.[Apollo](1)--- Apollo入门介绍篇 2.[Apollo](2)-- ...
- SpringBoot | 1.1 SpringBoot简介
前言 本博客仅为记录与总结SpringBoot的学习笔记,资料来源: 书籍<深入浅出SpringBoot>第三版 B站尚硅谷<雷丰阳2021版SpringBoot2零基础入门> ...
- SpringBoot入门教程(九)定时任务Schedule
在日常项目运行中,我们总会有需求在某一时间段周期性的执行某个动作.比如每天在某个时间段导出报表,或者每隔多久统计一次现在在线的用户量.在springboot中可以有很多方案去帮我们完成定时器的工作,有 ...
- springBoot专题3---->springBoot与多数据源的配置
最近有点忙,更新有点慢.今天进来说说一说springBoot中如何配置多数据源. 第一,新建一个名为springBoot-mutidata的maven项目,完整的pom.xml配置如下: <?x ...
- SpringBoot专题1----springboot与mybatis的完美融合
springboot大家都知道了,搭建一个spring框架只需要秒秒钟.下面给大家介绍一下springboot与mybatis的完美融合: 首先:创建一个名为springboot-mybatis的ma ...
随机推荐
- ssh理论知识
一.spring工作原理: 1.spring mvc请所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责负责对请求进行真正的处理工作. 2.DispatcherSer ...
- centos 中GTK的安装
centos 中GTK的安装 yum install gtk*
- 如果你需要从不同的服务器(不同域名)上获取数据就需要使用跨域 HTTP 请求
Response.AppendHeader("Access-Control-Allow-Origin", "*")Response.AppendHeader(& ...
- SET IDENTITY_INSERT <Table Name> ON/OFF 转载
This command is from SQL Server. This command is to enable the users to set their own value for IDE ...
- 文件上传之 commons-fileupload(二)
对commons fileupload上传组件的简单封装 在上一篇文章<利用Jakarta commons fileupload组件实现多文件上传>中,我介绍了commons fileup ...
- 去掉IntelliJ IDEA代码编辑区域的竖线
(网络配图) 作为从事编程或者测试工作的人来说,尤其是有强迫症的,看着非常痛苦,我们来看看怎么去掉 在 Settings-> Editor-> General-> Appearanc ...
- Oracle之标示符无效
一.引言 今天使用Oracle客户端执行一条sql语句 order by colname3 结果一直提示标示符无效,以为是自己把列名写错了打开表的列,一个字母一个字母的比对,还是没有错 二.原因及解决 ...
- 如何设置UITextView不可被编辑
在项目中遇到一些需求需要把文字用UITextView来展示,但是该文字不能被编辑,只要把以下该代理方法实现就可以了 -(BOOL)textViewShouldBeginEditing:(UITextV ...
- Struts2的简单使用
一.准备工作及实例 1.解压struts-2.1.6-all.zip apps目录:struts2自带的例子程序 docs目录:官方文档. lib 目录:存放所有jar文件. Src 目录:源文件存放 ...
- 【Android Studio】之构建项目报错
问题1: 报错: Could not download fastutil.jar (it.unimi.dsi:fastutil:7.2.0): No cached version available ...