基于spring-redis发布订阅模式的实现
redis配置:
- <?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:redis="http://www.springframework.org/schema/redis" xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/redis
- http://www.springframework.org/schema/redis/spring-redis-1.0.xsd"
- default-autowire="byName">
- <context:property-placeholder location="classpath:redis.properties" />
- <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="testOnBorrow" value="${redis.testOnBorrow}" />
- </bean>
- <bean id="jedisConnectionFactory"
- class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- destroy-method="destroy">
- <property name="poolConfig" ref="jedisPoolConfig"></property>
- <property name="hostName" value="${redis.host}"></property>
- <property name="port" value="${redis.port}"></property>
- <property name="password" value="${redis.pass}"></property>
- </bean>
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="jedisConnectionFactory"></property>
- <property name="defaultSerializer">
- <bean
- class="org.springframework.data.redis.serializer.StringRedisSerializer" />
- </property>
- </bean>
- <bean id="registerMessageListener" class="com.gc.biz.cache.listener.RegisterMessageListener">
- <property name="redisTemplate" ref="redisTemplate"></property>
- </bean>
- <bean id="priDocMessageListener" class="com.gc.biz.cache.listener.PriDocRegActMsgListener">
- <property name="redisTemplate" ref="redisTemplate"></property>
- </bean>
- <bean id="redisDAO" class="com.gc.biz.cache.impl.MessageDaoImpl">
- <property name="redisTemplate" ref="redisTemplate" />
- </bean>
- <bean id="topicContainer"
- class="org.springframework.data.redis.listener.RedisMessageListenerContainer"
- destroy-method="destroy">
- <property name="connectionFactory" ref="jedisConnectionFactory" />
- <property name="taskExecutor">
- <bean
- class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
- <property name="poolSize" value="3"></property>
- </bean>
- </property>
- <property name="messageListeners">
- <map>
- <entry key-ref="registerMessageListener">
- <bean class="org.springframework.data.redis.listener.ChannelTopic">
- <constructor-arg value="coupon|redenvelop|notify|points" />
- </bean>
- </entry>
- <entry key-ref="priDocMessageListener">
- <bean class="org.springframework.data.redis.listener.ChannelTopic">
- <constructor-arg value="YZM|BG" />
- </bean>
- </entry>
- </map>
- </property>
- </bean>
- <bean id="springContext" class="com.gc.biz.cache.util.SpringContextHolder" />
- <bean id="doctorDAO" class="com.gc.biz.cache.impl.DoctorDAOImpl" >
- <property name="redisTemplate" ref="redisTemplate" />
- </bean>
- <bean id="remindDAO" class="com.gc.biz.cache.impl.RemindDAOImpl" />
- <bean id="userDAO" class="com.gc.biz.cache.impl.UserDAOImpl" />
- <bean id="userDataDAO" class="com.gc.biz.cache.impl.UserDataDAOImpl" />
- </beans>
监听器的实现:
- package com.gc.biz.cache.listener;
- import java.io.Serializable;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.log4j.Logger;
- import org.springframework.data.redis.connection.Message;
- import org.springframework.data.redis.connection.MessageListener;
- import org.springframework.data.redis.core.RedisTemplate;
- import com.gc.apps.jsk.coupon.service.CouponService;
- import com.gc.apps.jsk.coupon.service.impl.CouponServiceImpl;
- import com.gc.apps.jsk.invitationcode.service.InvitationService;
- import com.gc.apps.jsk.invitationcode.service.impl.InvitationServiceImpl;
- import com.gc.apps.jsk.login.service.RegisterService;
- import com.gc.apps.jsk.login.service.impl.RegisterServiceImpl;
- import com.gc.apps.jsk.membership.service.MemberShipService;
- import com.gc.apps.jsk.membership.service.impl.MemberShipServiceImpl;
- import com.gc.biz.member.dbobj.MemberInfo;
- import com.gc.common.util.StrUtil;
- import com.gc.frame.core.db.DBTransaction;
- import com.gc.frame.core.misc.StringUtil;
- import com.google.gson.Gson;
- public class RegisterMessageListener implements MessageListener {
- private RedisTemplate<Serializable, Serializable> redisTemplate;
- private static Logger logger = Logger.getLogger(RegisterMessageListener.class);
- public void setRedisTemplate(RedisTemplate<Serializable, Serializable> redisTemplate) {
- this.redisTemplate = redisTemplate;
- }
- @Override
- public void onMessage(Message message, byte[] pattern) {
- byte[] body = message.getBody();// 请使用valueSerializer
- byte[] channel = message.getChannel();
- // 请参考配置文件,本例中key,value的序列化方式均为string。
- // 其中key必须为stringSerializer。和redisTemplate.convertAndSend对应
- String msgContent = (String) redisTemplate.getValueSerializer().deserialize(body);
- String topic = (String) redisTemplate.getStringSerializer().deserialize(channel);
- System.out.println(topic + ":" + msgContent);
- Map<String, String> map = new Gson().fromJson(msgContent, Map.class);
- String from = map.get("from");
- if ("wx".equals(from)) {
- doRegisterMsg_wx(topic, msgContent);
- } else if ("app".equals(from)) {
- doRegisterMsg(topic, msgContent);
- }
- }
消息发送接口的实现:
- package com.gc.biz.cache.impl;
- import java.io.Serializable;
- import org.springframework.data.redis.core.RedisTemplate;
- import com.gc.biz.cache.dao.MessageDao;
- public class MessageDaoImpl implements MessageDao{
- private RedisTemplate<String , Object> redisTemplate = null;
- public MessageDaoImpl() {
- }
- @Override
- public void sendMessage(String channel, Serializable message) {
- redisTemplate.convertAndSend(channel, message);
- }
- public RedisTemplate<String, Object> getRedisTemplate() {
- return redisTemplate;
- }
- public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
- this.redisTemplate = redisTemplate;
- }
- }
测试调用的方法:
- MessageDao dao = SpringContextHolder.getBean("redisDAO");
- Map<String,String> map = new HashMap<String,String>();
- map.put("1", "11111");
- map.put("2", "22222");
- dao.sendMessage("coupon", new Gson().toJson(map));
- dao.sendMessage("redenvelop", new Gson().toJson(map));
- dao.sendMessage("notify", new Gson().toJson(map));
- map.put("UserBagID", "1");
- map.put("CreateDate", "2016-06-01 16:51:35");
- dao.sendMessage("iphone|xiaomi", new Gson().toJson(map));
注意:1、如果有多个项目同时使用此配置,只需要保留一个项目配置文件有关注项目;2、此配置没有考虑分布式部署的环境,如果要考虑从redis list和分布式锁的方向考虑。
基于spring-redis发布订阅模式的实现的更多相关文章
- 基于Spring的发布订阅模式 EventListener
		基于Spring的发布订阅模式 在我们使用spring开发应用时,经常会碰到要去解耦合一些依赖调用,比如我们在做代码的发布流程中,需要去通知相关的测试,开发人员关注发布中的错误信息.而且通知这个操作又 ... 
- SpringBoot Redis 发布订阅模式 Pub/Sub
		SpringBoot Redis 发布订阅模式 Pub/Sub 注意:redis的发布订阅模式不可以将消息进行持久化,订阅者发生网络断开.宕机等可能导致错过消息. Redis命令行下使用发布订阅 pu ... 
- 使用EventBus + Redis发布订阅模式提升业务执行性能
		前言 最近一直奔波于面试,面了几家公司的研发.有让我受益颇多的面试经验,也有让我感觉浪费时间的面试经历~因为疫情原因,最近宅在家里也没事,就想着使用Redis配合事件总线去实现下具体的业务. 需求 一 ... 
- redis发布/订阅模式
		其实在很多的MQ产品中都存在这样的一个模式,我们常听到的一个例子 就是邮件订阅的场景,什么意思呢,也就是说100个人订阅了你的博客,如果博主发表了文章,那么100个人就会同时收到通知邮件,除了这个 场 ... 
- Redis - 发布/订阅模式
		Redis 提供了一组命令可以让开发者实现 “发布/订阅” 模式.“发布/订阅” 可以实现进程间的消息传递,其原理是这样的: “发布/订阅” 模式中包含两种角色,分别是发布者和订阅者.订阅者可以订阅一 ... 
- redis 发布/订阅 模式
		发布/订阅模式的命令如下: * 进入发布订阅模式的客户端,不能执行除发布订阅模式以上命令的其他命令,否则出错. 
- 使用EventBus + Redis发布订阅模式提升业务执行性能(下)
		前言 上一篇博客上已经实现了使用EventBus对具体事件行为的分发处理,某种程度上也算是基于事件驱动思想编程了.但是如上篇博客结尾处一样,我们源码的执行效率依然达不到心里预期.在下单流程里我们明显可 ... 
- 把酒言欢话聊天,基于Vue3.0+Tornado6.1+Redis发布订阅(pubsub)模式打造异步非阻塞(aioredis)实时(websocket)通信聊天系统
		原文转载自「刘悦的技术博客」https://v3u.cn/a_id_202 "表达欲"是人类成长史上的强大"源动力",恩格斯早就直截了当地指出,处在蒙昧时代即低 ... 
- Spring源码之七registerListeners()及发布订阅模式
		Spring源码之七registerListeners()及发布订阅模式 大家好,我是程序员田同学. 今天带大家解读refresh()方法中的registerListeners()方法,也就是我们经常 ... 
- Redis 发布订阅,小功能大用处,真没那么废材!
		今天小黑哥来跟大家介绍一下 Redis 发布/订阅功能. 也许有的小伙伴对这个功能比较陌生,不太清楚这个功能是干什么的,没关系小黑哥先来举个例子. 假设我们有这么一个业务场景,在网站下单支付以后,需要 ... 
随机推荐
- 自定的 HttpModule 为什么总是执行两次
			其实可以看一下Html页面就知道了. 其实它又请求了一次.要关了这个或者忽略之. 
- webpack 多entry 配置
			// webpack 多entry 配置var path = require('path'); module.exports = { entry: { entry2: './entry.js', de ... 
- disruptor - Concurrent Programming Framework 并发编程框架
			disruptor发布了Java的2.0版本(.Net版本见这里),disruptor是一个高性能的异步处理框架,或者可以认为是最快的消息框架(轻量的JMS),也可以认为是一个观察者模式实现,或者事件 ... 
- 斐波那契(Fibonacci)数列的几种计算机解法
			题目:斐波那契数列,又称黄金分割数列(F(n+1)/F(n)的极限是1:1.618,即黄金分割率),指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.…….在数学上,斐波纳契数列以如下 ... 
- 移动设备上的媒体查询 CSS media queries for mobile device
			CSS的媒体查询虽然在传统的互联网页面可能发挥的余地不是很大,但是自从苹果和安卓的风靡之后,移动平台上的web开发变得越来越流行了,同时CSS的媒体查询可谓派上了大用场了. 以下为翻译内容,原文来自这 ... 
- [转]何为C10K问题
			我在学习网络编程的时候经常看到C10K问题,那么究竟什么是C10K问题呢?我看到了一篇好文章就转了过来,原文地址为:c10k问题 所谓c10k问题,指的是服务器同时支持成千上万个客户端的问题,也就是c ... 
- js cookie操作
			//写Cookie function writeCookie(name, value) { var expire = new Date(); expire.setFullYear(expire.get ... 
- 06 Linux下Shell介绍
			一.概述 每个人在成功登陆Linux后,系统会出现不同的提示符号,例如$,~,#等,然后你就可以开始输入需要的命令.若命令正确,系统就会依据命令的要求来执行,直到注销系统为止,在登陆到注销期间,输入的 ... 
- 关于mysql数据库在输入密码后,滴的一声直接退出界面的解决办法
			转自:http://www.2cto.com/database/201412/361751.html 网上搜索到的解决办法: 1.找到mysql安装目录下的bin目录路径.2.打开cmd,进入到bin ... 
- 236. Lowest Common Ancestor of a Binary Tree
			Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ... 
