日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/4/13 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用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.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設置 --> <!-- <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=dev.iwhere.com redis.host=192.168.1.110 redis.port=6379 redis.pass=redis密碼, 沒有密碼就注釋掉 redis.default.db=0 redis.timeout=100000 redis.maxActive=300 redis.maxIdle=100 redis.maxWait=1000 redis.testOnBorrow=true

?

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的使用redis的发布订阅模式实现消息队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。