1.application.properties配置redis以及连接池
spring.redis.host=123.207.115.129
spring.redis.password=198568huiqing
spring.redis.database=0
spring.redis.port=6379
2.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.li</groupId>
<artifactId>spring-boot</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>
3.redis消息监听器容器配置
/**
* <p>
* Title:RedisConfig
* </p>
* <p>
* Description:Redis配置类
* </p>
* <p>
* Copyright:Copyright(c)2005
* </p>
* <p>
* Company:lj
* </p>
* <p>
* Author:lsj
* </p>
* <p>
* Date:2018/4/18 13:47
* </p>
*/
@Configuration
@EnableCaching
public class RedisConfig{
/**
* Redis消息监听器容器
* @param connectionFactory
* @param listenerAdapter
* @return
*/
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//订阅了一个叫pmp 的通道
container.addMessageListener(listenerAdapter,new PatternTopic("pmp"));
//这个container 可以添加多个 messageListener
return container;
}
/**
* 配置消息接收处理类
* @param redisMsgSub 自定义消息接收类
* @return
*/
@Bean
MessageListenerAdapter listenerAdapter(RedisMsgSub redisMsgSub) {
//这个地方 是给messageListenerAdapter 传入一个消息接受的处理器,利用反射的方法调用“receiveMessage”
//也有好几个重载方法,这边默认调用处理器的方法 叫handleMessage 可以自己到源码里面看
return new MessageListenerAdapter(redisMsgSub, "receiveMessage");
}
}
4.redis消息接收
/**
* <p>
* Title:RedisMsgSub
* </p>
* <p>
* Description:自定义消息接收类
* </p>
* <p>
* Copyright:Copyright(c)2005
* </p>
* <p>
* Company:skycomm.com.cn
* </p>
* <p>
* Author:lishuangjiang
* </p>
* <p>
* Date:2018/4/18 13:47
* </p>
*/
@Component
public class RedisMsgSub{
/**
* 接收消息的方法
* @param message 订阅消息
*/
public void receiveMessage(String message){
System.out.println(message);
}
}
5.redis消息多个订阅不同的实现 代码改造
5.1添加redis订阅消息接口
/**
* <p>
* Title:RedisMsg
* </p>
* <p>
* Description:TODO
* </p>
* <p>
* Copyright:Copyright(c)2005
* </p>
* <p>
* Company:lj
* </p>
* <p>
* Author:lsj
* </p>
* <p>
* Date:2018/4/18 17:18
* </p>
*/
@Component
public interface RedisMsg {
public void receiveMessage(String message);
}
5.2实现消息接口
/**
* <p>
* Title:RedisPmpSub
* </p>
* <p>
* Description:自定义消息接收类
* </p>
* <p>
* Copyright:Copyright(c)2005
* </p>
* <p>
* Company:lj
* </p>
* <p>
* Author:lsj
* </p>
* <p>
* Date:2018/4/18 13:47
* </p>
*/
public class RedisPmpSub implements RedisMsg{
/**
* 接收消息的方法
* @param message 订阅消息
*/
public void receiveMessage(String message){
System.out.println(message);
}
}
5.2配置redis监听容器
/**
* <p>
* Title:RedisConfig
* </p>
* <p>
* Description:消息监听器容器
* </p>
* <p>
* Copyright:Copyright(c)2005
* </p>
* <p>
* Company:lj
* </p>
* <p>
* Author:lsj
* </p>
* <p>
* Date:2018/4/18 13:47
* </p>
*/
@Configuration
@EnableCaching
public class RedisConfig{
/**
* Redis消息监听器容器
* @param connectionFactory
* @return
*/
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//订阅了一个叫pmp 的通道
container.addMessageListener(listenerAdapter(new RedisPmpSub()),new PatternTopic("pmp"));
container.addMessageListener(listenerAdapter(new RedisChannelSub()),new PatternTopic("channel"));
//这个container 可以添加多个 messageListener
return container;
}
/**
* 配置消息接收处理类
* @param redisMsg 自定义消息接收类
* @return
*/
@Bean()
@Scope("prototype")
MessageListenerAdapter listenerAdapter(RedisMsg redisMsg) {
//这个地方 是给messageListenerAdapter 传入一个消息接受的处理器,利用反射的方法调用“receiveMessage”
//也有好几个重载方法,这边默认调用处理器的方法 叫handleMessage 可以自己到源码里面看
return new MessageListenerAdapter(redisMsg, "receiveMessage");
}
}