1.pom.xml 的依赖。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
2. yml 文件的配置
# redis
spring:
redis:
host: 127.0.0.1
port: 6379
# password:
database: 1
timeout: 5000
jedis:
pool:
max-active: 8
max-wait: 1
max-idle: 500
min-idle: 0
3. redis 配置文件与监听
package com.hou.config;
import com.hou.queue.MessageReceiver;
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.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
/**
* redis配置
*
*/
@Configuration
public class RedisConfig {
/**
* redis消息监听器容器
* 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器
* 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
* @param connectionFactory
* @param listenerAdapter1
* @param listenerAdapter2
* @return
*/
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter1,
MessageListenerAdapter listenerAdapter2) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//订阅了一个叫chat的通道
container.addMessageListener(listenerAdapter1, new PatternTopic("chat1"));
//container 可以添加多个 messageListener
container.addMessageListener(listenerAdapter2, new PatternTopic("chat2"));
return container;
}
/**
* 消息监听器适配器,绑定消息处理器,利用反射技术调用消息处理器的业务方法
* @param receiver
* @return
*/
@Bean
MessageListenerAdapter listenerAdapter1(MessageReceiver receiver) {
//给messageListenerAdapter 传入一个消息接受的处理器,利用反射的方法调用“receiveMessage”
//不填defaultListenerMethod默认调用handleMessage
return new MessageListenerAdapter(receiver, "receiverMessage1");
}
@Bean
MessageListenerAdapter listenerAdapter2(MessageReceiver receiver) {
return new MessageListenerAdapter(receiver, "receiverMessage2");
}
/**
* 读取内容的template
*/
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
}
4. 消息的接受处理
package com.hou.queue;
import org.springframework.stereotype.Component;
/**
* 消息处理接收到的方法
*
* @author
* @create
*/
@Component
public class MessageReceiver {
/**
* 接收消息方法
*/
public void receiverMessage1(String message) {
System.out.println("收到一条chat1新消息:" + message);
}
public void receiverMessage2(String message) {
System.out.println("收到一条chat2新消息:" + message);
}
}
5. 消息的发送
//开启定时器功能
@EnableScheduling
@Component
public class MessageSender {
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 间隔2秒,通过stringRedisTemplate对象向redis消息队列chat频道发布消息
*/
@Scheduled(fixedDelay = 2000)
public void sendMessage1() {
stringRedisTemplate.convertAndSend("chat1", String.valueOf(Math.random()));
}
@Scheduled(fixedDelay = 2000)
public void sendMessage2() {
stringRedisTemplate.convertAndSend("chat2", String.valueOf(System.currentTimeMillis()));
}
}
至此就完成了,redis 实现了消息的订阅与发送。