【Spring Data Scheduled】定时任务简单demo

定时任务

在Spring Boot中使用@Scheduled注解来实现定时任务,你需要引入Spring的调度模块依赖。在pom.xml文件中添加以下依赖:

xmlCopy code<dependencies>
    <!-- 其他依赖 -->

    <!-- 引入 Spring 的调度模块依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

spring-boot-starter依赖将会自动包含Spring的调度模块,并且还会包含其他必要的Spring Boot依赖。

启用Scheduling:在你的主应用程序类中添加@EnableScheduling注解,以启用定时任务调度。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ScheduledTaskApplication {
    public static void main(String[] args) {
        SpringApplication.run(ScheduledTaskApplication.class, args);
    }
}

创建定时任务:创建一个类来包含定时任务方法,并在方法上使用@Scheduled注解。

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyScheduledTasks {

    @Scheduled(fixedRate = 5000) // 每隔5秒执行一次
    public void performTask() {
        // 定时任务逻辑
        System.out.println("定时任务执行了。");
    }
}

在这个例子中,performTask方法将会每隔5秒执行一次。你可以根据需要调整fixedRate属性。

运行应用程序:运行你的Spring Boot应用程序。定时任务将会在后台自动执行。

这个简单的例子演示了如何使用Spring Boot的@Scheduled注解来创建和执行定时任务。你可以根据需要进行更复杂的配置,比如使用fixedDelayinitialDelaycron等来满足不同的调度需求。

确保你的定时任务方法是线程安全的,并且不会阻塞线程太长时间,以免影响其他任务的调度。

如何用cron来设置定时任务,用上面的例子讲解

@Scheduled(cron = “0/10 * * * * ?”) // 每隔10秒执行一次

0/10 * * * * ?表示在每分钟的每秒的第0秒开始,每隔10秒执行一次。你可以根据需要修改cron表达式来实现更精确的调度。

秒 分 时 日 月 周
  • 秒(0-59)
  • 分(0-59)
  • 时(0-23)
  • 日(1-31)
  • 月(1-12或JAN-DEC)
  • 周(0-6或SUN-SAT)

另外,*表示每个字段的每个值,?用于不指定值,而/用于指定步长。

http://cron.ciding.cc/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值