1. 程式人生 > >spring-boot ---Scheduled 定時任務

spring-boot ---Scheduled 定時任務

package com.shi.snyc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 *
 *  @EnableScheduling 開啟定時任務註解
 */

@EnableScheduling
@SpringBootApplication
public class SnycApplication {

	public static void main(String[] args) {
		SpringApplication.run(SnycApplication.class, args);
	}
}
package com.shi.snyc.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ScheduledService {

    /**
     * @Scheduled 標註該方法是定時執行的
     * 
     * econd(秒), minute(分), hour(時), day of month (日),month(月) ,day of week (周幾)
     * 0 * * * * MON-FRI
     */

    @Scheduled(cron = "0/4 * * * * MON-SAT")  //每隔4秒執行一次
    public  void hello(){
        System.out.println("hello....");
    }


}

 

#可以直接在類上面標註該註解
@Component
@EnableScheduling


#在方法上面標註下面的註解
@Scheduled(cron="0 59 * * * *")
@Transactional