|
| 1 | +package com.git.hui.boot.redis.list.component; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSONObject; |
| 4 | +import lombok.Getter; |
| 5 | +import lombok.ToString; |
| 6 | +import org.springframework.beans.BeansException; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.context.ApplicationContext; |
| 9 | +import org.springframework.context.ApplicationContextAware; |
| 10 | +import org.springframework.context.ApplicationEvent; |
| 11 | +import org.springframework.data.redis.core.StringRedisTemplate; |
| 12 | +import org.springframework.scheduling.annotation.Scheduled; |
| 13 | +import org.springframework.stereotype.Component; |
| 14 | +import org.springframework.util.CollectionUtils; |
| 15 | + |
| 16 | +import java.util.Set; |
| 17 | +import java.util.concurrent.CopyOnWriteArraySet; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author wuzebang |
| 21 | + * @date 2021/5/7 |
| 22 | + */ |
| 23 | +@Component |
| 24 | +public class RedisDelayListWrapper implements ApplicationContextAware { |
| 25 | + private static final Long DELETE_SUCCESS = 1L; |
| 26 | + @Autowired |
| 27 | + private StringRedisTemplate redisTemplate; |
| 28 | + |
| 29 | + private Set<String> topic = new CopyOnWriteArraySet<>(); |
| 30 | + |
| 31 | + public void publish(String key, Object val, long delayTime) { |
| 32 | + topic.add(key); |
| 33 | + String strVal = val instanceof String ? (String) val : JSONObject.toJSONString(val); |
| 34 | + |
| 35 | + redisTemplate.opsForZSet().add(key, strVal, System.currentTimeMillis() + delayTime); |
| 36 | + } |
| 37 | + |
| 38 | + public String fetchOne(String key) { |
| 39 | + Set<String> sets = redisTemplate.opsForZSet().rangeByScore(key, 0, System.currentTimeMillis(), 0, 3); |
| 40 | + if (CollectionUtils.isEmpty(sets)) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + for (String val : sets) { |
| 45 | + if (DELETE_SUCCESS.equals(redisTemplate.opsForZSet().remove(key, val))) { |
| 46 | + // 删除成功,表示抢占到 |
| 47 | + return val; |
| 48 | + } |
| 49 | + } |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + @Scheduled(fixedRate = 10_000) |
| 54 | + public void schedule() { |
| 55 | + for (String specialTopic : topic) { |
| 56 | + String cell = fetchOne(specialTopic); |
| 57 | + if (cell != null) { |
| 58 | + applicationContext.publishEvent(new DelayMsg(this, cell, specialTopic)); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private ApplicationContext applicationContext; |
| 64 | + |
| 65 | + @Override |
| 66 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 67 | + this.applicationContext = applicationContext; |
| 68 | + } |
| 69 | + |
| 70 | + @ToString |
| 71 | + public static class DelayMsg extends ApplicationEvent { |
| 72 | + @Getter |
| 73 | + private String msg; |
| 74 | + @Getter |
| 75 | + private String topic; |
| 76 | + |
| 77 | + /** |
| 78 | + * Create a new {@code ApplicationEvent}. |
| 79 | + * |
| 80 | + * @param source the object on which the event initially occurred or with |
| 81 | + * which the event is associated (never {@code null}) |
| 82 | + */ |
| 83 | + public DelayMsg(Object source, String msg, String topic) { |
| 84 | + super(source); |
| 85 | + this.msg = msg; |
| 86 | + this.topic = topic; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments