• spingboot @EnableScheduling


    springboot让开发更简单!springmvc中启用定时任务还得需要在xml中进行配置启用并且要配置扫描器,但是springboot只需要一个注解就可以。

    @EnableScheduling

    无需多余的jar依赖,所以pom不贴了

    applaction.java

    1. package com.sbm;
    2.  
    3.  import org.springframework.boot.SpringApplication;
    4.  import org.springframework.boot.autoconfigure.SpringBootApplication;
    5.  import org.springframework.scheduling.annotation.EnableScheduling;
    6.   
    7.  @SpringBootApplication
    8.  @EnableScheduling
    9.  public class Application {
    10.   
    11.  public static void main(String[] args) {
    12.  SpringApplication.run(Application.class, args);
    13.  }
    14.  }



    定时任务类AppCoreTask.java

    1.  package com.sbm.scheduling;
    2.   
    3.  import org.springframework.scheduling.annotation.Scheduled;
    4.  import org.springframework.stereotype.Component;
    5.  import java.util.Date;
    6.   
    7.  @Component
    8.  public class AppCoreTask {
    9.   
    10.  @Scheduled(cron = "0 53 15 * * ? ")
    11.  public void tesk() {
    12.  System.out.print("开启定时任务" + new Date());
    13.  }
    14.  }



    要求在15:53分打印一句话

    关于cron表达式的用法,可以在我的这篇文章里查看:Spring Task 中cron表达式整理记录

    运行sb程序

    
    
    1.  
      016-12-30 15:52:24.653 INFO 4204 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2.  
      2016-12-30 15:52:24.654 INFO 4204 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    3.  
      2016-12-30 15:52:24.722 INFO 4204 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    4.  
      2016-12-30 15:52:25.183 INFO 4204 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
    5.  
      2016-12-30 15:52:25.212 INFO 4204 --- [ main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
    6.  
      2016-12-30 15:52:25.315 INFO 4204 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    7.  
      2016-12-30 15:52:25.326 INFO 4204 --- [ main] com.sbm.Application : Started Application in 7.124 seconds (JVM running for 8.234)
    8.  
      开启定时任务Fri Dec 30 15:53:00 CST 2016
    9.  

  • 相关阅读:
    nohup npm start &启动之后关闭终端程序没有后台运行
    C++标准库之string返回值研究
    Apache Thrift的C++多线程编程定式
    实战C++对象模型之成员函数调用
    std::string的拷贝赋值研究
    REdis AOF文件结构分析
    使用Linux自带日志滚动工具logrotate滚动redis日志示例
    源码分析MySQL mysql_real_query函数
    源码解读Linux的limits.conf文件
    C++中的return和exit区别
  • 原文地址:https://www.cnblogs.com/jtlgb/p/9525372.html
Copyright © 2020-2023  润新知