From 07fafd528f1b6e5d89d628486dcc251115acd38d Mon Sep 17 00:00:00 2001 From: zhaoqifeng Date: Thu, 20 Oct 2022 16:29:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=89=E6=B1=82=E8=87=AA=E5=8A=A8=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../item/redis/ItemAutoProcessRedis.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/item/redis/ItemAutoProcessRedis.java diff --git a/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/item/redis/ItemAutoProcessRedis.java b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/item/redis/ItemAutoProcessRedis.java new file mode 100644 index 000000000..087ef98b6 --- /dev/null +++ b/esua-epdc/epdc-module/epdc-events/epdc-events-server/src/main/java/com/elink/esua/epdc/modules/item/redis/ItemAutoProcessRedis.java @@ -0,0 +1,99 @@ +/** + * Copyright 2018 人人开源 https://www.renren.io + *

+ * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + *

+ * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + *

+ * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.elink.esua.epdc.modules.item.redis; + +import com.elink.esua.epdc.commons.tools.redis.RedisUtils; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.cache.CacheProperties; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.ZSetOperations; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Date; +import java.util.Set; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * 诉求自动处理 + * + * @author zhaoqifeng + * @since v1.0.0 2022-09-03 + */ +@Component +@Slf4j +public class ItemAutoProcessRedis{ + /** + * redis key + */ + public static final String ITEM_TASK_KEY = "item:task:order"; + + @Resource + private StringRedisTemplate stringRedisTemplate; + + /** + * 生成诉求信息,用于延时任务达到时效后进行操作 + * @Param itemId + * @Param timeStamp 毫秒 + * @Author zhaoqifeng + * @Date 2022/10/20 14:12 + */ + public void produce(String itemId, Long totalMilliSeconds){ + stringRedisTemplate.opsForZSet().add( + ITEM_TASK_KEY, + itemId, + //24小时延时 + totalMilliSeconds + ); + + new Date(); + } + + /** + * 延时任务,也是异步任务,延时任务达到时效之后执行相关处理,并将延时任务从redis zset删除 + * @Param + * @Return + * @Author zhaoqifeng + * @Date 2022/10/20 14:26 + */ + public void consuming(){ + Set> itemSerialNos = stringRedisTemplate.opsForZSet().rangeByScoreWithScores( + ITEM_TASK_KEY, + //延时任务score最小值 + 0, + //延时任务score最大值(当前时间) + System.currentTimeMillis() + ); + if (!CollectionUtils.isEmpty(itemSerialNos)) { + for (ZSetOperations.TypedTuple itemSerialNo : itemSerialNos) { + log.info("诉求" + itemSerialNo.getValue() + "开始处理"); + //TODO 上报网格化平台,初验申请 + + log.info("诉求" + itemSerialNo.getValue() + "开始结束"); + //订单关闭之后,将订单延时任务从队列中删除 + stringRedisTemplate.opsForZSet().remove(ITEM_TASK_KEY, itemSerialNo.getValue()); + } + } + } +}