spring-boot ---Scheduled 定时任务

2019-08-01 10:58:34 浏览数 (1)

代码语言:javascript复制
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);
	}
}
代码语言:javascript复制
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....");
    }


}
代码语言:javascript复制
#可以直接在类上面标注该注解
@Component
@EnableScheduling


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

0 人点赞