SpringBoot专题2----springboot与schedule的激情相拥
2016-09-02 16:54
375 查看
Schedule:计划,任务。就是我们常说的定时任务。这在我们做一些系统的时候,是经常需要用到的。比如:定时更新一些数据,定时更新索引,都需要这样的一个功能。
第一:创建一个名为springboot-schedule的maven项目:
pom.xml完整配置如下:
第二:创建主启动类:SpringBootApplication
这里用到了cron技术,不懂的学森可以恶补一下cron的知识哦,很有用,很实用。
启动项目后,能看见以下console输出,就代表你成功啦,恭喜你!
本博客的完整代码:https://github.com/shenlanzhizunjustwangwei/springBoot/tree/master/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专题1----springboot与mybatis的完美融合
- SpringBoot专题1----springboot与mybatis的完美融合
- SpringBoot学习之路:03.Spring Boot使用Jpa操作数据库
- 企业级 SpringBoot 教程 (七)springboot开启声明式事务
- SpringBoot基础-在Eclipse中创建跑得起来的springboot项目,非STS插件方式
- 企业级 SpringBoot 教程 (八)springboot整合mongodb
- springboot访问方法时出错:Spring Boot Application: No converter found for return value of type
- SpringBoot(一):SpringBoot DEMO总包创建及jar说明
- springboot系列教程(一)——第一个springboot程序
- SpringBoot(八)_springboot集成swagger2
- SpringBoot(五)SpringBoot基础
- SpringBoot非官方教程 | 第十五篇:Springboot整合RabbitMQ
- java鬼混笔记:springboot 5、springboot的Scheduled定时器:fixedDelay和fixedRate区别
- SpringBoot系列(三)SpringBoot 之 MVC 支持
- 怎么创建SpringBoot 项目 (Spring Boot菜鸟教学一)
- SpringBoot(一)——构建第一个SpringBoot项目
- SpringBoot学习(六)--SpringBoot集成swagger在线API文档工具
- springboot(十一):Spring boot中mongodb的使用
- SpringBoot+Maven项目实战(2):集成SpringBoot
- SpringBoot学习:Hibernate,Mybatis分别与Springboot的整合