资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Springboot怎么通过Scheduled实现定时任务

小编给大家分享一下Springboot怎么通过Scheduled实现定时任务,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

成都创新互联公司主要从事成都网站设计、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务延长,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

定时任务一般会存在中大型企业级项目中,为了减少服务器、数据库的压力往往会采用时间段性的去完成某些业务逻辑。比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回调,这种回调一般都是定时任务来完成的。还有就是报表的生成,我们一般会在客户访问量过小的时候来完成这个操作,那往往都是在凌晨。这时我们也可以采用定时任务来完成逻辑。SpringBoot为我们内置了定时任务,我们只需要一个注解就可以开启定时为我们所用了。

在开发中,定时任务是常见的功能,在spring boot 下开发定时任务其实很简单,具体代码如下:

1、配置依赖包pom.xml

由于默认的maven仓库经常访问不了,这里采用了阿里云的maven仓库镜像。



  4.0.0

  com.example
  demo
  0.0.1-SNAPSHOT
  jar

  spring-boot-scheduled
  Demo project for Spring Boot

  
  
    
      public
      aliyun nexus
      http://maven.aliyun.com/nexus/content/groups/public/
      
        true
      
    
  
  
    
      public
      aliyun nexus
      http://maven.aliyun.com/nexus/content/groups/public/
      
        true
      
      
        false
      
    
  

  
    org.springframework.boot
    spring-boot-starter-parent
    1.4.5.RELEASE
     
  

  
    UTF-8
    UTF-8
    1.8
  

  
    
      org.springframework.boot
      spring-boot-starter-web
    

    
      org.projectlombok
      lombok
      true
    
    
      org.springframework.boot
      spring-boot-starter-test
      test
    
  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  

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实现定时任务”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


当前标题:Springboot怎么通过Scheduled实现定时任务
本文来源:http://cdkjz.cn/article/psipos.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220