SpringBoot 写定时任务
Timed.java 定时任务类
需要用到 @Component 注解,(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Timed {
/**
*
* @desc 定时执行,每十分钟执行一次
* @return void
*/
@Scheduled(cron = "0 0/10 * * * ?") //多长时间执行一次 根据自己的需要去改
public void autoDividend() {
Date date=new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.format(date);
System.err.println("我已经开始执行了 "+simpleDateFormat.format(date));
//在此方法中写要执行的任务,例如定时查询订单,删除订单
}
}
关于Cron表达式(转载)
表达式网站生成:
需要在启动类中添加 @EnableScheduling 注解才可以开始定时任务 此注解是开启定时任务
配置完成