springboot 基于 redis redisson的消息队列

springboot 基于 redis redisson的消息队列

消息接收方

@Slf4j
@Component
@Order(200)
public class ClickQueueMessageReceive implements ApplicationRunner {

    @Resource
    private RedissonClient redissonClient;

    @Async("birdsExecutor")
    @Override
    public void run(ApplicationArguments args) throws Exception {
        while (true){
        
            try {
                RBlockingQueue<NovelIdTopicDto> blockingQueue =
    redissonClient.getBlockingQueue(TopicAndQueueKeyConst.CLICK_QUEUE);
                NovelIdTopicDto novelIdTopicDto = blockingQueue.take();
               //do some 
               
            } catch (InterruptedException e) {
                log.warn(e.getMessage(),e);
            }
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                log.info(e.getMessage(),e);
            }
        }
    }
}

消息发送方

@Slf4j
@Service
public class TopicAndQueuePushService {

    @Resource
    private RedissonClient redissonClient;

    @Async
    public void sendRead(String novelId){
        RBlockingQueue<NovelIdTopicDto> blockingQueue =
                redissonClient.getBlockingQueue(TopicAndQueueKeyConst.CLICK_QUEUE);
        blockingQueue.putAsync(getNovelIdTopicDto(novelId));
    }
}

完整代码查看

github开源地址(飞鸟小说):https://github.com/caobinrg/birds-novel
gitee开源地址(飞鸟小说) : https://gitee.com/caobinrg/birds-novel

Spring Boot 应用程序中使用 Redisson 实现消息队列是一个常见的应用场景。Redisson 提供了一些功能来支持消息队列的实现,例如使用 Redis 的 List 数据结构来作为消息队列的存储容器。 首先,你需要在你的 Spring Boot 项目中添加 Redisson 的依赖。可以通过 Maven 或 Gradle 来引入 Redisson 的相关依赖。 接下来,在你的应用程序中配置 Redisson 的连接信息,包括 Redis 的主机名、端口号、密码等。你可以使用 Spring Boot 的配置文件来进行配置,或者通过编程方式进行配置。 在配置完成后,你可以使用 Redisson 提供的 RQueue 接口来操作消息队列。RQueue 接口继承自 Java 的 Queue 接口,并提供了一些额外的方法来实现消息队列的特性,如 push、poll、size 等。 你可以使用 RQueue 的 push 方法将消息推送到队列中,使用 poll 方法从队列中获取并移除消息。你还可以使用 size 方法获取队列的长度等信息。 另外,Redisson 还提供了一些高级功能来支持更复杂的消息队列场景,如延迟队列、优先级队列等等。你可以根据自己的需求选择合适的功能来实现消息队列。 总结起来,Spring Boot Redisson 提供了一种简单而强大的方式来实现消息队列功能,通过配置和使用 Redisson 提供的相关接口,你可以轻松地在你的 Spring Boot 应用程序中实现消息队列,并提高应用程序的可扩展性和性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值