当前位置: 首页 > article >正文

redis list 单推送消息,批量消费消息,springboot实现

在Redis中,列表(List)是一种数据结构,可以用来作为消息队列。以下是如何在Spring Boot中使用Redis List作为消息队列进行消息推送和批量消费消息的示例代码。

首先,确保你的pom.xml文件中包含了Spring Boot和Spring Data Redis的依赖。

然后,配置Redis服务,在application.propertiesapplication.yml中添加Redis配置:

properties

复制

# application.properties
spring.redis.host=localhost
spring.redis.port=6379
# 如果需要密码
# spring.redis.password=yourpassword

下面是使用Redis List作为消息队列的代码示例:

消息推送(生产者):

java

复制

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisListMessageService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public void pushMessage(String key, String message) {
        redisTemplate.opsForList().leftPush(key, message);
    }
}

批量消费消息(消费者):

java

复制

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class RedisListConsumerService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private static final String QUEUE_KEY = "your-queue-key";
    private static final int BATCH_SIZE = 10;

    @Scheduled(fixedRate = 5000) // 每5秒执行一次
    public void consumeMessages() {
        while (true) {
            List<String> messages = redisTemplate.opsForList().range(QUEUE_KEY, 0, BATCH_SIZE - 1);
            if (messages.isEmpty()) {
                break;
            }
            for (String message : messages) {
                // 处理消息
                System.out.println("Consumed message: " + message);
                // 消费完消息后,从队列中移除
                redisTemplate.opsForList().remove(QUEUE_KEY, 1, message);
            }
        }
    }
}

启用定时任务:

在Spring Boot应用中,你需要使用@EnableScheduling注解来启用定时任务。

java

复制

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class RedisListQueueApplication {

    public static void main(String[] args) {
        SpringApplication.run(RedisListQueueApplication.class, args);
    }
}

在上面的代码中,RedisListMessageService类用于推送消息到Redis List。RedisListConsumerService类包含一个定时任务,它定期从Redis List中批量获取并消费消息。这个例子使用了Spring的@Scheduled注解来定期执行任务。

注意,fixedRate属性表示任务的执行频率,而rangeremove方法用于从队列中获取和删除消息。BATCH_SIZE定义了每次批量消费的消息数量。

在生产环境中,可能需要考虑错误处理、事务管理、消息持久化、消费者竞争条件等问题。这个示例为了简单起见,没有包含这些高级特性。


http://www.kler.cn/news/283664.html

相关文章:

  • Nginx配置实例-负载均衡
  • 密码学(二)---DES、SM、RSA
  • c++中的匿名对象及内存管理及模版初阶
  • 【系统架构师软考】计算机网络知识(四)
  • 在类Unix操作系统(如Linux)上运行Windows应用程序方法小记
  • flutter和原生Android以及IOS开发相比有什么优缺点?
  • Gradio学习——图像流输出
  • ShenNiusModularity项目源码学习(3:用户登录)
  • MFC工控项目实例之七点击下拉菜单弹出对话框
  • Python使用总结之Flask-SocketIO介绍
  • 查看显卡cuda版本
  • PD协议沟通过程
  • 最大池化、非线性激活、线性层
  • 【C++ Qt day3】
  • PrimeVue DataTable 属性值解析
  • validationtools中按键测试选项光标移除
  • JavaEE 第18节 TCPUDP优缺点(对比)
  • 基于SVM的手势识别,SVM工具箱详解,SVM工具箱使用注意事项
  • 【策略方法】设计模式:构建灵活的算法替换方案
  • 已经git push,但上传的文件超过100MB
  • 都2024了,还在为uniapp的app端tabbar页面闪烁闪白而发愁吗
  • AI:引领未来的科技浪潮
  • 基于vue框架的餐馆管理系统jo0i7(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
  • 解决Vite+Vue3打包项目本地运行html白屏和报错问题
  • 【iOS】Masonry学习
  • EasyCode实现完整CRUD + 分页封装
  • RateLimiter超时
  • Memcached stats items 命令
  • JVM运行时数据区详解
  • 全球视角下的AI应用:国内外技术与实践的比较分析