分享一个使用 redisson 实现 redis 消费队列只消费一次的实现方法

因为 redis 的消费队列并没有提供多消费只消费一次的功能,如果想要实现多消费(实例)保证只消费一次,则需要自己在消费端去实现。
简单封装一下 Redisson 中的 RTopic 的监听方法:

redis 5.0 之后添加了 Redis Stream 数据结构, 支持消费者分组和消息窗口持久化等功能, 若是满足版本可直接使用官方支持。
https://redis.io/docs/data-types/streams/

public class RedisMessageUnrepeatable {

    private static final String INDEX_SUFFIX = "_unrepeatable";

    private static final String RW_SUFFIX = "_rwLock";
	
    private final RedissonClient client;

	// 主题名
    private final String topic;
	
	// 序列化方式
    private final Codec codec;

    private final RTopic rTopic;

    private final RAtomicLong topicIndex;

    private final RReadWriteLock rwLock;

    public RedisMessageUnrepeatable(@NonNull RedissonClient client, @NonNull String topic, Codec codec) {
        this.client = client;
        this.topic = topic;
        this.codec = codec;
        this.rTopic = client.getTopic(topic, codec);
        this.topicIndex = client.getAtomicLong(topic + INDEX_SUFFIX);
        rwLock = client.getReadWriteLock(topic + RW_SUFFIX);
    }

    public <M> void addListener(Class<M> type, MessageListener<M> listener) {
        // 获取写锁, 保证在注册新的消费者的时候 topicIndex 不会被修改.
        // 若在注册的时候 topicIndex 在改变, 可能导致与 currentIndex 不一致,
        // 从而导致 currentIndex 永远比 topicIndex 小, 造成该消费者永远消费不到消息.
        rwLock.writeLock().lock();
        try {
            final AtomicLong currentIndex = new AtomicLong(topicIndex.get());
            rTopic.addListener(type, (charSequence, s) -> {
                // 获取读锁, 读锁是共享锁.
                rwLock.readLock().lock();
                try {
                    if (!topicIndex.compareAndSet(currentIndex.get(),
                            currentIndex.incrementAndGet())) {
                        return;
                    }
                    listener.onMessage(charSequence, s);
                }finally {
                    rwLock.readLock().unlock();
                }
            });
        }finally {
            rwLock.writeLock().unlock();
        }
    }

    public RedissonClient getClient() {
        return client;
    }

    public String getTopic() {
        return topic;
    }

    public Codec getCodec() {
        return codec;
    }
}

简单使用:

@Component
public class Test{

	@Autowired
    private RedissonClient client;
	
	@PostConstruct
	public void listener(){
		RedisMessageUnrepeatable redisMq = new RedisMessageUnrepeatable(client, "test", StringCodec.INSTANCE);
		redisMq.addListener(String.class, (t, m) -> System.out.printf("topic: %s, message: %s \n", t, m));
	}
}

基本思想:

  1. 在 redis 中为每一个消息队列维护一个 topicIndex, 表示当前消费的消息序列。
  2. 在每一个消费者中维护一个 currentIndex, 表示当前消费者已消费到的消息序列, 创建时与 topicIndex 同步。
  3. redis 会为每一个消费者都推送每一条消息, 当消费者拿到当前消息时, 会将本地的 currentIndex 和 currentIndex 加一后的结果与 redis 中的 topicIndex 进行 CAS。
    1. 如果成功的将替换 topicIndex 的值则表示能够消费当前数据,随后执行具体消费逻辑。
    2. 如果未能替换 topicIndex , 则表示消息序列为 currentIndex 的消息已被其它的消费者消费, 则跳过当前消息处理下一条消息。
    3. 无论是否消费到消息, currentIndex 都将加一。
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Redisson一个基于RedisJava客户端,提供了分布式锁、分布式集合、分布式对象等功能。在Redisson中,消息队列实现主要是通过Redis的list数据结构来实现的。 下面是一个简单的Redisson实现消息队列的示例: 1. 引入Redisson依赖 ```xml <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.15.5</version> </dependency> ``` 2. 创建Redisson客户端 ```java Config config = new Config(); config.useSingleServer().setAddress("redis://localhost:6379"); RedissonClient redisson = Redisson.create(config); ``` 3. 创建消息队列并添加消息 ```java RQueue<String> queue = redisson.getQueue("myQueue"); queue.add("Hello"); queue.add("World"); ``` 4. 消费消息队列中的消息 ```java while (true) { String message = queue.take(); if (message != null) { System.out.println(message); } } ``` 在这个示例中,我们首先创建了一个Redisson客户端,然后创建了一个名为“myQueue”的消息队列,并添加了两个消息。最后,我们使用一个无限循环来消费消息队列中的消息。 值得注意的是,Redisson的消息队列是阻塞的,即当消息队列为空时,消费者线程会被阻塞,直到队列中有新的消息到来。因此,在消费消息时,我们需要使用take()方法而不是poll()方法。 除此之外,Redisson还提供了许多其他的分布式数据结构,如分布式锁、分布式集合、分布式Map等,可以满足不同场景下的分布式应用需求。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值