java实现redis的消息发送和消费,类似kafka功能

devtools/2024/10/21 3:12:50/
确保在 pom.xml 中添加了 Spring Data Redis 和 Jedis 的依赖。如下所示:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId>
</dependency>

配置 Redis 连接
在这里插入图片描述

使用 RedisTemplate 进行操作

在 Spring Boot 中,推荐使用 RedisTemplate 进行 Redis 操作,而不是直接使用 Jedis。下面是如何创建和使用 RedisTemplate 的示例。

创建 RedisConfig 类
创建一个配置类来定义 RedisTemplate 的 Bean

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);return template;}
}

使用 RedisTemplate 发送和接收消息

接下来,可以使用 RedisTemplate 进行消息的发送和接收。

发送消息
创建一个消息生产者的类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;@Service
public class MessageProducer {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;public void sendMessage(String queueName, String message) {redisTemplate.opsForList().leftPush(queueName, message);System.out.println("Message sent to queue: " + message);}
}

接收消息 创建一个消息消费者的类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;@Service
public class MessageConsumer {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;public void consumeMessage(String queueName) {String message = (String) redisTemplate.opsForList().rightPop(queueName);if (message != null) {System.out.println("Message received from queue: " + message);} else {System.out.println("No message in queue.");}}
}

使用 Publish/Subscribe 模式

对于使用发布/订阅模式,可以创建一个订阅类,并使用 Spring 的 @MessageMapping 注解或直接使用
JedisPubSub。

发布者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;@Service
public class Publisher {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;public void publishMessage(String channelName, String message) {redisTemplate.convertAndSend(channelName, message);System.out.println("Message published to channel " + channelName + ": " + message);}
}

订阅者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;@Service
public class Subscriber {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;public void subscribe(String channelName) {Jedis jedis = (Jedis) redisTemplate.getConnectionFactory().getConnection().getNativeConnection();jedis.subscribe(new JedisPubSub() {@Overridepublic void onMessage(String channel, String message) {System.out.println("Message received from channel " + channel + ": " + message);}}, channelName);}
}

启动应用

确保你的应用程序使用了 Spring Boot 的 @SpringBootApplication 注解,并启动应用。


http://www.ppmy.cn/devtools/127449.html

相关文章

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-16

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-16 目录 文章目录 目录1. Leveraging Social Determinants of Health in Alzheimers Research Using LLM-Augmented Literature Mining and Knowledge Graphs2. Alignment Between the Decision-Making Logic of …

三菱PLC伺服-停止位置不正确故障排查

停止位置不正确时&#xff0c;请确认以下项目。 1)请确认伺服放大器(驱动单元)的电子齿轮的设定是否正确。 2&#xff09;请确认原点位置是否偏移。 1、设计近点信号(DOG)时&#xff0c;请考虑有足够为0N的时间能充分减速到爬行速度。该指令在DOG的前端开始减速到爬行速度&…

Linux下的杀毒软件介绍

Linux下的杀毒软件介绍 一、Linux杀毒软件的基本概念和作用二、Linux杀毒软件的选择三、Linux杀毒软件推荐四、Linux杀毒软件对应用进程的影响五、结论在当今数字化和网络化的环境中,保护计算机系统的安全至关重要。尽管Linux操作系统因其开源、稳定且相对安全的特性而较少受到…

【HarmonyOS NEXT】权限申请及应用设置页跳转

关键词&#xff1a;鸿蒙、程序访问控制、定位、应用详情页、startability、want 在app开发过程中&#xff0c;常进行系统权限的申请以提供设备访问或个性化功能&#xff08;如扫一扫、城市定位、剪贴板等&#xff09;&#xff0c;从而保障应用功能的完整性&#xff0c;那么本期…

数据结构与算法:数据结构的前沿研究(最终章)

目录 18.1 可持久化数据结构 18.2 随机化数据结构 18.3 内存与存储优化的数据结构 18.4 新兴数据结构与未来趋势 18.5 研究前沿与挑战 总结 数据结构与算法&#xff1a;数据结构的前沿研究&#xff08;最终章&#xff09; 随着计算机科学和技术的不断发展&#xff0c;数…

FPGA实现UDP通信(3)——数据发送实现

基于FPGA实现UDP通信,看完你就懂了!!!(附源码) 上两篇文章分别介绍了UDP通信的物理接口以及UDP通信协议,忘记的同学可以查看我的上几篇文章,介绍有关UDP通信的物理接口文章地址:FPGA实现UDP通信(2)——通信接口简介-CSDN博客 介绍有关UDP通信协议文章地址:FFGA实现UD…

spring-cloud-alibaba-nacos-config2023.0.1.*启动打印配置文件内容

**背景&#xff1a;**在开发测试过程中如果可以打印出配置文件的内容&#xff0c;方便确认配置是否准确&#xff1b;那么如何才可以打印出来呢&#xff1b; spring-cloud-alibaba-nacos-config 调整日志级别 logging:level:com.alibaba.cloud.nacos.configdata.NacosConfigD…

【win11】终端/命令提示符/powershell美化

文章目录 1.设置字体1.1. 打开win11的终端/命令提示符/powershell其中之一1.2. 打开终端设置&#xff0c;修改所有终端默认字体为新宋体 2. 修改powershell背景色为蓝色 win11的默认终端/命令提示符/powershell主题风格让人感觉与win10撕裂太大&#xff0c;尤其是字体、背景色&…