注解方式使用定时器
@Component
public class TestQuartz {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 6000)
private void process(){
TestService test = ApplicationContextRegister.getBean(TestService.class);
System.out.println("现在时间:" + dateFormat.format(new Date()));
}
}
@Scheduled两种定时的设置:
一种是cornexpression。
一种是Rate/Delay表达式(毫秒值):
@Scheduled(fixedRate = 6000):上一次开始执行时间点后每隔6秒执行一次。
@Scheduled(fixedDelay = 6000):上一次执行完毕时间点之后6秒再执行。
@Scheduled(initialDelay=1000, fixedRate=6000):第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次
cornexpression表达式说明:
完整字段:[秒] [分] [小时] [日] [月] [周] [年]
字段 允许值 允许特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日 1-31 , - * ? / L W C
月 1-12或JAN-DEC , - * /
周 1-7或SUN-SAT , - * ? / L C #
年 留空或1970-2099 , - * /