Spring: Scheduling task (Maven Version)

Source: Internet
Author: User
Tags dateformat

Spring: Scheduling task (Maven Version)

[This tutorial is translated from the Spring official website and deleted as appropriate.]

Task Scheduling is a common function, such as regularly updating system logs.

You will set up

Use Spring's @ Scheduled annotation to print the current time every 5 seconds on the console.

Tools

A text editor, JDK 3.0 and later, Maven 1.11 + or Gradle +. (Maven will be used in this article)


Pom. xml list:

 
     
  
   4.0.0
      
  
   org.springframework
      gs-scheduling-tasks    
  
   0.1.0
      
          
   
    org.springframework.boot
           spring-boot-starter-parent        
   
    1.1.5.RELEASE
       
      
          
               
    
     org.springframework.boot
                spring-boot-starter        
       
          
          
   
    hello.Application
       
          
          
               
    
                      maven-compiler-plugin                 
     
      2.3.2
                  
                
                    
     
      org.springframework.boot
                     spring-boot-maven-plugin            
            
       
      
          
               
    
     spring-releases
                
    
     Spring Releases
                
    
     http://repo.spring.io/libs-release
            
       
      
          
               
    
     spring-releases
                
    
     Spring Releases
                
    
     http://repo.spring.io/libs-release
            
       
  
 

Create a project
First, create a directory structure that complies with Maven specifications, src/main/java/hello

[Plain]View plaincopy
  1. ── Src
  2. ── Main
  3. ── Java
  4. ── Create a scheduling task in the hello directory:

    package hello;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;@EnableSchedulingpublic class ScheduledTasks {    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");    @Scheduled(fixedRate = 5000)    public void reportCurrentTime() {        System.out.println("The time is now " + dateFormat.format(new Date()));    }}

    The core of this Code Execution scheduling task is to use @ EnableScheduling and @ Schduled annotations.

    @ EnabledScheduling: ensure that the backend task executor is created. Otherwise, nothing can be scheduled. @ Scheduled configure a specific method. In this example, fixedRate is used to indicate the time interval at which the method starts to be called. fixedDelay is also available to indicate the task completion interval. You can also use the @ Schduled (cron = "...") expression to perform more complex scheduling.

    Note:

    Example patterns:

      "0 0 ****" = the top of every hour of every day. start every hour every day "*/10 *****" = every ten seconds. every 10 seconds "0 0 8-10 ***" = 8, 9 and 10 o 'clock of every day. to every day "0 0/30 8-10 ***" =, and 10 o'clock every day. half past eight, half past nine, "0 0 9-17 ** MON-FRI" = on the hour nine-to-five weekdays from Monday to Friday "0 0 0 25 12? "= Every Christmas Day at midnight December 25
      Although the Application class schedules tasks to be embedded into a Web Application, it is simpler to generate an independent Application.
      package hello;import org.springframework.boot.SpringApplication;public class Application {    public static void main(String[] args) throws Exception {        SpringApplication.run(ScheduledTasks.class);    }}

      The method for packaging and execution is similar to that in the previous article,
      mvn clean package

      Then,
      java -jar target/gs-scheduling-tasks-0.1.0.jar

      The result is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.