程序员|灵感来了挡也挡不住,基于Redis解决业务场景中延迟队列

在一次产品需求设计中,有这样一种场景,对于一个工单,专员可以选择将工单挂起(一次挂起2小时;二次挂起12小时),或者转存(表单输入转存的天数),然后提交工单 到既定时间后,该工单需要重新被领单,然后再次审核。对于工单,有优先级概念,意味着优先级越大,该工单则需要优先被派单,对于这种场景,我们可以基于Redis来解决场景问题。
一、业务概述 我们假定设置两个队列,一个队列维护正式工单,另一个队列维护挂起工单。对于挂起操作,我们通过Redis设置key有效时间,当key失效时,客户端监听失效事件,获取工单,实现 挂起工单队列的移除,正式队列的入队即可。
业务流程图:
程序员|灵感来了挡也挡不住,基于Redis解决业务场景中延迟队列
文章图片


二、代码实现 程序员|灵感来了挡也挡不住,基于Redis解决业务场景中延迟队列
文章图片


整个业务实现,可以分为三个大模块。
  • 队列模块,通过WorkOrderQueueTransfer带外提供内部的调用。
  • 工单模块、通过OperateStrategyManager管理器实现工单相关场景的操作。
  • 事件监听、通过继承KeyExpirationEventMessageListener,实现对Key过期失效的监听(特别说明:Redis服务器redis.conf需要开启Key过期失效通知)。
2.1、工单队列实现
我们可以基于Redis ZSet数据存储,ZSet是个有序集合,可以实现基于score排序。
2.1.1、定义上下文类(WorkOrderContext)
/** * @description: 工单上下文对象 * @Date : 2020/7/13 下午4:28 * @Author : 石冬冬-Seig Heil */ @Data @NoArgsConstructor @AllArgsConstructor @Builder public class WorkOrderContext { /** * 是否测试人员专用 */ private boolean isTest; /** * 工单号 */ private WorkOrder worOrder; /** * 队列类型 */ private QueueType queueType; /** * 创建-正式队列(立即需要被派单 * @return */ public static WorkOrderContext buildImmediate() { return WorkOrderContext.builder().queueType(QueueType.immediate).build(); }/** * 创建-挂起队列(挂起n小时执行) * @return */ public static WorkOrderContext buildSuspended() { return WorkOrderContext.builder().queueType(QueueType.suspended).build(); }/** * 转存队列(转存n天后执行) * @return */ public static WorkOrderContext buildStored() { return WorkOrderContext.builder().queueType(QueueType.stored).build(); }/** * 创建-正式队列(立即需要被派单) * * @param workCode * @param priority * @return */ public static WorkOrderContext buildImmediate(String workCode, double priority) { WorkOrder workOrder = WorkOrder.builder().workCode(workCode).priority(priority).delayedTime(0).build(); return WorkOrderContext.builder().worOrder(workOrder).queueType(QueueType.immediate).build(); }/** * 创建-挂起队列(挂起n小时执行) * * @param workCode * @param priority * @param delayedTime * @return */ public static WorkOrderContext buildSuspended(String workCode, double priority, long delayedTime) { WorkOrder workOrder = WorkOrder.builder().workCode(workCode).priority(priority).delayedTime(delayedTime).build(); return WorkOrderContext.builder().worOrder(workOrder).queueType(QueueType.suspended).build(); }/** * 转存队列(转存n天后执行) * * @param workCode * @param priority * @param delayedTime * @return */ public static WorkOrderContext buildStored(String workCode, double priority, long delayedTime) { WorkOrder workOrder = WorkOrder.builder().workCode(workCode).priority(priority).delayedTime(delayedTime).build(); return WorkOrderContext.builder().worOrder(workOrder).queueType(QueueType.stored).build(); }/** * 队列类型 */ public enum QueueType { /** * 正式队列(立即需要被派单) */ immediate, /** * 挂起队列(挂起n小时执行) */ suspended, /** * 转存队列(转存n天后执行) */ stored }@Data @NoArgsConstructor @AllArgsConstructor @Builder public static class WorkOrder { /** * 工单号 */ private String workCode; /** * 优先级 */ private double priority; /** * 延迟时间 */ private long delayedTime; } }

2.1.2、定义抽象缓存类(AbstractCacheManager)
该抽象类定义了一个方法,抽象定义了一个具有重试机制的方法。其中注入的BeanRedisService是咱们子类基于Redis API的封装。
/** * @description: 抽象缓存管理器 * @Date : 2020/7/18 下午9:41 * @Author : 石冬冬-Seig Heil */ @Slf4j public abstract class AbstractCacheManager {final int MAX_RETRIES = 3; @Autowired RedisService redisService; /** * 重试操作 * @param retries 重试次数 * @param context 上下文 * @param call 重试动作 */ public void retry(int retries, T context, Function call){ boolean done = false; int retry = 1; do { try { done = call.apply(retry); log.info("[retry] context={},retry={},done={}", JSONObject.toJSON(context),retry,done); retry ++; TimeUnit.MILLISECONDS.sleep(100); } catch (Exception e) { log.error("[retry] 异常 ctx={}", JSONObject.toJSON(context),e); retry ++; } }while (retry <= retries && !done); } }

2.1.3、定义基于Redis Cache的Manager类(WorkOrderCacheManager)
该类的主要作用,基于Redis String对象存储,实现具有Key失效机制的存储。
  • 内部静态类CacheValue,作为Redis String对象存储的Value值。
  • 内部枚举类CacheType,维护了缓存Key的业务前缀。
  • 特别说明的是,我们构成Redis String存储Key的命名规则,例如:carCarthage:stored_cache_单号。
/** * @description: 工单缓存管理器 * @Date : 2020/7/14 下午4:28 * @Author : 石冬冬-Seig Heil */ @Component @Slf4j public class WorkOrderCacheManager extends AbstractCacheManager{/** * 设置缓存并设置缓存失效日期 * @param cache */ public void setCacheInExpire(CacheValue cache){ retry(MAX_RETRIES,cache,idx -> { String redisKey = redisService.getKey(getRedisKeySuffix(cache.getType(),cache.getWorkCode())); redisService.set(redisKey, JSONObject.toJSONString(cache),cache.getExpireSeconds()); log.info("[setCacheInExpire],redisKey={},CacheValue=https://www.it610.com/article/{}",redisKey,JSONObject.toJSONString(cache)); return Boolean.TRUE; }); }/** * 查询某个工单号的缓存值 * @param cacheType 缓存类型 {@link CacheType} * @param workCode 工单号 * @return */ public CacheValue get(CacheType cacheType,String workCode){ String redisKey = redisService.getKey(getRedisKeySuffix(cacheType,workCode)); String value =https://www.it610.com/article/redisService.get(redisKey,String.class); return JSONObject.parseObject(value,CacheValue.class); }/** * 从上下文队列类型获取队列redis key * @param cacheType 缓存类型 {@link CacheType} * @param workCode 工单号 * @return */ String getRedisKeySuffix(CacheType cacheType,String workCode){ switch (cacheType){ case stored_cache: return CacheType.stored_cache.getKey() + workCode; case suspended_cache: return CacheType.suspended_cache.getKey() + workCode; default: break; } return null; }/** * 缓存值 */ @Data @NoArgsConstructor @AllArgsConstructor @Builder public static class CacheValue{ /** * 缓存类型 */ private CacheType type; /** * 工单号 */ private String workCode; /** * 优先级 */ private double priority; /** * 延迟截止时间(单位:时间戳) */ private long delayedTime; /** * 缓存失效时间(单位:秒) */ private long expireSeconds; /** * 创建-挂起队列(挂起n小时执行) * * @param workCode * @param priority * @param delayedTime * @param expireSeconds * @return */ public static CacheValue buildSuspended(String workCode, double priority, long delayedTime,long expireSeconds) { return CacheValue.builder() .type(CacheType.suspended_cache) .workCode(workCode) .priority(priority) .delayedTime(delayedTime) .expireSeconds(expireSeconds) .build(); }/** * 转存队列(转存n天后执行) * * @param workCode * @param priority * @param delayedTime * @param expireSeconds * @return */ public static CacheValue buildStored(String workCode, double priority, long delayedTime,long expireSeconds) { return CacheValue.builder() .type(CacheType.stored_cache) .workCode(workCode) .priority(priority) .delayedTime(delayedTime) .expireSeconds(expireSeconds) .build(); } }/** * 实现 挂起|转存 缓存key */ @Getter public enum CacheType{ stored_cache("stored_cache_"), suspended_cache("suspended_cache_"), ; CacheType(String key) { this.key = key; } private String key; } }

2.1.4、工单队列管理器(WorkOrderQueueManager)
该类基于Redis ZSet 对象结构的有序集合,可以实现按照优先级出队。
相关方法介绍:
  • String getRedisKey(WorkOrderContext context):从 WorkOrderContext中获取队列类型,返回集合要存储的Key。
  • Long queueSize(WorkOrderContext context):返回队列大小
  • Boolean leftPush(WorkOrderContext context):执行处理(入队操作)
  • Boolean leftPushIfAbsent(WorkOrderContext context):执行处理(入队操作),如果入队元素缺席则入队,返回true;否则返回false。
  • Long remove(WorkOrderContext context):从队列移除某个元素
  • WorkOrderContext.WorkOrder pop(WorkOrderContext context):从集合中获取评分最小的成员出队
  • Set rank(WorkOrderContext context):按照升序查看队列中所有成员
  • Long removeRange(String key, long start, long end):按照范围移除队列元素
  • Long removeValues(String key, List values):移除指定元素
  • long getDelayedTime(WorkOrderContext.QueueType queueType,String workCode):获取对应工单的延迟时间(适用于 挂起 和 转存)
  • /** * @description: 工单队列管理器 * @Date : 2020/7/14 下午4:28 * @Author : 石冬冬-Seig Heil */ @Component @Slf4j public class WorkOrderQueueManager extends AbstractCacheManager{final String LOCK_KEY = "ZSET_ATOMIC_LOCK"; @Autowired ZSetOperations zSetOperations; @Autowired WorkOrderCacheManager workOrderCacheManager; /** * 从上下文队列类型获取队列redis key * @param context * @return */ String getRedisKey(WorkOrderContext context){ String keySuffix = null; switch (context.getQueueType()){ case immediate: keySuffix = CarthageConst.WorkOrderKey.IMMEDIATE_QUEUE_DEFAULT; break; case stored: keySuffix = CarthageConst.WorkOrderKey.STORED_QUEUE_DEFAULT; break; case suspended: keySuffix = CarthageConst.WorkOrderKey.SUSPENDED_QUEUE_DEFAULT; break; default: break; } if(null != keySuffix){ if(context.isTest()){ keySuffix += CarthageConst.TEST_SUFFIX; } return redisService.getKey(keySuffix); } return null; }/** * 返回队列大小 * @param context * @return */ public Long queueSize(WorkOrderContext context) { return zSetOperations.size(getRedisKey(context)); }/** * 执行处理(入队操作) * @param context * @return */ public Boolean leftPush(WorkOrderContext context) { String redisKey = getRedisKey(context); String workCode = context.getWorOrder().getWorkCode(); double priority = context.getWorOrder().getPriority(); Boolean action = zSetOperations.add(redisKey,workCode,priority); if(Objects.equals(Boolean.FALSE,action)){ Long value = https://www.it610.com/article/zSetOperations.rank(redisKey,workCode); log.info("[Queue.leftPush],hasLeftPushed,action={},value=https://www.it610.com/article/{}, context={}", action,value,JSONObject.toJSON(context)); if(Objects.nonNull(value)){ return Boolean.TRUE; } } log.info("[Queue.leftPush] context={}", JSONObject.toJSON(context)); retry(MAX_RETRIES,context,idx -> action); return Optional.ofNullable(action).orElse(Boolean.FALSE); }/** * 执行处理(入队操作) * 如果入队元素缺席则入队,返回true;否则返回false。 * @param context * @return */ public Boolean leftPushIfAbsent(WorkOrderContext context) { String redisKey = getRedisKey(context); String workCode = context.getWorOrder().getWorkCode(); double priority = context.getWorOrder().getPriority(); Boolean action = zSetOperations.add(redisKey,workCode,priority); log.info("[WorkOrderQueue.leftPushIfAbsent,action={},context={}",action, JSONObject.toJSON(context)); return Optional.ofNullable(action).orElse(Boolean.FALSE); }/** * 从队列移除某个元素 * @param context * @return */ public Long remove(WorkOrderContext context){ String redisKey = getRedisKey(context); String workCode = context.getWorOrder().getWorkCode(); log.info("[WorkOrderQueue.remove] context={}", JSONObject.toJSON(context)); Long rem = zSetOperations.remove(redisKey,workCode); Long action = Optional.ofNullable(rem).orElse(0L); retry(MAX_RETRIES,context,idx -> action.longValue() > 0); return action; }/** * 从集合中获取评分最小的成员出队 * @param context * @return */ public WorkOrderContext.WorkOrder pop(WorkOrderContext context) { WorkOrderContext.WorkOrder workOrder = null; try { String redisKey = getRedisKey(context); //通过分布式锁,实现 zset 的 zpopmin 命令操作 boolean locked = redisService.lock(LOCK_KEY,5000); if(locked){ //1、取出第一个最小评分元素 Set> set = redisService.zSetOperations().rangeWithScores(redisKey,0,0); if(set.isEmpty()){ return null; } //2、移除该最小评分元素 Long value = https://www.it610.com/article/redisService.zSetOperations().removeRange(redisKey,0,0); retry(MAX_RETRIES,context,idx -> value.longValue() > 0); //3、返回出队成员 workOrder = WorkOrderContext.WorkOrder.builder().build(); for(ZSetOperations.TypedTuple each : set){ workOrder.setWorkCode(each.getValue().toString()); workOrder.setPriority(each.getScore()); workOrder.setDelayedTime(0); break; } } } catch (Exception e) { log.error("[WorkOrderQueue.pop] exception ctx={}", JSONObject.toJSON(context)); }finally { redisService.unlock(LOCK_KEY); } return workOrder; }/** * 按照升序查看队列中所有成员 * @param context * @return */ public Set rank(WorkOrderContext context) {Set> set = redisService.zSetOperations().rangeWithScores(getRedisKey(context),0,-1); Set members = Sets.newLinkedHashSetWithExpectedSize(set.size()); set.forEach(each -> { WorkOrderContext.WorkOrder every = WorkOrderContext.WorkOrder.builder() .workCode(each.getValue().toString()) .priority(each.getScore()) .delayedTime(getDelayedTime(context.getQueueType(),each.getValue().toString())) .build(); members.add(every); }); return members; }/** * 按照范围移除队列元素 * @param key * @param start * @param end * @return */ public Long removeRange(String key, long start, long end){ String redisKey = redisService.getKey(key); Long count = zSetOperations.removeRange(redisKey,start,end); log.info("[WorkOrderQueue.removeRange] redisKey={},start={},end={},count={}", redisKey,start,end,count); return count; }/** * 移除指定元素 * @param key * @param values * @return */ public Long removeValues(String key, List values){ String redisKey = redisService.getKey(key); LongAdder longAdder = new LongAdder(); values.forEach(each -> { Long count = zSetOperations.remove(redisKey,each); longAdder.add(count); }); Long count =longAdder.longValue(); log.info("[WorkOrderQueue.removeValues] redisKey={},values={},count={}", redisKey,JSONObject.toJSONString(values),count); return count; }/** * 获取对应工单的延迟时间(适用于 挂起 和 转存) * @param queueType * @param workCode * @return */ long getDelayedTime(WorkOrderContext.QueueType queueType,String workCode){ long delayedTime = 0; WorkOrderCacheManager.CacheType cacheType = null; switch (queueType){ case suspended: cacheType = WorkOrderCacheManager.CacheType.suspended_cache; break; case stored: cacheType = WorkOrderCacheManager.CacheType.stored_cache; break; default: break; } if(null != cacheType){ WorkOrderCacheManager.CacheValue cacheValue = https://www.it610.com/article/workOrderCacheManager.get(cacheType,workCode); if(null != cacheValue){ delayedTime = cacheValue.getDelayedTime(); } } return delayedTime; } }
    2.2、工单队列转移管理器
    2.2.1、工单队列转移管理器(WorkOrderQueueTransfer)
    该类实现延迟队列出队、正式队列入队的原子操作(通过Redis分布式锁实现)。
    /** * @description: 工单队列转移管理器 * @Date : 2020/7/23 下午6:15 * @Author : 石冬冬-Seig Heil */ @Component @Slf4j public class WorkOrderQueueTransfer extends AbstractCacheManager{final static String ATOMIC_KEY = "delayed_queue_key_expire_lock_{0}"; final static long ATOMIC_KEY_EXPIRE = 5000; @Autowired RedisService redisService; @Autowired WorkOrderQueueManager workOrderQueueManager; @Autowired WorkOrderCacheManager workOrderCacheManager; /** * 从[挂起|暂存]队列转移到正式队列中 * @param cacheType 挂起|暂存 * @param delayedContext * @return */ public Boolean transferImmediateQueue(WorkOrderCacheManager.CacheType cacheType,WorkOrderContext delayedContext){ boolean tryLock = false; Boolean done = Boolean.FALSE; String lockKey = null; try { WorkOrderContext.WorkOrder workOrder = delayedContext.getWorOrder(); lockKey = redisService.getKey(MessageFormat.format(ATOMIC_KEY,workOrder.getWorkCode())); tryLock = redisService.lock(lockKey,ATOMIC_KEY_EXPIRE); if(tryLock){ //1、构建正式队列 WorkOrderContext immediateContext = WorkOrderContext.buildImmediate(workOrder.getWorkCode(),workOrder.getPriority()); done = workOrderQueueManager.leftPushIfAbsent(immediateContext); //2、从当前延迟队列移除该元素 Long count = workOrderQueueManager.remove(delayedContext); log.info("[挂起|转存队remove],count={},delayedContext={}", count,JSONObject.toJSONString(delayedContext)); } } catch (Exception e) { log.error("[transferImmediateQueue]异常,delayedContext={},cacheType={}", JSONObject.toJSONString(delayedContext),cacheType); }finally { if(Objects.nonNull(lockKey) && tryLock){ redisService.unlock(lockKey); } } return Optional.ofNullable(done).orElse(Boolean.FALSE); } }

    2.3、Redis过期key监听
    2.3.1、Redis过期回调监听(RedisKeyExpirationListener)
    /** * @description: Redis过期回调监听 * @Date : 2020/7/18 上午10:43 * @Author : 石冬冬-Seig Heil */ @Component @Slf4j public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {final static String STORED_CACHE_KEY_PREFIX = WorkOrderCacheManager.CacheType.stored_cache.getKey(); final static String SUSPENDED_CACHE_KEY_PREFIX = WorkOrderCacheManager.CacheType.suspended_cache.getKey(); @Autowired TraceLogService traceLogService; @Autowired RedisService redisService; @Autowired WorkOrderService workOrderService; @Autowired DelayedScheduledOperateBridge delayedScheduledOperateBridge; public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); }@Override public void onMessage(Message message, byte[] pattern) { Date startTime = TimeTools.createNowTime(); String expiredKey = message.toString(); String bizPrefix = redisService.getKeyPrefix().getName(); if(!expiredKey.startsWith(bizPrefix)){ return; } String caseOfStored = redisService.getKey(STORED_CACHE_KEY_PREFIX); String caseOfSuspended = redisService.getKey(SUSPENDED_CACHE_KEY_PREFIX); WorkOrderCacheManager.CacheType cacheType; WorkOrderContext.QueueType queueType; if(expiredKey.startsWith(caseOfStored)){ queueType = WorkOrderContext.QueueType.stored; cacheType = WorkOrderCacheManager.CacheType.stored_cache; }else if(expiredKey.startsWith(caseOfSuspended)){ queueType = WorkOrderContext.QueueType.suspended; cacheType = WorkOrderCacheManager.CacheType.suspended_cache; }else{ return; } String workCode = getWorkCode(expiredKey); log.info("监听到 redis key=[{}] 已过期",expiredKey); if(Objects.nonNull(workCode)){ log.info("监听到 redis key=[{}],挂起|转存工单开始处理,workCode={}",expiredKey,workCode); WorkOrder workOrder = workOrderService.queryOne(workCode); if(Objects.isNull(workOrder)){ log.info("监听到 redis key=[{}],挂起|转存工单开始处理,未找到工单,workCode={}",expiredKey,workCode); return; } WorkOrderContext delayedContext = WorkOrderContext.builder() .worOrder(WorkOrderContext.WorkOrder.builder().delayedTime(5).priority(workOrder.getCasePriority()).workCode(workOrder.getWorkCode()).build()) .queueType(queueType). build(); Boolean done = delayedScheduledOperateBridge.transferImmediateQueue(cacheType,delayedContext); saveTraceLog(delayedContext,done,traceLog -> { JSONObject requestBody = new JSONObject(); requestBody.put("expiredKey",expiredKey); requestBody.put("workCode",workCode); traceLog.setRequestBody(requestBody.toJSONString()); traceLog.setRequestTime(startTime); }); } }/** * traceLog入库 * @param context * @param done * @param consumer */ void saveTraceLog(WorkOrderContext context, Boolean done, Consumer consumer){ try { String hostAddress = InetAddress.getLocalHost().getHostAddress(); JSONObject responseBody = new JSONObject(); responseBody.put("workOrderContext",context); responseBody.put("transferImmediateQueue",done); TraceLog traceLog = TraceLog.builder() .appCode(context.getWorOrder().getWorkCode()) .url("["+hostAddress+"]redisKeyExpirationListener.onMessage") .target(this.getClass().getPackage().getName() + "." + this.getClass().getSimpleName()) .responseBody(responseBody.toJSONString()) .responseTime(TimeTools.createNowTime()) .traceType(TraceTypeEnum.REDIS_KEY_EXPIRE.getIndex()) .build(); consumer.accept(traceLog); traceLogService.insertRecord(traceLog); } catch (Exception e) { log.error("saveTraceLog exception,[context={}]",JSONObject.toJSONString(context),e); } }/** * 从字符串截取制定的工单号 * @param value * @return */ String getWorkCode(String value){ return value.substring(value.lastIndexOf("_") + 1); } }

    2.4、延迟订单既定处理桥接器
    该类的主要作用,就是通过WorkOrderQueueTransfer实现队列元素的转移,同时通过OperateStrategyManager实现工单的数据库表操作。
    /** * @description: 延迟订单既定处理处理桥接 * 场景描述:挂起|转存 工单到既定时间的处理 * @Date : 2020/7/23 22:20 * @Author : Seig Heil */ @Slf4j @Component public class DelayedScheduledOperateBridge {static final String LOCK_KEY = CarthageConst.KEY_EXPIRE_LISTENER_LOCK; static final long EXPIRE_SECONDS = 120; @Autowired RedisService redisService; @Autowired WorkOrderQueueTransfer workOrderQueueTransfer; @Autowired OperateStrategyManager operateStrategyManager; /** * 实现从延迟队列到正式队列的转移业务处理,同时更新工单的状态 * @param cacheType * @param delayedContext * @return */ public Boolean transferImmediateQueue(WorkOrderCacheManager.CacheType cacheType, WorkOrderContext delayedContext){ String workCode = delayedContext.getWorOrder().getWorkCode(); boolean tryLock = false; String redisKey = null; try { redisKey = redisService.getKey(MessageFormat.format(LOCK_KEY,workCode)); tryLock = redisService.lock(redisKey,EXPIRE_SECONDS); if(!tryLock){ log.info("[DelayedScheduledOperateBridge.tryLock={}获取锁失败,redisKey={}]挂起|转存既定时间处理幂等,workCode={}",tryLock,redisKey,workCode); } if(tryLock){ log.info("[DelayedScheduledOperateBridge.tryLock={}获取锁成功,redisKey={}]挂起|转存既定时间处理幂等,workCode={}",tryLock,redisKey,workCode); Boolean done = workOrderQueueTransfer.transferImmediateQueue(cacheType,delayedContext); if(!done.booleanValue()){ return Boolean.FALSE; } OperateContext operateContext = OperateContext.builder() .operateStrategyEnum(OperateContext.OperateStrategyEnum.DELAYED_SCHEDULED_ORDER) .operateParam( OperateContext.OperateParam.builder().workCode(workCode).build() ).build(); operateStrategyManager.execute(operateContext); log.info("[DelayedScheduledOperateBridge.transferImmediateQueue],delayedContext={},callResult={}", JSONObject.toJSONString(delayedContext),JSONObject.toJSONString(operateContext.getExecuteResult())); return operateContext.getExecuteResult().isSuccess(); } } catch (Exception e) { log.error("[DelayedScheduledOperateBridge]挂起|转存既定时间处理异常,workCode={},delayedContext={}",workCode,JSONObject.toJSONString(delayedContext)); }finally { if(tryLock){ redisService.unlock(redisKey); } } return false; } }

    2.5、工单操作管理器
    该类的主要作用,就是对外暴露工单操作策略类的管理,外部无需关注策略类的存在,策略类实例的创建由该类负责。
    • 通过OPERATE_STRATEGY_MAP维护枚举和策略类Bean的映射。
    • 通过init()实现OPERATE_STRATEGY_MAP容器的初始化工作。
    • 通过Result execute(OperateContext context)实现对外提供策略类的操作。
    /** * @description: GPS工单操作策略管理类 * @Date : 2020/7/15 下午5:43 * @Author : 石冬冬-Seig Heil */ @Component @Slf4j public class OperateStrategyManager {static final Map OPERATE_STRATEGY_MAP = Maps.newHashMapWithExpectedSize(6); @Autowired CreateOperateStrategy createOperateStrategy; @Autowired AllotOrderOperateStrategy allotOrderOperateStrategy; @Autowired SubmitWithFinishOperateStrategy submitWithFinishOperateStrategy; @Autowired SubmitWithStoreOperateStrategy submitWithStoreOperateStrategy; @Autowired SubmitWithSuspendOperateStrategy submitWithSuspendOperateStrategy; @Autowired DelayedScheduledOperateStrategy delayedScheduledOperateStrategy; @Autowired AssignOrderOperateStrategy assignOrderOperateStrategy; @PostConstruct private void init() { OPERATE_STRATEGY_MAP.put(OperateContext.OperateStrategyEnum.INIT_WORK_ORDER, createOperateStrategy); OPERATE_STRATEGY_MAP.put(OperateContext.OperateStrategyEnum.ALLOT_WORK_ORDER, allotOrderOperateStrategy); OPERATE_STRATEGY_MAP.put(OperateContext.OperateStrategyEnum.STORE_WORK_ORDER, submitWithStoreOperateStrategy); OPERATE_STRATEGY_MAP.put(OperateContext.OperateStrategyEnum.SUSPEND_WORK_ORDER, submitWithSuspendOperateStrategy); OPERATE_STRATEGY_MAP.put(OperateContext.OperateStrategyEnum.FINISH_WORK_ORDER, submitWithFinishOperateStrategy); OPERATE_STRATEGY_MAP.put(OperateContext.OperateStrategyEnum.DELAYED_SCHEDULED_ORDER, delayedScheduledOperateStrategy); OPERATE_STRATEGY_MAP.put(OperateContext.OperateStrategyEnum.ASSIGN_ORDER, assignOrderOperateStrategy); }/** * 对外提供对策略类的调用 * @param context * @return */ public Result execute(OperateContext context) { StopWatch stopWatch = new StopWatch(); stopWatch.start("OperateStrategyManager.execute"); AbstractOperateStrategy operateStrategy = OPERATE_STRATEGY_MAP.get(context.getOperateStrategyEnum()); context.buildExecuteResultWithSuccess(); operateStrategy.execute(context); Result executeResult = context.getExecuteResult(); if(context.getExecuteResult().isSuccess()) { return Result.suc(executeResult.getMsg()); } stopWatch.stop(); long spendMillSeconds = stopWatch.getLastTaskTimeMillis(); long duration = (System.currentTimeMillis() - spendMillSeconds) / 1000; String executeResultMsg = executeResult.getMsg(); log.info("[execute] done,duration={},executeResultMsg={}",duration,executeResultMsg); return Result.fail(RemoteEnum.FAILURE, executeResultMsg); } }

    2.6、工单策略类实现
    【程序员|灵感来了挡也挡不住,基于Redis解决业务场景中延迟队列】由于工单涉及到创建、挂起、转存、处理完结等操作,因此对于这类场景我们可以通过策略类来实现。
    2.6.1、挂起操作
    /** * @description: 提交调查结果(挂起操作)-策略类 * @Date : 2020/7/15 下午5:32 * @Author : 石冬冬-Seig Heil */ @Slf4j @Component public class SubmitWithSuspendOperateStrategy extends AbstractSubmitOperateStrategy{static final Map suspend_to_attention_event_map = new HashMap<>(); static final Map suspend_to_sub_status_map = new HashMap<>(); static final Map suspend_count_map = new HashMap<>(); static {suspend_to_attention_event_map.put(MoveToEnum.SUSPENDED_AT_ONCE,AttentionEventEnum.SUSPENDED_AT_ONCE); suspend_to_attention_event_map.put(MoveToEnum.SUSPENDED_AT_TWICE,AttentionEventEnum.SUSPENDED_AT_TWICE); suspend_to_sub_status_map.put(MoveToEnum.SUSPENDED_AT_ONCE,WorkOrderStatusEnum.SubStatusEnum.SUSPENDED_AT_ONCE); suspend_to_sub_status_map.put(MoveToEnum.SUSPENDED_AT_TWICE,WorkOrderStatusEnum.SubStatusEnum.SUSPENDED_AT_TWICE); suspend_count_map.put(MoveToEnum.SUSPENDED_AT_ONCE,1); suspend_count_map.put(MoveToEnum.SUSPENDED_AT_TWICE,2); log.info("init... suspend_to_attention_event_map={}",suspend_to_attention_event_map.toString()); log.info("init... suspend_to_sub_status_map={}",suspend_to_sub_status_map.toString()); log.info("init... suspend_count_map={}",suspend_count_map.toString()); }@Autowired DiamondConfigProxy diamondConfigProxy; @Override public void prepare(OperateContext context) { super.prepare(context); SurveyResult surveyResult = context.getSurveyResult(); MoveToEnum moveToEnum = MoveToEnum.getByIndex(surveyResult.getMoveTo()); AttentionEvent attentionEvent = suspend_to_attention_event_map.getOrDefault(moveToEnum,null); ATTENTION_EVENT_CONTEXT.set(attentionEvent); context.setAttentionEvent(attentionEvent); }@Override WorkOrder buildWorkOrder(OperateContext context){ SurveyResult surveyResult = context.getSurveyResult(); MoveToEnum moveToEnum = MoveToEnum.getByIndex(surveyResult.getMoveTo()); WorkOrder workOrder = super.buildWorkOrder(context); workOrder.setSuspendedCount(suspend_count_map.getOrDefault(moveToEnum,0).intValue()); workOrder.setMainStatus(WorkOrderStatusEnum.WAITING.getIndex()); workOrder.setSubStatus(suspend_to_sub_status_map.get(moveToEnum).getIndex()); workOrder.setIsFinished(Const.NON_INDEX); workOrder.setIsStore(Const.NON_INDEX); workOrder.setDelayedTime(context.getOperateParam().getDelayedTime()); return workOrder; }@Override void operationExtend(OperateContext context) { long delayedTime = context.getOperateParam().getDelayedTime().getTime(); int delayedSeconds = context.getOperateParam().getDelayedSeconds(); WorkOrder workOrder = context.getWorkOrder(); WorkOrderContext cxt = WorkOrderContext.buildSuspended(workOrder.getWorkCode(),workOrder.getCasePriority(),delayedTime); workOrderQueueManager.leftPush(cxt); WorkOrderCacheManager.CacheValue cacheValue = https://www.it610.com/article/WorkOrderCacheManager.CacheValue. buildSuspended(workOrder.getWorkCode(),workOrder.getCasePriority(),delayedTime,delayedSeconds); workOrderCacheManager.setCacheInExpire(cacheValue); super.operationExtend(context); }@Override public void setDelayedTime(OperateContext context) { SurveyResult surveyResult = context.getSurveyResult(); MoveToEnum moveToEnum = MoveToEnum.getByIndex(surveyResult.getMoveTo()); DiamondConfig.SuspendOrderConfig suspendOrderConfig = diamondConfigProxy.suspendOrderConfig(); Date delayedTime = TimeTools.createNowTime(); int timeUnit = Calendar.HOUR_OF_DAY; int delayedSeconds = 0; int value = suspendOrderConfig.getConfig().getOrDefault(moveToEnum.name(),0); switch (suspendOrderConfig.getTimeUnit()){ case"DAY": timeUnit = Calendar.DAY_OF_YEAR; delayedSeconds = value * 24 * 3600; break; case "HOUR": timeUnit = Calendar.HOUR_OF_DAY; delayedSeconds = value * 3600; break; case "MINUTE": timeUnit = Calendar.MINUTE; delayedSeconds = value * 60; break; case "SECOND": timeUnit = Calendar.SECOND; delayedSeconds = value; break; default: break; }TimeTools.addTimeField(delayedTime, timeUnit,value); context.getOperateParam().setDelayedTime(delayedTime); context.getOperateParam().setDelayedSeconds(delayedSeconds); } }

    2.6.2、转存操作
    /** * @description: 提交调查结果(转存操作)-策略类 * @Date : 2020/7/15 下午5:32 * @Author : 石冬冬-Seig Heil */ @Slf4j @Component public class SubmitWithStoreOperateStrategy extends AbstractSubmitOperateStrategy{ /** * 转存天数 换算 秒数 */ static final int DAY_TO_SECONDS = 24 * 60 * 60; @Override public void prepare(OperateContext context) { ATTENTION_EVENT_CONTEXT.set(AttentionEventEnum.STORE_ORDER); context.setAttentionEvent(AttentionEventEnum.STORE_ORDER); super.prepare(context); }@Override public boolean paramCheck(OperateContext context) { if(Objects.isNull(context.getSurveyResult().getDelayedDays())){ context.buildExecuteResultWithFailure("[surveyResult.delayedDays]为空!"); } if(context.getSurveyResult().getDelayedDays() == 0){ context.buildExecuteResultWithFailure("等待天数[delayedDays]必须大于0!"); } return super.paramCheck(context); }@Override WorkOrder buildWorkOrder(OperateContext context){ WorkOrder workOrder = super.buildWorkOrder(context); workOrder.setMainStatus(WorkOrderStatusEnum.PENDING.getIndex()); workOrder.setSubStatus(WorkOrderStatusEnum.SubStatusEnum.STORED.getIndex()); workOrder.setIsFinished(Const.NON_INDEX); workOrder.setIsStore(Const.YES_INDEX); //setSuspendedCount 这里需要重置为0,转存后派单流程状态依赖该字段 workOrder.setSuspendedCount(0); workOrder.setDelayedTime(context.getOperateParam().getDelayedTime()); return workOrder; }@Override void operationExtend(OperateContext context) { long delayedTime = context.getOperateParam().getDelayedTime().getTime(); int delayedSeconds = context.getOperateParam().getDelayedSeconds(); WorkOrder workOrder = context.getWorkOrder(); WorkOrderContext cxt = WorkOrderContext.buildStored(workOrder.getWorkCode(),workOrder.getCasePriority(),delayedTime); workOrderQueueManager.leftPush(cxt); WorkOrderCacheManager.CacheValue cacheValue = https://www.it610.com/article/WorkOrderCacheManager.CacheValue. buildStored(workOrder.getWorkCode(),workOrder.getCasePriority(),delayedTime,delayedSeconds); workOrderCacheManager.setCacheInExpire(cacheValue); super.operationExtend(context); }@Override public void setDelayedTime(OperateContext context) { int delayedDays = context.getSurveyResult().getDelayedDays(); Date delayedTime = TimeTools.createNowTime(); TimeTools.addTimeField(delayedTime, Calendar.DAY_OF_YEAR,delayedDays); context.getOperateParam().setDelayedTime(delayedTime); context.getOperateParam().setDelayedSeconds(delayedDays * DAY_TO_SECONDS); } }

    三、交互UI介绍
    • 系统链路日志
    程序员|灵感来了挡也挡不住,基于Redis解决业务场景中延迟队列
    文章图片


    我们可以实现对于key失效事件监听,把工单落库,便于后期排查问题,无需再查看服务器日志。
    • 队列监控
    程序员|灵感来了挡也挡不住,基于Redis解决业务场景中延迟队列
    文章图片


    输出队列元素,直观看到当前挂起、转存的工单。我们可以直观看到key的剩余存活时间。
    • 参数配置
    挂起配置,尽管业务实现按照天维度、但是我代码依然实现了可以支持天、时、分、秒维度,提供了可扩展性。具体可以看 SubmitWithStoreOperateStrategy类 方法void setDelayedTime(OperateContext context) 的实现。
    四、总结
    在接到该需求后,尽管这个只是其中需求的一小部分,整个产品需求33页,内容还是丰富的,对于延迟队列操作,我在调研技术方案时,也考虑过其他方法,譬如java自带的带有延迟特性的 队列DelayedQueue(对于分布式多实例场景它就不适合了),以及可以通过RabbitMQ实现(感觉实现比较复杂),最终选择了Redis(可以利用相关数据特性比如ZSet,String,Expire),技术实现比较简单上手。

      推荐阅读