Springboot通过Scheduled实现定时任务代码

 更新时间:2017年11月27日 11:47:46   作者:lirenqing  
这篇文章主要介绍了Springboot通过Scheduled实现定时任务代码,具有一定参考价值,需要的朋友可以了解下。

定时任务一般会存在中大型企业级项目中,为了减少服务器、数据库的压力往往会采用时间段性的去完成某些业务逻辑。比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回调,这种回调一般都是定时任务来完成的。还有就是报表的生成,我们一般会在客户访问量过小的时候来完成这个操作,那往往都是在凌晨。这时我们也可以采用定时任务来完成逻辑。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;
@Component
public 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
@EnableScheduling
public class SpringBootScheduledApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringBootScheduledApplication.class, args);
	}
}

4、输出结果

20:16:27
init : 20:16:28
init : 20:16:30
init : 20:16:32
init : 20:16:34
init : 20:16:36
20:16:37
init : 20:16:38
init : 20:16:40
init : 20:16:42
init : 20:16:44
init : 20:16:46
20:16:47
init : 20:16:48
current time : 20:16:50
init : 20:16:50
init : 20:16:52
init : 20:16:54

总结

以上就是本文关于Springboot通过Scheduled实现定时任务代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

Spring boot跨域设置实例详解

快速了解Spring Boot

浅谈Springboot之于Spring的优势

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

  • Mybatis-Plus insertBatch执行缓慢的原因查询

    Mybatis-Plus insertBatch执行缓慢的原因查询

    这篇文章主要介绍了Mybatis-Plus insertBatch执行缓慢的原因查询,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • spring系列笔记之常用注解

    spring系列笔记之常用注解

    这篇文章主要给大家介绍了关于spring系列笔记之常用注解的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • 详解Java修饰符

    详解Java修饰符

    Java语言提供了很多修饰符,主要分为以下两类:访问修饰符;非访问修饰符。修饰符用来定义类、方法或者变量,通常放在语句的最前端。我们通过下面的例子来说明,下面就跟小编一起来看下吧
    2016-12-12
  • springbean的八种加载方式汇总

    springbean的八种加载方式汇总

    这篇文章主要介绍了springbean的八种加载方式,一种是XML方式声明bean,使用@Component及其衍生注解@Controller 、@Service、@Repository定义bean,还有其他方法,本文给大家介绍的非常详细,需要的朋友可以参考下
    2022-10-10
  • 关于Java中Json的各种处理

    关于Java中Json的各种处理

    这篇文章主要介绍了关于Java中Json的各种处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • Mybatis Mapper中多参数方法不使用@param注解报错的解决

    Mybatis Mapper中多参数方法不使用@param注解报错的解决

    这篇文章主要介绍了Mybatis Mapper中多参数方法不使用@param注解报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教。
    2022-01-01
  • 使用SpringBoot注解方式处理事务回滚实现

    使用SpringBoot注解方式处理事务回滚实现

    这篇文章主要介绍了使用SpringBoot注解方式处理事务回滚实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • 应用Java泛型和反射导出CSV文件的方法

    应用Java泛型和反射导出CSV文件的方法

    这篇文章主要介绍了应用Java泛型和反射导出CSV文件的方法,通过一个自定义函数结合泛型与反射的应用实现导出CSV文件的功能,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12
  • SpringCloud之Feign代理,声明式服务调用方式

    SpringCloud之Feign代理,声明式服务调用方式

    这篇文章主要介绍了SpringCloud之Feign代理,声明式服务调用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 详解Java中日期工具类的操作

    详解Java中日期工具类的操作

    这篇文章主要为大家详细介绍了Java中日期工具类的常见操作,如:字符串和Date互转、字符串和LocalDate互转等,感兴趣的小伙伴可以学习一下
    2022-11-11

最新评论