使用redis的发布订阅模式实现消息队列
生活随笔
收集整理的這篇文章主要介紹了
使用redis的发布订阅模式实现消息队列
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
配置文件
<?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設(shè)置 --> <!-- <bean id='sendTopic' class='org.springframework.data.redis.listener.ChannelTopic'> --> <!-- <constructor-arg value='send' /> --> <!-- </bean> --><!-- Channel設(shè)置 --><bean id='amap' class='org.springframework.data.redis.listener.ChannelTopic'><constructor-arg value='amap' /></bean> </beans>?
Demo演示:
消息發(fā)布端:?
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;/*** 測(cè)試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";/*** 測(cè)試連接*/@Testpublic void test1() {String message = "c26c4ac0-37a3-4277-9020-bfa274c058f5|526548902996869120|Success";redisTemplate.convertAndSend(channel, message);System.out.println("發(fā)送完成");} }消息接收端
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();// ""里面的參數(shù)為需要轉(zhuǎn)化的編碼,一般是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密碼, 沒(méi)有密碼就注釋掉 redis.default.db=0 redis.timeout=100000 redis.maxActive=300 redis.maxIdle=100 redis.maxWait=1000 redis.testOnBorrow=true?
超強(qiáng)干貨來(lái)襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的使用redis的发布订阅模式实现消息队列的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux下查看CPU型号,内存大小,硬
- 下一篇: mysql5.7.14多实例安装