Springboot如何编写非分布式定时任务代码?

2026-04-30 06:172阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计620个文字,预计阅读时间需要3分钟。

Springboot如何编写非分布式定时任务代码?

1. 核心提示:在SpringBoot项目中,我们可便捷地使用Spring的注解@Scheduled和@EnableScheduling实现定时任务的快速开发。@EnableScheduling注解用于启动定时任务功能,而@Scheduled注解则用于标记需要定时执行的方法。通过这两个注解的配合,我们可以轻松实现定时任务的自动化执行。

1. 核心注解

在springboot项目中我们可以很方便地使用spring自己的注解@Scheduled和@EnableScheduling配合来实现便捷开发定时任务。

@EnableScheduling注解的作用是发现注解@Scheduled的任务并后台执行,此注解可以加到启动类上也可以加到执行调度任务类上。

经测试,当有多个包含定时任务的类时,@EnableScheduling注解加在其中一个类上就可以保证所有定时任务的成功实现。

注意:定时任务的类上还需要配合使用@Configuration或@Component注解,这两个注解都可以。

2. 实例代码

Springboot如何编写非分布式定时任务代码?

2.1 @EnableScheduling加在启动类上;

import com.my.common.util.DateUtil; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; /** * @description: * @author: Karl * @date: 2020/10/10 */ @Component public class TestSchedule01 { @Scheduled(cron = "0 * * * * ? ") public void test() { System.out.println("我是定时任务01,我执行了" + DateUtil.formatDateByDateTime(new Date())); } }

import com.my.common.util.DateUtil; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; /** * @description: * @author: Karl * @date: 2020/10/10 */ @Configuration public class TestSchedule02 { @Scheduled(cron = "1 * * * * ? ") public void test() { System.out.println("我是定时任务02,我执行了" + DateUtil.formatDateByDateTime(new Date())); } }

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

2.1 @EnableScheduling加在任务类上;

import com.my.common.util.DateUtil; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; /** * @description: * @author: Karl * @date: 2020/10/10 */ @Component @EnableScheduling public class TestSchedule01 { @Scheduled(cron = "0 * * * * ? ") public void test() { System.out.println("我是定时任务01,我执行了" + DateUtil.formatDateByDateTime(new Date())); } }

import com.my.common.util.DateUtil; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; /** * @description: * @author: Karl * @date: 2020/10/10 */ @Configuration public class TestSchedule02 { @Scheduled(cron = "1 * * * * ? ") public void test() { System.out.println("我是定时任务02,我执行了" + DateUtil.formatDateByDateTime(new Date())); } }

注意:只需要在其中一个任务类上加上@EnableScheduling注解,所有的定时任务就都可以正常运行。

3. @Scheduled的几种用法

@Scheduled这个注解支持3种定时方式,即:cron、fixedRate和fixedDelay

cron:是以表达式的形式来表示时间,最常见;

fixedRate:表示Scheduled隔多长时间调用一次,不管任务是否执行完;

fixedDelay:表示该任务执行完后隔多长时间再调用;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计620个文字,预计阅读时间需要3分钟。

Springboot如何编写非分布式定时任务代码?

1. 核心提示:在SpringBoot项目中,我们可便捷地使用Spring的注解@Scheduled和@EnableScheduling实现定时任务的快速开发。@EnableScheduling注解用于启动定时任务功能,而@Scheduled注解则用于标记需要定时执行的方法。通过这两个注解的配合,我们可以轻松实现定时任务的自动化执行。

1. 核心注解

在springboot项目中我们可以很方便地使用spring自己的注解@Scheduled和@EnableScheduling配合来实现便捷开发定时任务。

@EnableScheduling注解的作用是发现注解@Scheduled的任务并后台执行,此注解可以加到启动类上也可以加到执行调度任务类上。

经测试,当有多个包含定时任务的类时,@EnableScheduling注解加在其中一个类上就可以保证所有定时任务的成功实现。

注意:定时任务的类上还需要配合使用@Configuration或@Component注解,这两个注解都可以。

2. 实例代码

Springboot如何编写非分布式定时任务代码?

2.1 @EnableScheduling加在启动类上;

import com.my.common.util.DateUtil; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; /** * @description: * @author: Karl * @date: 2020/10/10 */ @Component public class TestSchedule01 { @Scheduled(cron = "0 * * * * ? ") public void test() { System.out.println("我是定时任务01,我执行了" + DateUtil.formatDateByDateTime(new Date())); } }

import com.my.common.util.DateUtil; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; /** * @description: * @author: Karl * @date: 2020/10/10 */ @Configuration public class TestSchedule02 { @Scheduled(cron = "1 * * * * ? ") public void test() { System.out.println("我是定时任务02,我执行了" + DateUtil.formatDateByDateTime(new Date())); } }

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

2.1 @EnableScheduling加在任务类上;

import com.my.common.util.DateUtil; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; /** * @description: * @author: Karl * @date: 2020/10/10 */ @Component @EnableScheduling public class TestSchedule01 { @Scheduled(cron = "0 * * * * ? ") public void test() { System.out.println("我是定时任务01,我执行了" + DateUtil.formatDateByDateTime(new Date())); } }

import com.my.common.util.DateUtil; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; /** * @description: * @author: Karl * @date: 2020/10/10 */ @Configuration public class TestSchedule02 { @Scheduled(cron = "1 * * * * ? ") public void test() { System.out.println("我是定时任务02,我执行了" + DateUtil.formatDateByDateTime(new Date())); } }

注意:只需要在其中一个任务类上加上@EnableScheduling注解,所有的定时任务就都可以正常运行。

3. @Scheduled的几种用法

@Scheduled这个注解支持3种定时方式,即:cron、fixedRate和fixedDelay

cron:是以表达式的形式来表示时间,最常见;

fixedRate:表示Scheduled隔多长时间调用一次,不管任务是否执行完;

fixedDelay:表示该任务执行完后隔多长时间再调用;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。