redis配置:

  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. <context:property-placeholder location="classpath:redis.properties" />
  13. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  14. <property name="maxIdle" value="${redis.maxIdle}" />
  15. <property name="maxTotal" value="${redis.maxTotal}" />
  16. <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
  17. <property name="testOnBorrow" value="${redis.testOnBorrow}" />
  18. </bean>
  19. <bean id="jedisConnectionFactory"
  20. class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  21. destroy-method="destroy">
  22. <property name="poolConfig" ref="jedisPoolConfig"></property>
  23. <property name="hostName" value="${redis.host}"></property>
  24. <property name="port" value="${redis.port}"></property>
  25. <property name="password" value="${redis.pass}"></property>
  26. </bean>
  27. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  28. <property name="connectionFactory" ref="jedisConnectionFactory"></property>
  29. <property name="defaultSerializer">
  30. <bean
  31. class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  32. </property>
  33. </bean>
  34. <bean id="registerMessageListener" class="com.gc.biz.cache.listener.RegisterMessageListener">
  35. <property name="redisTemplate" ref="redisTemplate"></property>
  36. </bean>
  37. <bean id="priDocMessageListener" class="com.gc.biz.cache.listener.PriDocRegActMsgListener">
  38. <property name="redisTemplate" ref="redisTemplate"></property>
  39. </bean>
  40. <bean id="redisDAO" class="com.gc.biz.cache.impl.MessageDaoImpl">
  41. <property name="redisTemplate" ref="redisTemplate" />
  42. </bean>
  43. <bean id="topicContainer"
  44. class="org.springframework.data.redis.listener.RedisMessageListenerContainer"
  45. destroy-method="destroy">
  46. <property name="connectionFactory" ref="jedisConnectionFactory" />
  47. <property name="taskExecutor">
  48. <bean
  49. class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
  50. <property name="poolSize" value="3"></property>
  51. </bean>
  52. </property>
  53. <property name="messageListeners">
  54. <map>
  55. <entry key-ref="registerMessageListener">
  56. <bean class="org.springframework.data.redis.listener.ChannelTopic">
  57. <constructor-arg value="coupon|redenvelop|notify|points" />
  58. </bean>
  59. </entry>
  60. <entry key-ref="priDocMessageListener">
  61. <bean class="org.springframework.data.redis.listener.ChannelTopic">
  62. <constructor-arg value="YZM|BG" />
  63. </bean>
  64. </entry>
  65. </map>
  66. </property>
  67. </bean>
  68. <bean id="springContext" class="com.gc.biz.cache.util.SpringContextHolder" />
  69. <bean id="doctorDAO" class="com.gc.biz.cache.impl.DoctorDAOImpl" >
  70. <property name="redisTemplate" ref="redisTemplate" />
  71. </bean>
  72. <bean id="remindDAO" class="com.gc.biz.cache.impl.RemindDAOImpl" />
  73. <bean id="userDAO" class="com.gc.biz.cache.impl.UserDAOImpl" />
  74. <bean id="userDataDAO" class="com.gc.biz.cache.impl.UserDataDAOImpl" />
  75. </beans>

监听器的实现:

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

消息发送接口的实现:

  1. package com.gc.biz.cache.impl;
  2. import java.io.Serializable;
  3. import org.springframework.data.redis.core.RedisTemplate;
  4. import com.gc.biz.cache.dao.MessageDao;
  5. public class MessageDaoImpl implements MessageDao{
  6. private RedisTemplate<String , Object> redisTemplate = null;
  7. public MessageDaoImpl() {
  8. }
  9. @Override
  10. public void sendMessage(String channel, Serializable message) {
  11. redisTemplate.convertAndSend(channel, message);
  12. }
  13. public RedisTemplate<String, Object> getRedisTemplate() {
  14. return redisTemplate;
  15. }
  16. public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
  17. this.redisTemplate = redisTemplate;
  18. }
  19. }

测试调用的方法:

  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. map.put("UserBagID", "1");
  9. map.put("CreateDate", "2016-06-01 16:51:35");
  10. dao.sendMessage("iphone|xiaomi", new Gson().toJson(map));

注意:1、如果有多个项目同时使用此配置,只需要保留一个项目配置文件有关注项目;2、此配置没有考虑分布式部署的环境,如果要考虑从redis list和分布式锁的方向考虑。

基于spring-redis发布订阅模式的实现的更多相关文章

  1. 基于Spring的发布订阅模式 EventListener

    基于Spring的发布订阅模式 在我们使用spring开发应用时,经常会碰到要去解耦合一些依赖调用,比如我们在做代码的发布流程中,需要去通知相关的测试,开发人员关注发布中的错误信息.而且通知这个操作又 ...

  2. SpringBoot Redis 发布订阅模式 Pub/Sub

    SpringBoot Redis 发布订阅模式 Pub/Sub 注意:redis的发布订阅模式不可以将消息进行持久化,订阅者发生网络断开.宕机等可能导致错过消息. Redis命令行下使用发布订阅 pu ...

  3. 使用EventBus + Redis发布订阅模式提升业务执行性能

    前言 最近一直奔波于面试,面了几家公司的研发.有让我受益颇多的面试经验,也有让我感觉浪费时间的面试经历~因为疫情原因,最近宅在家里也没事,就想着使用Redis配合事件总线去实现下具体的业务. 需求 一 ...

  4. redis发布/订阅模式

    其实在很多的MQ产品中都存在这样的一个模式,我们常听到的一个例子 就是邮件订阅的场景,什么意思呢,也就是说100个人订阅了你的博客,如果博主发表了文章,那么100个人就会同时收到通知邮件,除了这个 场 ...

  5. Redis - 发布/订阅模式

    Redis 提供了一组命令可以让开发者实现 “发布/订阅” 模式.“发布/订阅” 可以实现进程间的消息传递,其原理是这样的: “发布/订阅” 模式中包含两种角色,分别是发布者和订阅者.订阅者可以订阅一 ...

  6. redis 发布/订阅 模式

    发布/订阅模式的命令如下: * 进入发布订阅模式的客户端,不能执行除发布订阅模式以上命令的其他命令,否则出错.

  7. 使用EventBus + Redis发布订阅模式提升业务执行性能(下)

    前言 上一篇博客上已经实现了使用EventBus对具体事件行为的分发处理,某种程度上也算是基于事件驱动思想编程了.但是如上篇博客结尾处一样,我们源码的执行效率依然达不到心里预期.在下单流程里我们明显可 ...

  8. 把酒言欢话聊天,基于Vue3.0+Tornado6.1+Redis发布订阅(pubsub)模式打造异步非阻塞(aioredis)实时(websocket)通信聊天系统

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_202 "表达欲"是人类成长史上的强大"源动力",恩格斯早就直截了当地指出,处在蒙昧时代即低 ...

  9. Spring源码之七registerListeners()及发布订阅模式

    Spring源码之七registerListeners()及发布订阅模式 大家好,我是程序员田同学. 今天带大家解读refresh()方法中的registerListeners()方法,也就是我们经常 ...

  10. Redis 发布订阅,小功能大用处,真没那么废材!

    今天小黑哥来跟大家介绍一下 Redis 发布/订阅功能. 也许有的小伙伴对这个功能比较陌生,不太清楚这个功能是干什么的,没关系小黑哥先来举个例子. 假设我们有这么一个业务场景,在网站下单支付以后,需要 ...

随机推荐

  1. 对OCR文字识别软件的扫描选项怎么设置

    说到OCR文字识别软件,越来越多的人选择使用ABBYY FineReader识别和转换文档,然而并不是每个人都知道转换质量取决于源图像的质量和所选的扫描选项,今天就给大家普及一下这方面的知识. ABB ...

  2. ORA-12170: TNS: 连接超时

    ORA-12170: TNS: 连接超时 如果在本机可以正常使用,可是到局域网中的其他机器就出现“ORA-12170:TNS:连接超时 解决方法: 1.cmd-----ping ip地址 查看网络问题 ...

  3. JavaScript 的字符串转换

    数字.布尔值等其他数据类型都可以转换成字符串:一般来说,脚本引擎将根据上下文自动完成这样的转换.例如,当把数字或布尔型变量传给希望接收字符串变量的函数时,就会先隐式地将该数值转换成字符串,再进行处理: ...

  4. linux概念之/dev/shm

    Linux默认(CentOS)/dev/shm分区的大小是系统物理内存的50%, 虽说使用/dev/shm对文件操作的效率会高很多,但是目前各发行软件中却很少有使用它的(除了前面提到的Oracle), ...

  5. html初学(三)

    <!-- 我就是我,不一样的烟花 piu piu piu 干啥子 如来神掌 -- --- ----- .======. ***********啊啊啊啊啊啊 | INRI | | | | | .= ...

  6. Restfull API 示例

    什么是Restfull API Restfull API 从字面就可以知道,他是rest式的接口,所以就要先了解什么是rest rest 不是一个技术,也不是一个协议 rest 指的是一组架构约束条件 ...

  7. java生成随机字符串uuid

    GUID是一个128位长的数字,一般用16进制表示.算法的核心思想是结合机器的网卡.当地时间.一个随即数来生成GUID.从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义 ...

  8. Photoshop CS3 如何汉化

    1. 下载汉化包 http://download.csdn.net/detail/yangtian1158/8740959 2. 将下载的.bat文件放到CS3的安装目录里即可 C:\Program ...

  9. Android手机平板两不误,使用Fragment实现兼容手机和平板的程序

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744943 记得我之前参与开发过一个华为的项目,要求程序可以支持好几种终端设备,其 ...

  10. php ord和chr函数

    直接上代码 //通过ord()函数获取字符的ASCII码值,如果返回值大于 127则表示为中文字符的一半,再获取后一半组合成一个完整字符 $string = "hello不要迷恋哥world ...