配置文件

<?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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://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="" />
<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="" 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;
@Before
public void before() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:redis-context.xml");
redisTemplate = context.getBean(StringRedisTemplate.class);
} private String channel = "amap";
/**
* 测试连接
*/
@Test
public 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 { @Autowired
private ChannelTopic channelTopic; @Override
public void onMessage(org.springframework.data.redis.connection.Message message, byte[] pattern) { byte[] bytes = message.getBody();// ""里面的参数为需要转化的编码,一般是ISO8859-1
try {
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=
redis.pass=redis密码, 没有密码就注释掉
redis.default.db=
redis.timeout=
redis.maxActive=
redis.maxIdle=
redis.maxWait=
redis.testOnBorrow=true

使用redis的发布订阅模式实现消息队列的更多相关文章

  1. redis的发布订阅模式

    概要 redis的每个server实例都维护着一个保存服务器状态的redisServer结构 struct redisServer {     /* Pubsub */     // 字典,键为频道, ...

  2. 13、Redis的发布订阅模式

     写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------------------- ...

  3. redis的发布订阅模式pubsub

    前言 redis支持发布订阅模式,在这个实现中,发送者(发送信息的客户端)不是将信息直接发送给特定的接收者(接收信息的客户端),而是将信息发送给频道(channel),然后由频道将信息转发给所有对这个 ...

  4. Springboot+Redis(发布订阅模式)跨多服务器实战

    一:redis中发布订阅功能(http://www.redis.cn/commands.html#pubsub) PSUBSCRIBE pattern [pattern -]:订阅一个或者多个符合pa ...

  5. 【转】Redis之发布 订阅模式

    本例包括 jedis_demo:入口类 jedis_control:jedis控制器(jedis的连接池) jedis_pub_sub_listener:订阅的监听器 singleton_agent: ...

  6. Spring Boot中使用redis的发布/订阅模式

    原文:https://www.cnblogs.com/meetzy/p/7986956.html redis不仅是一个非常强大的非关系型数据库,它同时还拥有消息中间件的pub/sub功能,在sprin ...

  7. 15天玩转redis —— 第九篇 发布/订阅模式

    本系列已经过半了,这一篇我们来看看redis好玩的发布订阅模式,其实在很多的MQ产品中都存在这样的一个模式,我们常听到的一个例子 就是邮件订阅的场景,什么意思呢,也就是说100个人订阅了你的博客,如果 ...

  8. redis发布/订阅模式

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

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

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

随机推荐

  1. hdu 5882 Balanced Game 2016-09-21 21:22 80人阅读 评论(0) 收藏

    Balanced Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. Spark-1.2.2部署

    1.安装Scala 1.1解压和安装 在Scala官网http://www.scala-lang.org/download/下载Scala安装包,然后解压.(注:JDK的版本最好是1.7及以上,否则S ...

  3. hdu 5032 不易发觉的树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=5032 给定一个1000x1000的点阵,m组询问,每次询问一个由(0,0).(x,0)点一以及从原点出发的方向向 ...

  4. Android ADB命令基本常用操作

    电脑配置好环境变量之后呢,在cmd里面自测一下,是否配置OK: 1.查看目前连接的设备: adb devices 2.使目前连接的设备重启: adb reboot 3.有时候由于设备冲突导致adb出现 ...

  5. ASP.NET 中 <%= %> 与 <%: %> 的区别

    做个备忘 <%= %> 内容原封不动输出 <%: %> 对内容进行编码后输出 即:<%: str %> 等价于 <%= Html.Encode(str) %& ...

  6. Team Foundation Server 开发流程管理管理研讨会

    这周,和微软公司的朋友一起,受北京某金融企业邀请,为企业软件部门一个70多人的软件团队提供了一场基于Team Foundation Server的软件软件流程的技术研讨会.在研讨会中,培训基于微软Te ...

  7. 设计模式之外观模式(Facade Pattern)

    一.什么是外观模式? 简单的说,外观模式是用来简化接口的. 通常,我们觉得一个子系统不好用,可能是因为它提供的外部接口太接近低层组件,让我们用起来感到很麻烦. 因为我们不需要知道内部细节,我们只想要一 ...

  8. C#实现在图片上动态写内容

    之前在项目上遇到这么一个需求,就是要在图片上写内容,而且要求是动态,我所谓的动态就是在图片上写的内容是动态的.网上找了找,很多人实现了网图片上写内容的功能,但是,并没有实现动态.所以在这里把我的解决办 ...

  9. NET 下载共享文件

    执行 public static void Run() { "); if (state) { // 共享文件夹的目录 TransportRemoteToLocal(@"\\192. ...

  10. 使用vue.js常见错误之一

    打包项目时,在vscode中输入如下命令 webpack .\src\main.js .\dist\bundle.js 出现如下错误: WARNING in configurationThe 'mod ...