600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 使用redis的发布订阅模式实现消息队列

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

时间:2018-12-23 07:23:40

相关推荐

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

配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance" xmlns:aop="/schema/aop"xmlns:tx="/schema/tx" xmlns:util="/schema/util"xmlns:context="/schema/context" xmlns:mongo="/schema/data/mongo"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.0.xsd/schema/aop /schema/aop/spring-aop-3.0.xsd/schema/tx /schema/tx/spring-tx-3.0.xsd/schema/util /schema/util/spring-util-3.0.xsd/schema/data/mongo /schema/data/mongo/spring-mongo-1.0.xsd/schema/context /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=redis.host=192.168.1.110redis.port=6379redis.pass=redis密码, 没有密码就注释掉redis.default.db=0redis.timeout=100000redis.maxActive=300redis.maxIdle=100redis.maxWait=1000redis.testOnBorrow=true

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。