SpringBoot专题2----springboot与schedule的激情相拥

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

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

     pom.xml完整配置如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>springboot-schedule</groupId>
 8     <artifactId>springboot-schedule</artifactId>
 9     <version>1.0.1</version>
10     <packaging>war</packaging>
11 
12     <name>springboot-schedule</name>
13     <description>这是springboot定时任务</description>
14 
15     <parent>
16         <groupId>org.springframework.boot</groupId>
17         <artifactId>spring-boot-starter-parent</artifactId>
18         <version>1.4.0.RELEASE</version>
19         <relativePath></relativePath>
20     </parent>
21 
22     <properties>
23         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26 
27     <dependencies>
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter</artifactId>
31         </dependency>
32 
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-test</artifactId>
36             <scope>test</scope>
37         </dependency>
38     </dependencies>
39 
40     <build>
41         <plugins>
42             <plugin>
43                 <groupId>org.springframework.boot</groupId>
44                 <artifactId>spring-boot-maven-plugin</artifactId>
45             </plugin>
46             <!--打包跳过测试插件-->
47             <plugin>
48                 <groupId>org.apache.maven.plugins</groupId>
49                 <artifactId>maven-surefire-plugin</artifactId>
50                 <configuration>
51                     <skip>true</skip>
52                 </configuration>
53             </plugin>
54         </plugins>
55     </build>
56 </project>

  第二:创建主启动类:SpringBootApplication

  

 1 package shenlan;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.scheduling.annotation.EnableScheduling;
 5 
 6 /**
 7  * Created by wangwei on 2016/9/2.
 8  */
 9 @org.springframework.boot.autoconfigure.SpringBootApplication
10 @EnableScheduling
11 public class SpringBootApplication {
12     public static void main(String[] args){
13         SpringApplication.run(SpringBootApplication.class,args);
14     }
15 }
 @EnableScheduling这是任务所必须的一个注解,有了这个注解,我们的项目才能开启定时任务的能力。
最后一步,开启我们的测试吧:
 1 package shenlan.web;
 2 
 3 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 import org.springframework.scheduling.annotation.Scheduled;
 6 import org.springframework.stereotype.Component;
 7 
 8 /**
 9  * Created by wangwei on 2016/9/2.
10  */
11 @Component
12 public class Task {
13     private Logger logger = LoggerFactory.getLogger(this.getClass());
14 
15     @Scheduled(cron="*/3 * * * * *")
16     public void reportCurrentTime(){
17         //每三秒打印一行log
18         System.out.println("-------------------------------------");
19         logger.info("======================");
20     }
21 }

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

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

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

  schedule,你值得拥有!

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

                     

posted @ 2016-09-02 16:54  深蓝至尊  阅读(2887)  评论(0编辑  收藏  举报