配置文件

<?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. opengl中场景变换|2D与3D互转换(转)

    opengl中场景变换|2D与3D互转换 我们生活在一个三维的世界——如果要观察一个物体,我们可以: 1.从不同的位置去观察它.(视图变换) 2.移动或者旋转它,当然了,如果它只是计算机里面的物体,我 ...

  2. application cache 应用缓存

    这些应用还是要自己实现一遍,否则真不知道哪里会出问题. 客户端: <!DOCTYPE html> <html manifest = 'demo.appcache'> <h ...

  3. jquery ui导入两次的错误提示

    如果jquery ui plugin的js文件出现到两次的话,就会出现报错. 解决办法: 找出引用了jquery ui 的文件,将其中一个去掉就ok了. 在Firefox下面的报错提示: TypeEr ...

  4. ASP.NET MVC 4 中Razor 视图中JS无法调试

    解决方法 1.首先检查IE中这2个属性是否勾选了. 2.选择IE浏览器进行调试,调试方法有2种     A:采用debugger;的方法,如下图所示: 这时不用调试断点就会在debugger位置中命中 ...

  5. Git Commit 标准化

    1 前言Git Commit Message 应该清晰明了,要用精简的语言说明本次提交的目的,其主要作用是为了后续的搜索.版本的回滚.合并冲突的追溯等操作. 我们在开发时一直以来对 Git Commi ...

  6. 浏览器拦截跨域请求处理方法(同源策略不允许读取XXX上的远程资源)

    直接了当了说,解决此类问题,最直接的方法就是,就是给被请求的服务器,添加HTTP头响应头,这里提供两种添加HTTP头的方法: 第一种,就是在程序中添加HTTP头: Response.AddHeader ...

  7. mysql之使用centos7实现主从复制(读写分离)的实现过程

    什么是主从复制? 主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库:主数据库一般是准实时的业务数据库. 主从复制的作用(好处)! 1.做数据的热备,作为后备数据库,主数据库服务器故 ...

  8. .net core 基于Jwt实现Token令牌

    Startup类ConfigureServices中 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJw ...

  9. Keil下Debug随笔

    很多时候我们需要通过硬件仿真来调试程序,在仿真时有时候会遇到这样的情况,那就是选择全速运行时,我们的全局变量无法随时更新,而在那设一个断点后发现值是变化的,那么为什么会出现这种情况呢,那就是可能是我们 ...

  10. C# 泛型实现Table与实体的相互转换

    public class ModelHandler<T> where T : new() { /// <summary> /// Table转换成实体 /// </sum ...