SpringBoot定时任务
Posted 飞翔在天
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot定时任务相关的知识,希望对你有一定的参考价值。
定时任务注解
主程序
@EnableScheduling @SpringBootApplication public class SpringBootLesson1Application { public static void main(String[] args) { SpringApplication.run(SpringBootLesson1Application.class, args); } }
Tasks类
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.util.Date; @Service public class Tasks { @Scheduled(fixedRate = 3000)//程序开始后每3秒钟执行一次 public void fixedRateJobTest() { System.out.println("每3秒钟执行一次...当前时间:" + new Date()); } }
POM依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
测试结果:
. ____ _ __ _ _ /\ / ___‘_ __ _ _(_)_ __ __ _ ( ( )\___ | ‘_ | ‘_| | ‘_ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) ‘ |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.4.RELEASE) 2020-06-04 23:29:52.467 INFO 8412 --- [ main] c.s.s.SpringBootLesson1Application : Starting SpringBootLesson1Application on SUN-PC with PID 8412 (D: 1-TestCodeSpringBoot_Studyspringboot_segment_Lessonsspring-boot-lesson-1 argetclasses started by SUN in D: 1-TestCodeSpringBoot_Study) 2020-06-04 23:29:52.471 INFO 8412 --- [ main] c.s.s.SpringBootLesson1Application : No active profile set, falling back to default profiles: default 2020-06-04 23:29:55.564 INFO 8412 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-06-04 23:29:55.590 INFO 8412 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-06-04 23:29:55.590 INFO 8412 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30] 2020-06-04 23:29:55.825 INFO 8412 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-06-04 23:29:55.826 INFO 8412 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3260 ms 2020-06-04 23:29:56.426 INFO 8412 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ‘applicationTaskExecutor‘ 2020-06-04 23:29:57.114 INFO 8412 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService ‘taskScheduler‘ 2020-06-04 23:29:57.424 INFO 8412 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘‘ 每3秒钟执行一次...当前时间:Thu Jun 04 23:29:57 CST 2020 2020-06-04 23:29:57.630 INFO 8412 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http) 2020-06-04 23:29:57.631 INFO 8412 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-06-04 23:29:57.632 INFO 8412 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30] 2020-06-04 23:29:57.653 INFO 8412 --- [ main] o.a.c.c.C.[Tomcat-1].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-06-04 23:29:57.653 INFO 8412 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 223 ms 2020-06-04 23:29:57.680 INFO 8412 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path ‘/actuator‘ 2020-06-04 23:29:57.952 INFO 8412 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ‘‘ 2020-06-04 23:29:57.958 INFO 8412 --- [ main] c.s.s.SpringBootLesson1Application : Started SpringBootLesson1Application in 7.227 seconds (JVM running for 8.315) 每3秒钟执行一次...当前时间:Thu Jun 04 23:30:00 CST 2020 每3秒钟执行一次...当前时间:Thu Jun 04 23:30:03 CST 2020 每3秒钟执行一次...当前时间:Thu Jun 04 23:30:06 CST 2020 每3秒钟执行一次...当前时间:Thu Jun 04 23:30:09 CST 2020
以上是关于SpringBoot定时任务的主要内容,如果未能解决你的问题,请参考以下文章
springboot项目使用SchedulingConfigurer实现多个定时任务
#yyds干货盘点# springboot整合Elastic Job实现分片配置定时任务