在项目中,我们时常会遇到需要定时去自动执行一串代码的需求
具体实现Spring 3.0已经包含了它的坐标 spring-context
第一步, 启动类添加@EnableScheduling开启定时执行
第二步,创建定时任务类
package com.sky.task;import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Component
@Slf4j
public class OrderTask {
@Resource
private OrderMapper orderMapper;
@Scheduled(cron = "0 * * * * ? ")
public void OrderPayTimeOut(){
LocalDateTime localDateTime=LocalDateTime.now().plusMinutes(-15);
List<Orders>orders= orderMapper.selectTimeOut(Orders.PENDING_PAYMENT,localDateTime);
orders.forEach(orders1 -> {
orders1.setCancelTime(LocalDateTime.now());
orders1.setStatus(Orders.CANCELLED);
orders1.setCancelReason("订单超时");
log.info("订单超时{}",orders1);
orderMapper.update(orders1);
});
}
@Scheduled(cron = "0 0 3 * * ? ")
public void OrderSureTimeOut(){
log.info("zhengzaizhixing2");
List<Orders>orders= orderMapper.selectTimeOut(Orders.DELIVERY_IN_PROGRESS,LocalDateTime.now());
orders.forEach(orders1 -> {
orders1.setCancelTime(LocalDateTime.now());
orders1.setStatus(Orders.COMPLETED);
orders1.setCancelReason("订单确认超时,自动确认");
log.info("订单确认超时{}",orders1);
orderMapper.update(orders1);
});
}
}
标签:Task,orderMapper,orders1,Spring,LocalDateTime,import,定时,now,Orders From: https://www.cnblogs.com/fubai/p/18290278