SpringBoot中如何设置定时任务执行?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1026个文字,预计阅读时间需要5分钟。
在常规开发过程中,经常使用定时任务来执行特定操作。在Spring MVC开发中,常与Quartz框架集成以实现定时任务。但在Spring Boot中,没有直接集成Quartz,而是利用Java的线程池来实现定时任务。以下是具体内容:
一、概述在Spring MVC开发中,通常与Quartz框架结合使用定时任务。然而,Spring Boot中并未直接提供Quartz集成,而是通过Java线程池来管理定时任务。
二、在Spring Boot中使用Java线程池实现定时任务
1.创建一个配置类,用于配置线程池:
java@Configurationpublic class ThreadPoolConfig { @Bean public ExecutorTask executorTask() { ThreadPoolTaskExecutor executor=new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(10); executor.setQueueCapacity(100); executor.setThreadNamePrefix(TaskExecutor-); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; }}2.创建一个定时任务类,实现`SchedulingConfigurer`接口:
java@Component@EnableSchedulingpublic class ScheduledTasks implements SchedulingConfigurer {@Autowired private ExecutorTask executor;
@Override public void configureTasks(ScheduledTaskRegistrar registrar) { registrar.setScheduler(executor); }}
3.创建一个定时任务方法,使用`@Scheduled`注解:
java@Componentpublic class ScheduledTask {@Scheduled(fixedRate=5000) public void scheduledTask() { // 执行定时任务逻辑 }}通过以上步骤,在Spring Boot项目中即可使用Java线程池实现定时任务。
在日常的开发过程中经常使用到定时任务,在springMVC的开发中,经常和quartz框架进行集成使用,但在springboot中没有这么做,而是使用了java的线程池来实现定时任务。
一、概述
在springboot中使用定时任务非常简单,只需要简单的几步即可完成。
二、详述
在springboot中要使用定时任务,首先要保证环境是springboot的,这里使用的是springboot-2.1.2.release版本。在启动类上加@EnableScheduling注解,如下,
package com.example.demo; import com.example.demo.properties.ApplicationPro; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableConfigurationProperties({ApplicationPro.class}) //引入开启定时任务的注解 @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@EnableScheduling注解的作用就是开启对定时任务的支持,这个注解的作用是开启定时任务的自动配置。
在使用了@EnableScheduling注解后便可以编写具体的定时任务的job类,该job类无需继承或实现任何接口,只要是一个被spring管理的类即可。为了使spring可以管理统一使用@Component注解标识。在定时任务的类中的方法上标识@Scheduled注解便可以定时执行该方法,@Scheduled注解上有几种不同的属性,看具体的该注解的定义,
fixedDelay
@Scheduled(fixedDelay=1000)/@Scheduled(fixedDelay="1000")的意思是该方法执行完后每隔1000ms执行一次。看具体的代码
package com.example.demo.job; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Slf4j @Component public class SchedulFixedRelayTest { //@Scheduled(fixedDelay = 5000) public void jobTest(){ try { log.info("使用fixedDelay的定时任务"); Thread.sleep(10*1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
看执行结果,
2020-12-09 22:02:47.511 INFO 7368 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 940 ms 2020-12-09 22:02:47.681 INFO 7368 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-12-09 22:02:47.782 INFO 7368 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler' 2020-12-09 22:02:47.802 INFO 7368 --- [ scheduling-1] c.e.demo.job.SchedulFixedRelayTest : 使用fixedDelay的定时任务 2020-12-09 22:02:47.820 INFO 7368 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (cron.qqe2.com/
通过cron在线生成,分别设置值,如,0/5 * * * * ? 每隔5s执行一次,
package com.example.demo.job; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Slf4j @Component public class SchedulCronTest { @Scheduled(cron = "0/5 * * * * ?") public void jobTest(){ try { log.info("使用cron的定时任务"); Thread.sleep(10*1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
每隔5s执行一次,任务耗时10s,看下执行结果,
2020-12-09 22:41:42.718 INFO 17828 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.609 seconds (JVM running for 2.453) 2020-12-09 22:41:45.015 INFO 17828 --- [ scheduling-1] com.example.demo.job.SchedulCronTest : 使用cron的定时任务 2020-12-09 22:42:00.004 INFO 17828 --- [ scheduling-1] com.example.demo.job.SchedulCronTest : 使用cron的定时任务 2020-12-09 22:42:15.000 INFO 17828 --- [ scheduling-1] com.example.demo.job.SchedulCronTest : 使用cron的定时任务
可以看到是每隔15s执行一次,也就是如果配置的时间间隔小于任务耗时,那么在任务执行完后的时间间隔后再执行,在此种情况下和fixDelay的用法一致。
三、总结
本文分析了springboot中定时任务的使用,
首先,使用@EnableScheduling开启定时任务的自动配置;
其次,任务类必须受spring管理(使用@Component、@Service等注解均可);
最后,任务方法使用@Scheduled注解标识,该注解有3中不同的属性配置,fixedDelay、fixedRate、cron;
以上就是如何在springboot中使用定时任务的详细内容,更多关于springboot中使用定时任务的资料请关注易盾网络其它相关文章!
本文共计1026个文字,预计阅读时间需要5分钟。
在常规开发过程中,经常使用定时任务来执行特定操作。在Spring MVC开发中,常与Quartz框架集成以实现定时任务。但在Spring Boot中,没有直接集成Quartz,而是利用Java的线程池来实现定时任务。以下是具体内容:
一、概述在Spring MVC开发中,通常与Quartz框架结合使用定时任务。然而,Spring Boot中并未直接提供Quartz集成,而是通过Java线程池来管理定时任务。
二、在Spring Boot中使用Java线程池实现定时任务
1.创建一个配置类,用于配置线程池:
java@Configurationpublic class ThreadPoolConfig { @Bean public ExecutorTask executorTask() { ThreadPoolTaskExecutor executor=new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(10); executor.setQueueCapacity(100); executor.setThreadNamePrefix(TaskExecutor-); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; }}2.创建一个定时任务类,实现`SchedulingConfigurer`接口:
java@Component@EnableSchedulingpublic class ScheduledTasks implements SchedulingConfigurer {@Autowired private ExecutorTask executor;
@Override public void configureTasks(ScheduledTaskRegistrar registrar) { registrar.setScheduler(executor); }}
3.创建一个定时任务方法,使用`@Scheduled`注解:
java@Componentpublic class ScheduledTask {@Scheduled(fixedRate=5000) public void scheduledTask() { // 执行定时任务逻辑 }}通过以上步骤,在Spring Boot项目中即可使用Java线程池实现定时任务。
在日常的开发过程中经常使用到定时任务,在springMVC的开发中,经常和quartz框架进行集成使用,但在springboot中没有这么做,而是使用了java的线程池来实现定时任务。
一、概述
在springboot中使用定时任务非常简单,只需要简单的几步即可完成。
二、详述
在springboot中要使用定时任务,首先要保证环境是springboot的,这里使用的是springboot-2.1.2.release版本。在启动类上加@EnableScheduling注解,如下,
package com.example.demo; import com.example.demo.properties.ApplicationPro; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableConfigurationProperties({ApplicationPro.class}) //引入开启定时任务的注解 @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@EnableScheduling注解的作用就是开启对定时任务的支持,这个注解的作用是开启定时任务的自动配置。
在使用了@EnableScheduling注解后便可以编写具体的定时任务的job类,该job类无需继承或实现任何接口,只要是一个被spring管理的类即可。为了使spring可以管理统一使用@Component注解标识。在定时任务的类中的方法上标识@Scheduled注解便可以定时执行该方法,@Scheduled注解上有几种不同的属性,看具体的该注解的定义,
fixedDelay
@Scheduled(fixedDelay=1000)/@Scheduled(fixedDelay="1000")的意思是该方法执行完后每隔1000ms执行一次。看具体的代码
package com.example.demo.job; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Slf4j @Component public class SchedulFixedRelayTest { //@Scheduled(fixedDelay = 5000) public void jobTest(){ try { log.info("使用fixedDelay的定时任务"); Thread.sleep(10*1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
看执行结果,
2020-12-09 22:02:47.511 INFO 7368 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 940 ms 2020-12-09 22:02:47.681 INFO 7368 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-12-09 22:02:47.782 INFO 7368 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler' 2020-12-09 22:02:47.802 INFO 7368 --- [ scheduling-1] c.e.demo.job.SchedulFixedRelayTest : 使用fixedDelay的定时任务 2020-12-09 22:02:47.820 INFO 7368 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (cron.qqe2.com/
通过cron在线生成,分别设置值,如,0/5 * * * * ? 每隔5s执行一次,
package com.example.demo.job; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Slf4j @Component public class SchedulCronTest { @Scheduled(cron = "0/5 * * * * ?") public void jobTest(){ try { log.info("使用cron的定时任务"); Thread.sleep(10*1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
每隔5s执行一次,任务耗时10s,看下执行结果,
2020-12-09 22:41:42.718 INFO 17828 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.609 seconds (JVM running for 2.453) 2020-12-09 22:41:45.015 INFO 17828 --- [ scheduling-1] com.example.demo.job.SchedulCronTest : 使用cron的定时任务 2020-12-09 22:42:00.004 INFO 17828 --- [ scheduling-1] com.example.demo.job.SchedulCronTest : 使用cron的定时任务 2020-12-09 22:42:15.000 INFO 17828 --- [ scheduling-1] com.example.demo.job.SchedulCronTest : 使用cron的定时任务
可以看到是每隔15s执行一次,也就是如果配置的时间间隔小于任务耗时,那么在任务执行完后的时间间隔后再执行,在此种情况下和fixDelay的用法一致。
三、总结
本文分析了springboot中定时任务的使用,
首先,使用@EnableScheduling开启定时任务的自动配置;
其次,任务类必须受spring管理(使用@Component、@Service等注解均可);
最后,任务方法使用@Scheduled注解标识,该注解有3中不同的属性配置,fixedDelay、fixedRate、cron;
以上就是如何在springboot中使用定时任务的详细内容,更多关于springboot中使用定时任务的资料请关注易盾网络其它相关文章!

