step1 创建定时配置类 TaskSchedulerConfig
package com.bhz.mail.config.schedule;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@Configuration
@EnableScheduling //定时任务配置启用
public class TaskSchedulerConfig implements SchedulingConfigurer{
/**
*
*/
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
//taskRegistrar.setScheduler(null);
}
}
step2 创建定时任务的实现 ConsumerMailTask
package com.bhz.mail.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ConsumerMailTask {
/**
* initialDelay:项目启动的时候,延迟5秒开始执行
* fixedDelay:每隔2秒执行一次
*/
@Scheduled(initialDelay=5000,fixedDelay=2000)
public void intervalFast() {
try {
System.out.println("执行... ... ... ... ... ");
} catch (Exception e) {
}
}
}