import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author JMWANG
* 定时器 定时加载数据库信息 (每分钟一次)
*/
@Component
@Configuration
@EnableScheduling
public class CompleteScheduleConfig implements Serializable {
private static Logger LOGGER = Logger.getLogger(CompleteScheduleConfig.class);
//@Scheduled(initialDelay = 0, fixedRate = 1000 * 10) 开始多少毫秒时候执行第一次 每隔多少毫秒执行一次
@Scheduled(cron = "0 */1 * * * ?") //cron 表达式
public void executeSendAskForLeavePerson(){
//格式化时间
SimpleDateFormat sdf = new SimpleDateFormat();
//a为am/pm的标记
sdf.applyPattern("yyyy-MM-dd HH:mm:ssa");
//获取当前时间
Date date = new Date();
LOGGER.info("定时器正常加载,当前时间为"+sdf.format(date));
}
}
Spring中的定时器(定时执行任务)
于 2022-04-02 17:54:40 首次发布