1、保存相关的id:
/**
* 将 businessId 加入订单状态查询队列(Redis Sorted Set,score 为时间戳)
*/
private void addToOrderStatusQueryQueue(String businessId) {
if (StringUtils.isBlank(businessId)) {
return;
}
ZSetOperations<String, String> zSetOps = stringRedisTemplate.opsForZSet();
zSetOps.add(“key”, businessId, System.currentTimeMillis());
log.info("已加入订单状态查询队列,businessId={}", businessId);
}
2、使用定时任务,在application.java上开启@EnableScheduling
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Set;
/**
* 订单状态查询定时任务
* 每15分钟执行一次,从 Redis 取出超过12分钟的 businessId 并调用订单查询接口更新状态
*
* @author system
*/
@Slf4j
@Component
public class OrderStatusQueryTask {
@Resource
private StringRedisTemplate stringRedisTemplate;
/**
* 每15分钟执行一次
*/
@Scheduled(cron = "0 */15 * * * ?")
public void execute() {
log.info("订单状态查询定时任务开始执行");
long expireThreshold = System.currentTimeMillis() - (long) 12 * 60 * 1000;
ZSetOperations<String, String> zSetOps = stringRedisTemplate.opsForZSet();
Set<String> expiredBusinessIds = zSetOps.rangeByScore(“key”, 0, expireThreshold);
if (expiredBusinessIds == null || expiredBusinessIds.isEmpty()) {
log.info("订单状态查询定时任务:无超时待处理任务");
return;
}
log.info("订单状态查询定时任务:待处理 businessId 数量={}", expiredBusinessIds.size());
for (String businessId : expiredBusinessIds) {
processBusinessId(zSetOps, businessId);
}
log.info("定时任务执行完成");
}
/**
* 处理单个 businessId:调用查询接口并移除队列
*/
private void processBusinessId(ZSetOperations<String, String> zSetOps, String businessId) {
try {
// TODO 执行任务逻辑
zSetOps.remove("key", businessId);
log.info("订单状态查询任务执行成功,businessId={}", businessId);
} catch (Exception e) {
log.error("订单状态查询任务执行异常,businessId={}", businessId, e);
}
}
}