定时任务的实现
定时任务的实现
一、Spring Task
如果使用的是 Spring 或 Spring Boot 框架,可以直接使用 Spring Framework 自带的定时任务,它可以轻松实现设定了具体时间的定时任务,比如当我们需要每周五来执行某项任务时
以 Spring Boot 为例,实现定时任务只需两步:
1.开启定时任务;
2.添加定时任务。
具体步骤如下:
① 开启定时任务
开启定时任务只需要在 Spring Boot 的启动类上声明 @EnableScheduling 即可,实现代码如下:
点击查看代码
package cn.itcast.order;
import cn.itcast.feign.client.OssUploadFileClient;
import cn.itcast.feign.client.RabbitMQClient;
import cn.itcast.feign.client.ReceivingOrderClient;
import cn.itcast.feign.client.WeChatUserClient;
import cn.itcast.feign.config.DefaultFeignConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @Classname: OrderApplication
* @Description: TODO
* @Date: 2022/1/26 0026 9:54
* @Created: by GZY
*/
// 添加注解开启Feign的功能
@EnableFeignClients(clients = {OssUploadFileClient.class,ReceivingOrderClient.class,RabbitMQClient.class, WeChatUserClient.class},defaultConfiguration = DefaultFeignConfiguration.class)
@SpringBootApplication
@EnableScheduling//开启定时任务
public class AllOrderApplication {
public static void main(String[] args) {
SpringApplication.run(AllOrderApplication.class,args);
}
}
② 开启定时任务
定时任务的添加只需要使用 @Scheduled 注解标注即可,如果有多个定时任务可以创建多个 @Scheduled 注解标注的方法,示例代码如下:
点击查看代码
package cn.itcast.order.modules.timerTask;
import cn.itcast.feign.client.WeChatUserClient;
import cn.itcast.feign.vo.WeiXinGetRefundResultVO;
import cn.itcast.order.modules.order.dao.OrderDao;
import cn.itcast.order.modules.order.entity.OrderEntity;
import cn.itcast.order.modules.orderRefund.dao.OrderRefundDao;
import cn.itcast.order.modules.orderRefund.entity.OrderRefundEntity;
import cn.itcast.order.modules.timerTask.vo.XqOrderVO;
import cn.itcast.order.modules.timerTask.vo.YyOrderVO;
import cn.itcast.order.modules.xqOrder.dao.XqOrderDao;
import cn.itcast.order.modules.xqOrder.entity.XqOrderEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Classname MyTimerTask
* @Description 自定义定时任务
* @Date 2022/4/6 16:56
* @Created by 小郭
*/
@Component
@Slf4j
public class MyTimerTask {
@Autowired
private OrderDao orderDao;
@Autowired
private WeChatUserClient weChatUserClient;
@Autowired
private OrderRefundDao orderRefundDao;
@Autowired
private XqOrderDao xqOrderDao;
/**
* 每隔10秒轮询【订单状态为(正在退款中)的订单】,根据查询结果,修改订单状态
*/
// 添加定时任务
@Scheduled(cron = "0/10 * * * * ?") // cron 表达式,每10秒轮询一次
public void updateOrderStatus() throws Exception {
System.out.println("定时任务【根据退款结果修改订单状态】执行中......");
List<YyOrderVO> yyOrderVOList=orderDao.getYyOrderVOList();
List<XqOrderVO> xqOrderVOList =orderDao.getXqOrderVOList();
if (!yyOrderVOList.isEmpty()){
for (YyOrderVO yyOrderVO:yyOrderVOList) {
OrderEntity orderEntity = new OrderEntity();
OrderRefundEntity orderRefundEntity = new OrderRefundEntity();
WeiXinGetRefundResultVO refundResult = weChatUserClient.getRefundResult(yyOrderVO.getRefundId());
orderEntity.setId(Long.parseLong(yyOrderVO.getId()));
orderRefundEntity.setId(Long.parseLong(yyOrderVO.getOrderRefundId()));
if (refundResult.getReturnCode().equals("SUCCESS") && refundResult.getReturnMsg().equals("OK")){
orderEntity.setOrderStatus(6);//已退款
orderDao.updateById(orderEntity);
log.info("预约订单表id为"+yyOrderVO.getId()+"的订单状态成功改为【已退款】");
orderRefundEntity.setRefundStatus(1);
orderRefundDao.updateById(orderRefundEntity);
log.info("退款表id为"+yyOrderVO.getOrderRefundId()+"的订单状态成功改为【已退款】");
}else {
orderEntity.setOrderStatus(9);//退款失败
orderDao.updateById(orderEntity);
log.info("预约订单表id为"+yyOrderVO.getId()+"的订单状态改为【退款失败】");
orderRefundEntity.setRefundStatus(2);//退款失败
orderRefundDao.updateById(orderRefundEntity);
log.info("退款表id为"+yyOrderVO.getOrderRefundId()+"的订单状态改为【退款失败】");
}
}
}
if (!xqOrderVOList.isEmpty()){
for (XqOrderVO xqOrderVO:xqOrderVOList) {
XqOrderEntity xqOrderEntity = new XqOrderEntity();
OrderRefundEntity refundEntity = new OrderRefundEntity();
WeiXinGetRefundResultVO refundResult = weChatUserClient.getRefundResult(xqOrderVO.getRefundId());
xqOrderEntity.setId(Long.parseLong(xqOrderVO.getId()));
refundEntity.setId(Long.parseLong(xqOrderVO.getOrderRefundId()));
if (refundResult.getReturnCode().equals("SUCCESS") && refundResult.getReturnMsg().equals("OK")){
xqOrderEntity.setOrderStatus(6);//已退款
xqOrderDao.updateById(xqOrderEntity);
log.info("需求订单表id为"+xqOrderVO.getId()+"的订单状态成功改为【已退款】");
refundEntity.setRefundStatus(1);
orderRefundDao.updateById(refundEntity);
log.info("退款表id为"+xqOrderVO.getOrderRefundId()+"的订单状态成功改为【已退款】");
}else {
xqOrderEntity.setOrderStatus(9);//退款失败
xqOrderDao.updateById(xqOrderEntity);
log.info("需求订单表id为"+xqOrderVO.getId()+"的订单状态改为【退款失败】");
refundEntity.setRefundStatus(2);//退款失败
orderRefundDao.updateById(refundEntity);
log.info("退款表id为"+xqOrderVO.getOrderRefundId()+"的订单状态改为【退款失败】");
}
}
}
}
}
本文来自博客园,作者:青喺半掩眉砂,转载请注明原文链接:https://www.cnblogs.com/xiaoguo-java/p/16111504.html