spring-boot 定时任务案例

简介: 1.运行环境开发工具:intellij ideaJDK版本:1.8项目管理工具:Maven 4.0.02.Maven Plugin管理pom.xml配置代码: 1 2 5 4.

1.运行环境

开发工具:intellij idea

JDK版本:1.8

项目管理工具:Maven 4.0.0

2.Maven Plugin管理

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>spring-boot-scheduler</groupId>
 8     <artifactId>spring-boot-scheduler</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <!-- Spring Boot 启动父依赖 -->
12     <parent>
13         <groupId>org.springframework.boot</groupId>
14         <artifactId>spring-boot-starter-parent</artifactId>
15         <version>1.5.6.RELEASE</version>
16     </parent>
17 
18     <dependencies>
19         <!-- Spring Boot web依赖 -->
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-web</artifactId>
23         </dependency>
24         <!-- Spring Boot test依赖 -->
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-test</artifactId>
28             <scope>test</scope>
29         </dependency>
30     </dependencies>
31 
32 
33 </project>
View Code

3.Application启动类编写

通过@EnableScheduling激活上下文中的所有定时任务;如果我们没有这个标注,所有@Scheduled标注都不会执行。

 1 package com.goku.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.web.servlet.ServletComponentScan;
 6 import org.springframework.scheduling.annotation.EnableScheduling;
 7 
 8 /**
 9  * Created by nbfujx on 2017/11/20.
10  */
11 // Spring Boot 应用的标识
12 @SpringBootApplication
13 @ServletComponentScan
14 @EnableScheduling
15 public class DemoApplication {
16 
17     public static void main(String[] args) {
18         // 程序启动入口
19         // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
20         SpringApplication.run(DemoApplication.class,args);
21     }
22 }
View Code

4.Scheduled Task创建

通过@Scheduled标注表示这个方法是需要定时执行的任务。

 1 package com.goku.demo.scheduler;
 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 nbfujx on 2017/12/6.
10  */
11 @Component
12 public class ExampleScheduler {
13 
14     private final Logger logger = LoggerFactory.getLogger(getClass());
15 
16     @Scheduled(cron = "*/5 * * * * * ")
17     public void loginfo() {
18        this.logger.info("this is one");
19     }
20 
21     @Scheduled(fixedRate = 5000)
22     public void loginfo2() {
23         this.logger.info("this is two");
24     }
25 
26     @Scheduled(fixedDelay = 5000)
27     public void loginfo3() {
28         this.logger.info("this is three");
29     }
30 }
View Code
  • fixedRate = 5000表示每隔5000ms,Spring scheduling会调用一次该方法,不论该方法的执行时间是多少
  • fixedDelay = 5000表示当方法执行完毕5000ms后,Spring scheduling会再次调用该方法
  • cron = "*/5 * * * * * ?"提供了一种通用的定时任务表达式,这里表示每隔5秒执行一次,更加详细的信息可以参考cron表达式。

5.查看运行情况

6.GITHUB地址

https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-scheduler

目录
相关文章
|
2月前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
47 0
|
2月前
|
Java 数据库 Spring
Spring Boot 实现定时任务的动态增删启停
Spring Boot 实现定时任务的动态增删启停
27 0
|
2月前
|
druid Java 数据库
Spring Boot的定时任务与异步任务
Spring Boot的定时任务与异步任务
|
4天前
|
Java 调度 Maven
Springboot实战篇--Springboot框架通过@Scheduled实现定时任务
Spring Boot的Scheduled定时任务无需额外Maven依赖,通过`@EnableScheduling`开启。任务调度有两种方式:fixedRate和fixedDelay,前者任务结束后立即按设定间隔执行,后者在任务完成后等待设定时间再执行。更灵活的是cron表达式,例如`0 0 3 * * ?`表示每天3点执行。实现定时任务时,需注意默认单线程执行可能导致的任务交错,可通过自定义线程池解决。
|
16天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
28 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
18天前
|
XML Java C++
【Spring系列】Sping VS Sping Boot区别与联系
【4月更文挑战第2天】Spring系列第一课:Spring Boot 能力介绍及简单实践
【Spring系列】Sping VS Sping Boot区别与联系
|
1月前
|
XML Java 数据格式
Spring(一)IOC小案例
Spring(一)IOC小案例
|
1月前
|
Java 数据库 Spring
Spring Boot 实现定时任务的动态增删启停
Spring Boot 实现定时任务的动态增删启停
18 1
|
2月前
|
前端开发 Java 网络安全
ssh(Spring+Spring mvc+hibernate)简单增删改查案例
ssh(Spring+Spring mvc+hibernate)简单增删改查案例
11 0
|
2月前
|
安全 Java 数据库
Spring Security 权限管理详解与案例
Spring Security 是 Spring 框架中用于提供认证和访问控制的模块。它保护了成千上万的应用程序,使其免受未经授权的访问。本文将详细介绍 Spring Security 的权限管理功能,并通过一个实际案例来展示其用法。
102 0