配置文件
<?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="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; @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=6379 redis.pass=redis密码, 没有密码就注释掉 redis.default.db=0 redis.timeout=100000 redis.maxActive=300 redis.maxIdle=100 redis.maxWait=1000 redis.testOnBorrow=true
时间: 2024-11-07 12:50:21