springboot redis 实现消息队列

在Spring Boot中使用Redis作为消息队列,可以通过以下步骤实现:

  1. 添加依赖

在`pom.xml`文件中添加Spring Boot Redis和Jedis的依赖:

```xml

<dependencies>

<!-- Spring Boot Redis -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<!-- Jedis -->

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

</dependency>

</dependencies>

```

  1. 配置Redis

在`application.properties`文件中配置Redis连接信息:

```properties

spring.redis.host=localhost

spring.redis.port=6379

```

  1. 创建生产者

创建一个生产者类,用于发送消息到Redis消息队列:

```java

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Component;

@Component

public class Producer {

@Autowired

private RedisTemplate<String, String> redisTemplate;

public void sendMessage(String key, String message) {

redisTemplate.convertAndSend(key, message);

}

}

```

  1. 创建消费者

创建一个消费者类,用于从Redis消息队列接收消息:

```java

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.connection.Message;

import org.springframework.data.redis.connection.MessageListener;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Component;

@Component

public class Consumer implements MessageListener {

@Autowired

private RedisTemplate<String, String> redisTemplate;

@Override

public void onMessage(Message message, byte\[\] pattern) {

System.out.println("Received message: " + message.toString());

}

}

```

  1. 配置消费者监听

在配置类中,将消费者添加到Redis消息队列的监听器列表中:

```java

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.RedisConnectionFactory;

import org.springframework.data.redis.listener.ChannelTopic;

import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration

public class RedisConfig {

@Autowired

private Consumer consumer;

@Bean

public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,

MessageListenerAdapter listenerAdapter) {

RedisMessageListenerContainer container = new RedisMessageListenerContainer();

container.setConnectionFactory(connectionFactory);

container.addMessageListener(listenerAdapter, topic());

return container;

}

@Bean

public MessageListenerAdapter listenerAdapter() {

return new MessageListenerAdapter(consumer, "onMessage");

}

@Bean

public ChannelTopic topic() {

return new ChannelTopic("messageQueue");

}

}

```

  1. 测试发送和接收消息

在Spring Boot应用的主类中,注入生产者并发送一条消息:

```java

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.CommandLineRunner;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class Application implements CommandLineRunner {

@Autowired

private Producer producer;

public static void main(String\[\] args) {

SpringApplication.run(Application.class, args);

}

@Override

public void run(String... args) throws Exception {

producer.sendMessage("messageQueue", "Hello, Redis!");

}

}

```

运行应用后,控制台将输出接收到的消息:

```

Received message: Hello, Redis!

```

相关推荐
西凉的悲伤32 分钟前
Spring Boot 中 @Async(value = “alertThreadPool“) 是什么?为什么企业项目喜欢自定义线程池?
spring boot·多线程·async·异步
AZaLEan__1 小时前
多源 BFS
java·开发语言·算法
程序员卷卷狗1 小时前
Java转Go面试速记:Go基础22问,一篇理清高频易错点一篇理清高频易错点
java·面试·golang
zzzzz3691 小时前
快速搭建SpringAi项目 集成智能问答,RAG,FUINCTION_CALLING等功能
java·ai编程
步十人1 小时前
【Redis】持久化机制
数据库·redis·缓存
笨蛋不要掉眼泪1 小时前
Java并发编程 :深入剖析LinkedBlockingQueue
java·开发语言·网络·并发
未若君雅裁1 小时前
算法复杂度与数据结构:Java 集合篇的第一块基石
java·数据结构·算法
致Great1 小时前
Claude Code 上线 Dynamic Workflows:一句话调度 1000 个子智能体并行干活
java·linux·服务器
一个做软件开发的牛马1 小时前
Java 常用类:String不可变、新时间API与包装类陷阱
java·后端
yurenpai(27届找实习中)2 小时前
redis_点评(25.附件店铺—把数据库里的店铺按【类型分组】,批量导入Redis 的 GEO 地理位置结构)
java·redis·缓存