基于spring-redis发布订阅模式的实现

redis配置: 
Java代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:redis="http://www.springframework.org/schema/redis" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.        http://www.springframework.org/schema/context  
  8.        http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  9.        http://www.springframework.org/schema/redis  
  10.         http://www.springframework.org/schema/redis/spring-redis-1.0.xsd"  
  11.     default-autowire="byName">  
  12.   
  13.     <context:property-placeholder location="classpath:redis.properties" />  
  14.   
  15.     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  16.         <property name="maxIdle" value="${redis.maxIdle}" />  
  17.         <property name="maxTotal" value="${redis.maxTotal}" />  
  18.         <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />  
  19.         <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
  20.     </bean>  
  21.     <bean id="jedisConnectionFactory"  
  22.         class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
  23.         destroy-method="destroy">  
  24.         <property name="poolConfig" ref="jedisPoolConfig"></property>  
  25.         <property name="hostName" value="${redis.host}"></property>  
  26.         <property name="port" value="${redis.port}"></property>  
  27.         <property name="password" value="${redis.pass}"></property>  
  28.     </bean>  
  29.   
  30.     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  31.         <property name="connectionFactory" ref="jedisConnectionFactory"></property>  
  32.         <property name="defaultSerializer">  
  33.             <bean  
  34.                 class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  35.         </property>  
  36.     </bean>  
  37.   
  38.     <bean id="registerMessageListener" class="com.gc.biz.cache.listener.RegisterMessageListener">  
  39.         <property name="redisTemplate" ref="redisTemplate"></property>  
  40.     </bean>  
  41.       
  42.     <bean id="priDocMessageListener" class="com.gc.biz.cache.listener.PriDocRegActMsgListener">  
  43.         <property name="redisTemplate" ref="redisTemplate"></property>  
  44.     </bean>  
  45.       
  46.       
  47.     <bean id="redisDAO" class="com.gc.biz.cache.impl.MessageDaoImpl">  
  48.         <property name="redisTemplate" ref="redisTemplate" />  
  49.     </bean>  
  50.       
  51.     <bean id="topicContainer"  
  52.         class="org.springframework.data.redis.listener.RedisMessageListenerContainer"  
  53.         destroy-method="destroy">  
  54.         <property name="connectionFactory" ref="jedisConnectionFactory" />  
  55.         <property name="taskExecutor">  
  56.             <bean  
  57.                 class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">  
  58.                 <property name="poolSize" value="3"></property>  
  59.             </bean>  
  60.         </property>  
  61.         <property name="messageListeners">  
  62.             <map>  
  63.                 <entry key-ref="registerMessageListener">  
  64.                     <bean class="org.springframework.data.redis.listener.ChannelTopic">  
  65.                         <constructor-arg value="coupon|redenvelop|notify|points" />  
  66.                     </bean>  
  67.                 </entry>  
  68.                   
  69.                 <entry key-ref="priDocMessageListener">  
  70.                     <bean class="org.springframework.data.redis.listener.ChannelTopic">  
  71.                         <constructor-arg value="YZM|BG" />  
  72.                     </bean>  
  73.                 </entry>  
  74.                   
  75.             </map>  
  76.         </property>  
  77.     </bean>  
  78.   
  79.   
  80.     <bean id="springContext" class="com.gc.biz.cache.util.SpringContextHolder" />  
  81.     <bean id="doctorDAO" class="com.gc.biz.cache.impl.DoctorDAOImpl" >  
  82.         <property name="redisTemplate" ref="redisTemplate" />  
  83.     </bean>  
  84.     <bean id="remindDAO" class="com.gc.biz.cache.impl.RemindDAOImpl" />  
  85.     <bean id="userDAO" class="com.gc.biz.cache.impl.UserDAOImpl" />  
  86.     <bean id="userDataDAO" class="com.gc.biz.cache.impl.UserDataDAOImpl" />  
  87.   
  88. </beans>    


监听器的实现: 
Java代码 
  1. package com.gc.biz.cache.listener;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import org.apache.log4j.Logger;  
  8. import org.springframework.data.redis.connection.Message;  
  9. import org.springframework.data.redis.connection.MessageListener;  
  10. import org.springframework.data.redis.core.RedisTemplate;  
  11.   
  12. import com.gc.apps.jsk.coupon.service.CouponService;  
  13. import com.gc.apps.jsk.coupon.service.impl.CouponServiceImpl;  
  14. import com.gc.apps.jsk.invitationcode.service.InvitationService;  
  15. import com.gc.apps.jsk.invitationcode.service.impl.InvitationServiceImpl;  
  16. import com.gc.apps.jsk.login.service.RegisterService;  
  17. import com.gc.apps.jsk.login.service.impl.RegisterServiceImpl;  
  18. import com.gc.apps.jsk.membership.service.MemberShipService;  
  19. import com.gc.apps.jsk.membership.service.impl.MemberShipServiceImpl;  
  20. import com.gc.biz.member.dbobj.MemberInfo;  
  21. import com.gc.common.util.StrUtil;  
  22. import com.gc.frame.core.db.DBTransaction;  
  23. import com.gc.frame.core.misc.StringUtil;  
  24. import com.google.gson.Gson;  
  25.   
  26. public class RegisterMessageListener implements MessageListener {  
  27.   
  28.     private RedisTemplate<Serializable, Serializable> redisTemplate;  
  29.   
  30.     private static Logger logger = Logger.getLogger(RegisterMessageListener.class);  
  31.   
  32.     public void setRedisTemplate(RedisTemplate<Serializable, Serializable> redisTemplate) {  
  33.         this.redisTemplate = redisTemplate;  
  34.     }  
  35.   
  36.     @Override  
  37.     public void onMessage(Message message, byte[] pattern) {  
  38.         byte[] body = message.getBody();// 请使用valueSerializer  
  39.         byte[] channel = message.getChannel();  
  40.         // 请参考配置文件,本例中key,value的序列化方式均为string。  
  41.         // 其中key必须为stringSerializer。和redisTemplate.convertAndSend对应  
  42.         String msgContent = (String) redisTemplate.getValueSerializer().deserialize(body);  
  43.         String topic = (String) redisTemplate.getStringSerializer().deserialize(channel);  
  44.         System.out.println(topic + ":" + msgContent);  
  45.         Map<String, String> map = new Gson().fromJson(msgContent, Map.class);  
  46.         String from = map.get("from");  
  47.         if ("wx".equals(from)) {  
  48.             doRegisterMsg_wx(topic, msgContent);  
  49.         } else if ("app".equals(from)) {  
  50.             doRegisterMsg(topic, msgContent);  
  51.         }  
  52.   
  53.     }  
  54.   
  55.      


消息发送接口的实现: 
Java代码 
  1. package com.gc.biz.cache.impl;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. import org.springframework.data.redis.core.RedisTemplate;  
  6.   
  7. import com.gc.biz.cache.dao.MessageDao;  
  8.   
  9. public class MessageDaoImpl implements MessageDao{  
  10.       
  11.     private RedisTemplate<String , Object> redisTemplate = null;  
  12.       
  13.     public MessageDaoImpl() {  
  14.   
  15.     }  
  16.       
  17.     @Override  
  18.     public void sendMessage(String channel, Serializable message) {  
  19.         redisTemplate.convertAndSend(channel, message);  
  20.     }  
  21.   
  22.     public RedisTemplate<String, Object> getRedisTemplate() {  
  23.         return redisTemplate;  
  24.     }  
  25.   
  26.     public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {  
  27.         this.redisTemplate = redisTemplate;  
  28.     }  
  29.       
  30.       
  31. }  


测试调用的方法: 
Java代码 
  1. MessageDao dao = SpringContextHolder.getBean("redisDAO");  
  2. Map<String,String> map = new HashMap<String,String>();  
  3. map.put("1""11111");  
  4. map.put("2""22222");  
  5. dao.sendMessage("coupon"new Gson().toJson(map));  
  6. dao.sendMessage("redenvelop"new Gson().toJson(map));  
  7. dao.sendMessage("notify"new Gson().toJson(map));  
  8.   
  9. map.put("UserBagID""1");  
  10. map.put("CreateDate""2016-06-01 16:51:35");  
  11. dao.sendMessage("iphone|xiaomi"new Gson().toJson(map));  


注意:1、如果有多个项目同时使用此配置,只需要保留一个项目配置文件有关注项目;2、此配置没有考虑分布式部署的环境,如果要考虑从redis list和分布式锁的方向考虑。
posted on 2016-09-24 00:04  _故乡的原风景  阅读(222)  评论(0编辑  收藏  举报