定时任务一般会存在中大型企业级项目中,为了减少服务器、数据库的压力往往会采用时间段性的去完成某些业务逻辑。比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回调,这种回调一般都是定时任务来完成的。还有就是报表的生成,我们一般会在客户访问量过小的时候来完成这个操作,那往往都是在凌晨。这时我们也可以采用定时任务来完成逻辑。springboot为我们内置了定时任务,我们只需要一个注解就可以开启定时为我们所用了。
在开发中,定时任务是常见的功能,在spring boot 下开发定时任务其实很简单,具体代码如下:
1、配置依赖包pom.xml
由于默认的maven仓库经常访问不了,这里采用了阿里云的maven仓库镜像。
<?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>com.example</groupid> <artifactid>demo</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>spring-boot-scheduled</name> <description>demo project for spring boot</description> <!-- 阿里云maven仓库 --> <repositories> <repository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginrepositories> <pluginrepository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginrepository> </pluginrepositories> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.4.5.release</version> <relativepath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <optional>true</optional> </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> </plugins> </build></project>
2、定制任务场景
定时任务实现,提供固定周期、固定周期延迟间隔和制定时间点执行等场景。采用@scheduled注解进行标注。
exampletimer.java
package com.example;import java.text.simpledateformat;import java.util.date;import org.springframework.scheduling.annotation.scheduled;import org.springframework.stereotype.component;@componentpublic class exampletimer { simpledateformat dateformat = new simpledateformat(hh:mm:ss); @scheduled(fixedrate = 10000) public void timerrate() { system.out.println(dateformat.format(new date())); } //第一次延迟1秒执行,当执行完后2秒再执行 @scheduled(initialdelay = 1000, fixeddelay = 2000) public void timerinit() { system.out.println(init : +dateformat.format(new date())); } //每天20点16分50秒时执行 @scheduled(cron = 50 16 20 * * ?) public void timercron() { system.out.println(current time : + dateformat.format(new date())); }}
3、启动应用程序
启动程序,需要增加@enablescheduling注解.
springbootscheduledapplication.java
package com.example;import org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.scheduling.annotation.enablescheduling;@springbootapplication@enableschedulingpublic class springbootscheduledapplication { public static void main(string[] args) { springapplication.run(springbootscheduledapplication.class, args); }}
4、输出结果
20:16:27init : 20:16:28init : 20:16:30init : 20:16:32init : 20:16:34init : 20:16:3620:16:37init : 20:16:38init : 20:16:40init : 20:16:42init : 20:16:44init : 20:16:4620:16:47init : 20:16:48current time : 20:16:50init : 20:16:50init : 20:16:52init : 20:16:54
以上就是springboot如何通过scheduled实现定时任务的详细内容。