使用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: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的发布订阅模式实现消息队列的更多相关文章
- redis的发布订阅模式
概要 redis的每个server实例都维护着一个保存服务器状态的redisServer结构 struct redisServer { /* Pubsub */ // 字典,键为频道, ...
- 13、Redis的发布订阅模式
写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------------------- ...
- redis的发布订阅模式pubsub
前言 redis支持发布订阅模式,在这个实现中,发送者(发送信息的客户端)不是将信息直接发送给特定的接收者(接收信息的客户端),而是将信息发送给频道(channel),然后由频道将信息转发给所有对这个 ...
- Springboot+Redis(发布订阅模式)跨多服务器实战
一:redis中发布订阅功能(http://www.redis.cn/commands.html#pubsub) PSUBSCRIBE pattern [pattern -]:订阅一个或者多个符合pa ...
- 【转】Redis之发布 订阅模式
本例包括 jedis_demo:入口类 jedis_control:jedis控制器(jedis的连接池) jedis_pub_sub_listener:订阅的监听器 singleton_agent: ...
- Spring Boot中使用redis的发布/订阅模式
原文:https://www.cnblogs.com/meetzy/p/7986956.html redis不仅是一个非常强大的非关系型数据库,它同时还拥有消息中间件的pub/sub功能,在sprin ...
- 15天玩转redis —— 第九篇 发布/订阅模式
本系列已经过半了,这一篇我们来看看redis好玩的发布订阅模式,其实在很多的MQ产品中都存在这样的一个模式,我们常听到的一个例子 就是邮件订阅的场景,什么意思呢,也就是说100个人订阅了你的博客,如果 ...
- redis发布/订阅模式
其实在很多的MQ产品中都存在这样的一个模式,我们常听到的一个例子 就是邮件订阅的场景,什么意思呢,也就是说100个人订阅了你的博客,如果博主发表了文章,那么100个人就会同时收到通知邮件,除了这个 场 ...
- 使用EventBus + Redis发布订阅模式提升业务执行性能
前言 最近一直奔波于面试,面了几家公司的研发.有让我受益颇多的面试经验,也有让我感觉浪费时间的面试经历~因为疫情原因,最近宅在家里也没事,就想着使用Redis配合事件总线去实现下具体的业务. 需求 一 ...
随机推荐
- maven镜像
使用maven管理项目,下载依赖jar包的时候,经常会下载很慢,但是如果使用镜像的话,速度超级快~~只要在.m2/setting.xml文件中设置镜像就可以啦~ 本文来自https://yq.aliy ...
- [label][politic-video]李锡锟的政治学视频下载链接
李锡锟政治学 1.http://r15---sn-p5qlsn7y.googlevideo.com/videoplayback?initcwndbps=1471000&signature=09 ...
- Beyond Compare脚本:命令行批量比较文件并生成html格式的差异报告
BComp.exe /silent /closescript /solo @E:\compareTest\BCbatch.txt text-report layout:side-by-side opt ...
- Moving XML/BI Publisher Components Between Instances
As it is well known fact that XMLPublisher stores the metadata and physical files for templates and ...
- C++互斥器:Mutex
互斥器的功能是,使多个线程和谐工作.同一时间内,只能有一个线程得到互斥对象,并获得资源操作权限,那么如果同一时间其他线程也想去操作资源,此时就会因为Mutex未处于激发状态,而无奈的等待…这时候,线程 ...
- Type conversions in C++类型转换
###Implicit conversions隐式转换* 可以在基本类型之间自由转换:* 可以把任何类型的pointer转换为void pointer:* 可以将子类pointer转换为基类point ...
- Team Foundation Server 开发流程管理管理研讨会
这周,和微软公司的朋友一起,受北京某金融企业邀请,为企业软件部门一个70多人的软件团队提供了一场基于Team Foundation Server的软件软件流程的技术研讨会.在研讨会中,培训基于微软Te ...
- linux系统编程之错误处理:perror,strerror和errno
1,在系统编程中错误通常通过函数返回值来表示,并通过特殊变量errno来描述. errno这个全局变量在<errno.h>头文件中声明如下:extern int errno; errno是 ...
- 使用C#创建windows服务续之使用Topshelf优化Windows服务
前言: 之前写了一篇“使用C#创建windows服务”,https://www.cnblogs.com/huangwei1992/p/9693167.html,然后有博友给我推荐了一个开源框架Tops ...
- wpf控件拖动
Thumb 拖动 上代码! <Window x:Class="Thumb控件移动.MainWindow" xmlns="http://schemas.microso ...