码迷,mamicode.com
首页 > 编程语言 > 详细

【SpringBoot】定时任务

时间:2020-06-05 00:57:37      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:link   date()   standard   res   end   let   def   public   poi   

 

定时任务注解

主程序

@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:\01-TestCode\SpringBoot_Study\springboot_segment_Lessons\spring-boot-lesson-1\target\classes started by SUN in D:\01-TestCode\SpringBoot_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】定时任务

标签:link   date()   standard   res   end   let   def   public   poi   

原文地址:https://www.cnblogs.com/clarino/p/13047249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!