1 changed files with 99 additions and 0 deletions
@ -0,0 +1,99 @@ |
|||||
|
/** |
||||
|
* Copyright 2018 人人开源 https://www.renren.io
|
||||
|
* <p> |
||||
|
* 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. |
||||
|
* <p> |
||||
|
* 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. |
||||
|
* <p> |
||||
|
* You should have received a copy of the GNU General Public License |
||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
*/ |
||||
|
|
||||
|
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<ZSetOperations.TypedTuple<String>> itemSerialNos = stringRedisTemplate.opsForZSet().rangeByScoreWithScores( |
||||
|
ITEM_TASK_KEY, |
||||
|
//延时任务score最小值
|
||||
|
0, |
||||
|
//延时任务score最大值(当前时间)
|
||||
|
System.currentTimeMillis() |
||||
|
); |
||||
|
if (!CollectionUtils.isEmpty(itemSerialNos)) { |
||||
|
for (ZSetOperations.TypedTuple<String> itemSerialNo : itemSerialNos) { |
||||
|
log.info("诉求" + itemSerialNo.getValue() + "开始处理"); |
||||
|
//TODO 上报网格化平台,初验申请
|
||||
|
|
||||
|
log.info("诉求" + itemSerialNo.getValue() + "开始结束"); |
||||
|
//订单关闭之后,将订单延时任务从队列中删除
|
||||
|
stringRedisTemplate.opsForZSet().remove(ITEM_TASK_KEY, itemSerialNo.getValue()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue