配置文件

<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsdhttp://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package='com.iwhere.redis.sub'/><!-- 获取配置资源 -->
<!--     <context:property-placeholder location="classpath:redis-context-config.properties" /> --><!-- redis  START --><bean id="propertyConfigurerRedis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  <property name="order" value="1" />  <property name="ignoreUnresolvablePlaceholders" value="true" />  <property name="locations">  <list>  <value>classpath:redis-context-config.properties</value>  </list>  </property>  </bean><!-- jedis pool配置 -->  <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  <property name="maxTotal" value="${redis.maxActive}" /><property name="maxIdle" value="${redis.maxIdle}" />  <property name="maxWaitMillis" value="${redis.maxWait}" />  <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="false"></property>  <property name="hostName" value="${redis.host}" />  <property name="port" value="${redis.port}" />  <property name="password" value="${redis.pass}" />  <property name="timeout" value="${redis.timeout}" />
<!--         <property name="database" value="${redis.default.db}"></property> -->         <constructor-arg index="0" ref="jedisPoolConfig" />  </bean>  <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  <property name="connectionFactory" ref="jedisConnectionFactory"></property><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property>  </bean><!-- redis  END --><bean id="serialization" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  <bean id="messageDelegateListener" class="com.iwhere.redis.sub.MessageDelegateListenerImpl" />  <bean id="messageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">  <property name="delegate" ref="messageDelegateListener" />  <property name="serializer" ref="serialization" />  </bean>  <bean id='messageContainer' class='org.springframework.data.redis.listener.RedisMessageListenerContainer'destroy-method='destroy'><property name='connectionFactory' ref='jedisConnectionFactory' /><property name='messageListeners'><map><entry key-ref='messageListener'><list><ref bean='amap' /></list></entry></map></property></bean><!-- Channel设置 -->
<!--     <bean id='sendTopic' class='org.springframework.data.redis.listener.ChannelTopic'> -->
<!--           <constructor-arg value='send' /> -->
<!--     </bean> --><!-- Channel设置 --><bean id='amap' class='org.springframework.data.redis.listener.ChannelTopic'><constructor-arg value='amap' /></bean>
</beans>

Demo演示:

消息发布端:

package com.iwhere.testredis;import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;/*** 测试redis做消息* @author 231**/
public class TestRedis {private StringRedisTemplate redisTemplate;@Beforepublic void before() {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:redis-context.xml");redisTemplate = context.getBean(StringRedisTemplate.class);}private String channel = "amap";/*** 测试连接*/@Testpublic void test1() {String message = "c26c4ac0-37a3-4277-9020-bfa274c058f5|526548902996869120|Success";redisTemplate.convertAndSend(channel, message);System.out.println("发送完成");}
}

消息接收端

package com.iwhere.redis.sub;import java.io.UnsupportedEncodingException;import org.aspectj.bridge.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.ChannelTopic;/** * */
public class MessageDelegateListenerImpl implements MessageListener {@Autowiredprivate ChannelTopic channelTopic;@Overridepublic void onMessage(org.springframework.data.redis.connection.Message message, byte[] pattern) {byte[] bytes = message.getBody();// ""里面的参数为需要转化的编码,一般是ISO8859-1try {String str = new String(bytes, "utf-8");System.out.println("I am here" + str + ": " +  channelTopic.getTopic());} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch block
            e.printStackTrace();}System.out.println(message);}}

redis的资源文件

#redis.host=dev.iwhere.com
redis.host=192.168.1.110
redis.port=6379
redis.pass=redis密码, 没有密码就注释掉
redis.default.db=0
redis.timeout=100000
redis.maxActive=300
redis.maxIdle=100
redis.maxWait=1000
redis.testOnBorrow=true

使用redis的发布订阅模式实现消息队列相关推荐

  1. redis的发布/订阅和mq消息队列的区别,该如何选择?

    好久没写笔记了,今天记录下使用消息队列的心得. 本文以reids和rocketmq对比 很多人一直有个疑问(包括我之前也是):redis支持已经消息队列(发布/订阅)了,为什么还需要mq呢? 项目已经 ...

  2. SpringBoot整合redis实现发布订阅模式

    Redis的发布订阅模式 发布订阅(Pub/Sub):目前广泛使用的通信模型,它采用事件作为基本的通信机制,提供大规模系统所要求的松散耦合的交互模式:订阅者(如客户端)以事件订阅的方式表达出它有兴趣接 ...

  3. Redis的发布订阅模式

    本文源码参看:https://github.com/duktig666/learn-example/tree/5586febea31c2fb368e19fbdba11ed08afd463e0/Redi ...

  4. Spring Boot 使用Redis发布订阅模式处理消息

    Spring Boot 使用Redis发布订阅模式 1. Redis发布订阅模式 2. Spring Boot中订阅消息 2.1 Redis监听器容器配置 2.2 创建通道监听器 2.3 测试订阅功能 ...

  5. Redis的发布订阅模式以及在SpringBoot中的使用

    1.基本介绍 Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. Redis 客户端可以订阅任意数量的频道. publish(发布命令) A ...

  6. Kafka的点对点模式、发布订阅模式、基础架构

    一.定义 Kafka 是一个分布式的基于发布/订阅模式的消息队列(Message Queue),主要应用于 大数据实时处理领域. 二.消息队列 使用消息队列的好处 1)解耦 允许你独立的扩展或修改两边 ...

  7. springboot使用redis实现消息队列功能,redis使用list和stream实现消息队列功能,redis实现消息队列的风险点分析

    文章目录 写在前面 基于list的消息队列解决方案 使用list基本实现消息队列 阻塞式消费,避免性能损失 替换while(true) 实现消息幂等 保证消息可靠性 基于stream的消息队列解决方案 ...

  8. Redis 笔记(10)— 发布订阅模式(发布订阅单个信道、订阅信道后的返回值分类、发布订阅多个信道)

    1. 发布-订阅概念 发布-订阅 模式包含两种角色,分别为发布者和订阅者. 订阅者可以订阅一个或者若干个频道(channel): 而发布者可以向指定的频道发送消息,所有订阅此频道的订阅者都可以收到此消 ...

  9. Redis的订阅发布功能对比RabbitMQ消息队列

    1.对比 特性 redis RabbitMQ 可靠性 没有相应的机制保证消息的可靠消费,如果发布者发布一条消息,而没有对应的订阅者的话,这条消息将丢失,不会存在内存中 具有消息消费确认机制,如果发布一 ...

最新文章

  1. java中JVM的原理【转】
  2. 关于使用TestNG的retry问题
  3. 162. Leetcode 45. 跳跃游戏 II (贪心算法-贪心区间)
  4. Web-IM前端解决方案
  5. centos linux 禁止ping
  6. 想要成为数据科学家?知道这11种机器学习算法吗?
  7. l和l_L&T的完整形式是什么?
  8. Scanner对象接收数据进行分类处理 java
  9. Linux内核无法放到Ubuntu中,无法通过Linux内核模块(Ubuntu)在内核内存中写入
  10. Linux上的ffmpeg完全使用指南
  11. 【WCF】错误处理(二):错误码―—FaultCode
  12. Matlab中值滤波去噪
  13. 各代DDR内存的速度表
  14. 周志华 《机器学习》之 第十二章(计算学习理论)概念总结
  15. 基于Qt的局域网即时通讯软件
  16. 【认知计算】IBM报告解读《认知中国》— 拉近人工智能未来与现实的距离,中国企业争当认知创新者
  17. 导航信号测试用什么软件,专业GPS测试软件 VisualGPSXP入门
  18. 斑马zpl指令二维码换行
  19. 在vue中使用鼠标事件@mousedown、@mouseenter等失效的解决办法,以及PC端长按实现
  20. word如何设置奇数页页脚在右下角,偶数页在左下角

热门文章

  1. 关于中英数字混排的字符串分割问题(转)
  2. update,options
  3. 互联网业界7月三件大事
  4. linux编译安装madam,linux 下 使用 mdadm 创建阵列
  5. python实现合并链表_python:16.合并两个排序的链表
  6. equals和hashCode
  7. elastic-job配置类
  8. MyBatis 集成到Spring 的原理是什么?
  9. ClassPathScanningCandidateComponentProvider 扫描给定包及其子包的类
  10. SpringBoot另一大神器-Actuator