1. spring boot的入口类Application.java中加入@EnableScheduling
package com.yunchuang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Demo8Application {
public static void main(String[] args) {
SpringApplication.run(Demo8Application.class, args);
}
}
2. 定时任务类
package com.yunchuang.schedule;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 定时器任务
*
* @author 尹冬飞
* @create 2017-04-12 8:24
*/
@Component
public class Jobs {
@Scheduled(fixedDelay = 1000)
public void job1(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
System.out.println(sdf.format(new Date()));
}
@Scheduled(cron = "0 10 3 ? * 1#3")
public void job2(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
System.out.println(sdf.format(new Date()));
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。