1,application.properties配置redis以及連接池
#redis
spring.redis.host=localhost
spring.redis.port=6379
#spring.redis.password=
spring.redis.database=1
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=500
spring.redis.pool.min-idle=0
spring.redis.timeout=0
2,消息發佈者、消息處理者POJO、redis消息監聽器容器以及redis監聽器注入IOC容器
- redis configuration
@Configuration //相當於xml中的beans
public class RedisConfig {
/**
* redis消息監聽器容器
* 可以添加多個監聽不同話題的redis監聽器,只需要把消息監聽器和相應的消息訂閱處理器綁定,該消息監聽器
* 通過反射技術調用消息訂閱處理器的相關方法進行一些業務處理
* @param connectionFactory
* @param listenerAdapter
* @return
*/
@Bean //相當於xml中的bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//訂閱了一個叫chat 的通道
container.addMessageListener(listenerAdapter, new PatternTopic("chat"));
//這個container 可以添加多個 messageListener
return container;
}
/**
* 消息監聽器適配器,綁定消息處理器,利用反射技術調用消息處理器的業務方法
* @param receiver
* @return
*/
@Bean
MessageListenerAdapter listenerAdapter(MessageReceiver receiver) {
//這個地方 是給messageListenerAdapter 傳入一個消息接受的處理器,利用反射的方法調用“receiveMessage”
//也有好幾個重載方法,這邊默認調用處理器的方法 叫handleMessage 可以自己到源碼裏面看
return new MessageListenerAdapter(receiver, "receiveMessage");
}
/**redis 讀取內容的template */
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
}
MessageListenerAdapter通過反射使普通的POJO就可以處理消息。具體情況見MessageListenerAdapter的onMessage方法。
3,消息發佈者
@EnableScheduling //開啓定時器功能
@Component
public class MessageSender {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Scheduled(fixedRate = 2000) //間隔2s 通過StringRedisTemplate對象向redis消息隊列chat頻道發佈消息
public void sendMessage(){
stringRedisTemplate.convertAndSend("chat",String.valueOf(Math.random()));
}
}
4,普通的消息處理器POJO
@Component
public class MessageReceiver {
/**接收消息的方法*/
public void receiveMessage(String message){
System.out.println("收到一條消息:"+message);
}
}
MessageListenerAdapter通過反射調用receiveMessage方法處理消息
5,pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.learn</groupId>
<artifactId>springboot-redis-message-pubsub-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-redis-message-pubsub-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
參考資料: