spirng+redis 发布订阅

来源:互联网 发布:mac怎么设置桌面壁纸 编辑:程序博客网 时间:2024/05/22 04:58
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:redis="http://www.springframework.org/schema/redis"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/redishttp://www.springframework.org/schema/redis/spring-redis-1.0.xsd">   <!-- scanner redis properties  -->     <context:property-placeholder location="classpath:redis.properties" /><!-- jedis pool配置 --><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxIdle" value="${redis.maxIdle}" /><property name="maxTotal" value="${redis.maxTotal}" /><property name="maxWaitMillis" value="${redis.maxWaitMillis}" /><property name="minIdle" value="${redis.minIdle}" /><property name="testOnBorrow" value="${redis.testOnBorrow}" /></bean><!-- spring data redis --><bean id="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="usePool" value="true"></property><property name="hostName" value="${redis.host}" /><property name="port" value="${redis.port}" /><property name="password" value="${redis.pass}" /><property name="timeout" value="${redis.maxWaitMillis}" /><property name="database" value="${redis.default.db}"></property><constructor-arg index="0" ref="jedisPoolConfig" /></bean><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="jedisConnectionFactory" /><!-- 只能存入string --><property name="defaultSerializer"><beanclass="org.springframework.data.redis.serializer.StringRedisSerializer" /></property></bean>  <bean id="listener" class="com.tr.ihome.service.RedisMessageListenerService" /><redis:listener-container connection-factory="jedisConnectionFactory"><!-- the method attribute can be skipped as the default method name is "handleMessage" --><!-- topic代表监听的通道,是一个正规匹配 --><redis:listener ref="listener" method="handleMessage"topic="*" /> <!--匹配所有的--></redis:listener-container><redis:listener-container connection-factory="jedisConnectionFactory"><!-- the method attribute can be skipped as the default method name is "handleMessage" --><!-- topic代表监听的通道,是一个正规匹配 --><redis:listener ref="listener" method="handleMessagejava"topic="*java" /> <!--只会监听topic匹配Java的话题--></redis:listener-container></beans>

监听端代码

package com.tr.ihome.service;import java.io.Serializable;import java.util.Arrays;import java.util.Date;import java.util.List;import java.util.Map;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 订阅服务类 *  * @author nibili 2015年5月14日 *  */public class RedisMessageListenerService {       public void handleMessage(Serializable message) {        // 什么都不做,只输出        if (message == null) {            System.out.println("收到的消息1:"+"null");        } else if (message.getClass().isArray()) {            System.out.println("收到的消息2:"+Arrays.toString((Object[]) message));        } else if (message instanceof List<?>) {            System.out.println("收到的消息3:"+message);        } else if (message instanceof Map<?, ?>) {            System.out.println("收到的消息4:"+message);        } else {             System.out.println("收到的消息5:"+message.toString());    }    }    public void handleMessagejava(Serializable message) {        // 什么都不做,只输出        if (message == null) {            System.out.println("T收到的消息1:"+"null");        } else if (message.getClass().isArray()) {            System.out.println("T收到的消息2:"+Arrays.toString((Object[]) message));        } else if (message instanceof List<?>) {            System.out.println("T收到的消息3:"+message);        } else if (message instanceof Map<?, ?>) {            System.out.println("T收到的消息4:"+message);        } else {             System.out.println("T收到的消息5:"+message.toString());    }    }}
发送端代码

  redisTemplate.opsForValue().set("liuli", "liuli123");redisTemplate.convertAndSend("java", "java我发布的消息!");redisTemplate.convertAndSend("okjava", "okjava");redisTemplate.convertAndSend("CCCC", "CCC"); 



0 0
原创粉丝点击