在使用spring的时候遇到如下场景:由于业务需要,需要定时更新数据库,比如每隔两个小时更新一次某个字段的状态,无论是什么场景只要是需要定时去执行某种操作都可以使用spring的定时器任务来解决,以下将介绍springboot如何使用定时器任务:
创建定时任务:
@Component
public class NoticeTask {
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
/**
* 每两个小时更新一次过期停水通知状态
* @return
*/
@Scheduled(fixedRate = 2*60*60*