一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式。利用redis这两种场景的消息队列都能够实现。

定义:
生产者消费者模式:生产者生产消息放到队列里,多个消费者同时监听队列,谁先抢到消息谁就会从队列中取走消息;即对于每个消息只能被最多一个消费者拥有。
发布者订阅者模式:发布者生产消息放到队列里,多个监听队列的消费者都会收到同一份消息;即正常情况下每个消费者收到的消息应该都是一样的。

下面就以Spring Data Redis实现简单的消息“发布/订阅”服务。

spring-redis使用RedisMessageListenerContainer进行消息监听,客户程序需要自己实现MessageListener,并以指定的topic注册到RedisMessageListenerContainer,这样,在指定的topic上如果有消息,RedisMessageListenerContainer便会通知该MessageListener。

下面是在spring配置文件中配置spring-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 引入jedis配置文件 -->
<context:property-placeholder location="classpath*:resource/redis.properties" /> <context:component-scan base-package="com.ljq.durian" /> <!-- jedis pool配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testOnReturn" value="${redis.testOnReturn}" />
</bean> <!-- spring data redis -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="poolConfig" ref="jedisPoolConfig"></property>
<property name="timeout" value="${redis.timeout}"></property>
<property name="usePool" value="true"></property>
</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="redisMessageListener" class="com.ljq.durian.common.listener.RedisMessageListener">
<property name="redisTemplate" ref="redisTemplate"/>
</bean> <bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="messageListeners">
<map>
<entry key-ref="redisMessageListener">
<list>
<!-- 普通订阅,订阅具体的频道 -->
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="topic.channel" />
</bean>
<!-- 模式订阅,支持模式匹配订阅,*为模糊匹配符 -->
<bean class="org.springframework.data.redis.listener.PatternTopic">
<constructor-arg value="topic.*" />
</bean>
<!-- 匹配所有频道 -->
<bean class="org.springframework.data.redis.listener.PatternTopic">
<constructor-arg value="*" />
</bean>
</list>
</entry>
</map>
</property>
</bean> </beans>

上面例子中,最后三个bean的配置是实现发布/订阅服务的关键,RedisMessageListener是自己写的实现了org.springframework.data.redis.connection.MessageListener的业务类,并以“topic.channel” 这个topic注册到RedisMessageListenerContainer。RedisMessageListenerContainer在消息到达后负责通知MessageListener。
下面是RedisMessageListener的代码:

package com.ljq.durian.common.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer; public class RedisMessageListener implements MessageListener {
@Autowired
private RedisTemplate<String, String> redisTemplate; @Override
public void onMessage(Message message, byte[] pattern) {
RedisSerializer<?> serializer = redisTemplate.getValueSerializer();
Object channel = serializer.deserialize(message.getChannel());
Object body = serializer.deserialize(message.getBody());
System.out.println("主题: " + channel);
System.out.println("消息内容: " + String.valueOf(body));
} public RedisTemplate<String, String> getRedisTemplate() {
return redisTemplate;
} public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}
}

这样,应用启动时,消息的订阅方(subscriber)就注册好了。这时候只要使用一个简单的程序,模拟publisher,向指定topic发布消息,RedisMessageListener就可以接收到消息,spring-redis的写法是这样:

redisTemplate.convertAndSend("topic.channel", "hello world!");

Spring Data Redis实现消息队列——发布/订阅模式的更多相关文章

  1. redis实现消息队列&发布/订阅模式使用

    在项目中用到了redis作为缓存,再学习了ActiveMq之后想着用redis实现简单的消息队列,下面做记录.   Redis的列表类型键可以用来实现队列,并且支持阻塞式读取,可以很容易的实现一个高性 ...

  2. 【转】redis 消息队列发布订阅模式spring boot实现

    最近做项目的时候写到一个事件推送的场景.之前的实现方式是起job一直查询数据库,看看有没有最新的消息.这种方式非常的不优雅,反正我是不能忍,由于羡慕本身就依赖redis,刚好redis 也有消息队列的 ...

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

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

  4. Spring Boot使用Redis进行消息的发布订阅

    今天来学习如何利用Spring Data对Redis的支持来实现消息的发布订阅机制.发布订阅是一种典型的异步通信模型,可以让消息的发布者和订阅者充分解耦.在我们的例子中,我们将使用StringRedi ...

  5. redis消息通知(任务队列/优先级队列/发布订阅模式)

    1.任务队列 对于发送邮件或者是复杂计算这样的操作,常常需要比较长的时间,为了不影响web应用的正常使用,避免页面显示被阻塞,常常会将此类任务存入任务队列交由专门的进程去处理. 队列最基础的方法如下: ...

  6. rabbitmq消息队列——"发布订阅"

    三."发布订阅" 上一节的练习中我们创建了一个工作队列.队列中的每条消息都会被发送至一个工作进程.这节,我们将做些完全不同的事情--我们将发送单个消息发送至多个消费者.这种模式就是 ...

  7. Redis实现消息的发布/订阅

    利用spring-boot结合redis进行消息的发布与订阅: 发布: class Publish { private static String topicName = “Topic:chat”; ...

  8. redis之mq实现发布订阅模式

    示例代码-github 概述 Redis不仅可作为缓存服务器,还可用作消息队列,本示例演示如何使用redis实现发布/订阅消息队列. 在Redis中,发布者没有将消息发送给特定订阅者的程序.相反,发布 ...

  9. Redis进阶篇:发布订阅模式原理与运用

    "65 哥,如果你交了个漂亮小姐姐做女朋友,你会通过什么方式将这个消息广而告之给你的微信好友?" "那不得拍点女朋友的美照 + 亲密照弄一个九宫格图文消息在朋友圈发布大肆 ...

随机推荐

  1. Linux 两组信号对比(关闭和停止进程信号)

    之前看信号的时候,没有太注意不同信号的对比.今天再次看到的时候,突然感觉对一些信号,非常相似,乃至非常容易混淆.今天周末就抽空总结一下. 一.关闭进程信号 常见的4中关闭进程信号是SIGKILL,SI ...

  2. Android下利用zbar类库实现扫一扫

    程序源代码及可执行文件下载地址:http://files.cnblogs.com/rainboy2010/zbardemo.zip Android下常用的条码扫描类库有zxing和zbar,比较了一下 ...

  3. 将文字数字转阿拉伯数字(Python)

    今天改进爬虫的时候有这样的需求,如果是文字的数字把他转化成数字存储. 然后百度了一下没什么能看的博客. 其实挺无奈的,搜出来的博客全是一样的代码,有的图都是COPY,尤指CSDN,博客质量大多很差.怀 ...

  4. 自定义Dialog的详细步骤(实现自定义样式一般原理)

    现在很多App的提示对话框都非常有个性,然而你还用系统的对话框样式,是不是觉得很落后呢,今天我就给大家讲讲怎样自定义自己的Dialog,学会了之后,你就会根据自家app的主题,设计出相应的Dialog ...

  5. 快速开发android,离不开这10个优秀的开源项目

    作为一名菜鸡Android,时常瞻仰大佬们的开源项目是非常必要的.这里我为大家收集整理了10个优秀的开源项目,方便我们日常开发中学习! 作者:ListenToCode博客:https://www.ji ...

  6. Confluence 6 配置服务器基础地址

    服务器基础地址(Server Base URL)是用户访问 Confluence 的 URL 地址.这个基础的 URL 地址必须与你在浏览器中访问 Confluence 中的地址. Confluenc ...

  7. Confluence 6 新 Confluence 安装配置一个数据源连接

    如果在你的 Tomcat 中配置了数据源,并且Confluence 设置指南在安装的时候检测到这个配置的时候,配置数据源的选项将会提供给你进行配置.入股你希望使用数据源,请参考下面的配置. 1. 停止 ...

  8. Confluence 6 数据库整合的方法 1:基本流程

    步骤 1:对你的插件进行记录 对你近期在 Confluence 中安装和启用的插件进行记录,这你可以在后期对插件进行重新安装或者调整.针对你安装的插件,你需要记录下面的一些内容: 插件名称 版本号 启 ...

  9. conda 安装多个版本的python

    再添加一个python3.6conda create --name py36 python=3.6然后通过source activate py36来激活环境

  10. Java的输入语句以及本周对于文件读写的研究

    日期:2018.9.20 博客期:010 星期四 ##:今天下午要考试 java(小考)!那么,我就应对相应的方法给出策略吧! 首先是 Java 里的输入语句,我一般是用Scanner类,用这个之前要 ...