zoukankan      html  css  js  c++  java
  • redis延迟队列

    异步消息队列
    Redis 的 list(列表) 数据结构常用来作为异步消息队列使用,使用rpush/lpush操作入队列,
    使用 lpop 和 rpop 来出队列。
    > rpush notify-queue apple banana pear
    (integer) 3
    > llen notify-queue
    (integer) 3
    > lpop notify-queue
    "apple"
    > llen notify-queue
    (integer) 2
    > lpop notify-queue
    "banana"
    > llen notify-queue
    (integer) 1
    > lpop notify-queue
    "pear"
    > llen notify-queue
    (integer) 0
    > lpop notify-queue
    (nil)
    上面是 rpush 和 lpop 结合使用的例子。还可以使用 lpush 和 rpop 结合使用,效果是一
    样的。
     
    队列空了怎么办?
    通常我们使用 sleep 来解决这个问题,让线程睡一会,睡个 1s 钟就可以了。不但客户端
    的 CPU 能降下来,Redis 的 QPS 也降下来了。
    Thread.sleep(1000) # java 睡 1s
     
    队列延迟
    阻塞读在队列没有数据的时候,会立即进入休眠状态,一旦数据到来,则立刻醒过来。消
    息的延迟几乎为零。用 blpop/brpop 替代前面的 lpop/rpop,就完美解决了上面的问题。
    但是有问题就是如果线程一直阻塞在哪里,Redis 的客户端连接就成了闲置连接,闲置过久,服务器一般
    会主动断开连接,减少闲置资源占用。这个时候 blpop/brpop 会抛出异常来。
    所以编写客户端消费者的时候要小心,注意捕获异常,还要重试。
     
    延时队列的实现
    import java.lang.reflect.Type; 
    import java.util.Set; 
    import java.util.UUID;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.TypeReference; 
    import redis.clients.jedis.Jedis; 
    public class RedisDelayingQueue<T> { 
    static class TaskItem<T> { 
    public String id; 
    public T msg; 
    } 
    // fastjson 序列化对象中存在 generic 类型时,需要使用 TypeReference 
    private Type TaskType = new TypeReference<TaskItem<T>>() { }.getType(); 
    private Jedis jedis; 
    private String queueKey; 
    public RedisDelayingQueue(Jedis jedis, String queueKey) { 
    this.jedis = jedis; 
    this.queueKey = queueKey; 
    } 
    public void delay(T msg) { 
    TaskItem task = new TaskItem(); 
    task.id = UUID.randomUUID().toString(); // 分配唯一的 uuid 
    task.msg = msg; 
    String s = JSON.toJSONString(task); // fastjson 序列化
    jedis.zadd(queueKey, System.currentTimeMillis() + 5000, s); // 塞入延时队列 ,5s 后再试
    } 
    public void loop() { 
    while (!Thread.interrupted()) {
    // 只取一条
    Set values = jedis.zrangeByScore(queueKey, 0, System.currentTimeMillis(), 0, 1); 
    if (values.isEmpty()) { 
    try { 
    Thread.sleep(500); // 歇会继续
    } 
    catch (InterruptedException e) { 
    break;
    } 
    continue; 
    } 
    String s = values.iterator().next(); 
    if (jedis.zrem(queueKey, s) > 0) { // 抢到了
    TaskItem task = JSON.parseObject(s, TaskType); // fastjson 反序列化
    this.handleMsg(task.msg); 
    } 
    } 
    } 
    public void handleMsg(T msg) { 
    System.out.println(msg); 
    } 
    public static void main(String[] args) { 
    Jedis jedis = new Jedis(); 
    RedisDelayingQueue queue = new RedisDelayingQueue<>(jedis, "q-demo"); 
    Thread producer = new Thread() { 
    public void run() { 
    for (int i = 0; i < 10; i++) { 
    queue.delay("codehole" + i); 
    } 
    } 
    }; 
    Thread consumer = new Thread() { 
    public void run() { 
    queue.loop(); 
    } 
    }; 
    producer.start(); 
    consumer.start(); 
    try { 
    producer.join(); 
    Thread.sleep(6000); 
    consumer.interrupt(); 
    consumer.join(); 
    } 
    catch (InterruptedException e) { 
    } 
    } 
    }
     
  • 相关阅读:
    Web API DataContract DataMember Serializable简单解释
    v-bind和v-on
    CSS大写转换
    Web API 实体显示注释
    Web API性能优化(一)压缩
    时间序列化问题
    border 0px和border none的区别
    CSS实现单行、多行文本溢出显示省略号
    MySQL防止重复插入记录SQL
    mvc和webapi同一解决方案调试办法
  • 原文地址:https://www.cnblogs.com/wuwuyong/p/11738918.html
Copyright © 2011-2022 走看看