网址:https://docs.spring.io/spring-data/redis/docs/1.8.6.RELEASE/reference/html/#pubsub
1.首先在spring配置文件中加载redis的配置文件,一下是代码块:
<!-- 属性文件读入 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>
2. 添加redis.properties 文件
redis.hostName1=
redis.hostName2=
redis.hostName3=
redis.port=6379
redis.timeout=15000
redis.usePool=true
redis.maxIdle=6
redis.maxRedirects=3
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=3
redis.timeBetweenEvictionRunsMillis=60000
3.spring-redis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "> <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="maxRedirects" value="${redis.maxRedirects}"></property> <property name="clusterNodes"> <set> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="${redis.hostName1}"></constructor-arg> <constructor-arg name="port" value="${redis.port}"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="${redis.hostName2}"></constructor-arg> <constructor-arg name="port" value="${redis.port}"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="${redis.hostName3}"></constructor-arg> <constructor-arg name="port" value="${redis.port}"></constructor-arg> </bean> </set> </property> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg ref="redisClusterConfiguration" /> <constructor-arg ref="jedisPoolConfig" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="defaultSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> </bean> <bean id="redisDAO" class="dao.RedisDAOImpl"> <property name="redisTemplate" ref="redisTemplate" /> </bean> <bean id="serialization" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> <bean id="messageDelegateListener" class="common.MessageDelegateListenerImpl" /> <bean id="messageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter"> <property name="delegate" ref="messageDelegateListener" /> <property name="serializer" ref="serialization" /> </bean> <bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer"> <property name="connectionFactory" ref="jedisConnectionFactory"/> <property name="messageListeners"> <!-- map of listeners and their associated topics (channels or/and patterns) --> <map> <entry key-ref="messageListener"> <bean class="org.springframework.data.redis.listener.ChannelTopic"> <constructor-arg value="JAVA" /> <!-- 这里配置消费端需要订阅的频道,可以是多个。该一例子订阅JAVA这个频道 --> </bean> </entry> </map> </property> </bean> </beans>
3. 测试类
import org.junit.Before; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import common.MessageDelegateListenerImpl; public class TestRedisConsumer { private MessageDelegateListenerImpl messageDelegateListener=null; @Before public void setUp() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-redis.xml"); messageDelegateListener = (MessageDelegateListenerImpl) applicationContext.getBean("messageDelegateListener"); } public static void main(String[] args) { new ClassPathXmlApplicationContext("spring-redis.xml"); System.out.println("消费者1"); while (true) { //这里是一个死循环,目的就是让进程不退出,用于接收发布的消息 try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } }