社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
springRedis.xml,增加了Spring Redis Channel配置
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 池化 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="10" /> <property name="maxIdle" value="3" /> <property name="maxWaitMillis" value="1000" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="redis1.tuniu-dev.org" /> <property name="port" value="6379" /> <property name="poolConfig" ref="jedisPoolConfig" /> <property name="usePool" value="true" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> <!--<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />--> </property> </bean> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/> <!-- Spring Redis Channel --> <bean id="myMessageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter"> <constructor-arg> <bean class="com.tuniu.test.RedisMessageListener"/> </constructor-arg> </bean> <bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer"> <property name="connectionFactory" ref="jedisConnectionFactory"/> <property name="messageListeners"> <map> <entry key-ref="myMessageListener"> <bean class="org.springframework.data.redis.listener.ChannelTopic"> <constructor-arg value="hello-channel2" /> </bean> </entry> </map> </property> </bean> </beans>
RedisMessageListener
package com.tuniu.test; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import java.io.UnsupportedEncodingException; public class RedisMessageListener implements MessageListener { @Override public void onMessage(Message message, byte[] pattern) { try { /*System.out.println("channel:" + new String(message.getChannel()) + ",message:" + new String(message.getBody(), "utf-8").substring(7));*/ System.out.println("channel:" + new String(message.getChannel()) + ",message:" + new String(message.getBody(), "utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
TestJedis.java测试类
package com.tuniu.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.io.Serializable; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ /*"classpath:conf-spring/spring-dao.xml",*/ "classpath:springRedis.xml" }) public class TestJedis { @Autowired private RedisTemplate<String, Object> template; @Test public void sample(){ /** * opsForValue */ //this.testOpsForValue(); /** * 测试使用redis发送和接送消息 */ this.testPublishAndReceiveMessage(); } public void testOpsForValue() { /** * 测试使用opsForValue */ RedisObject redisObject = new RedisObject(); redisObject.setObjectType(Integer.class); template.opsForValue().set("redisObject", redisObject); redisObject = (RedisObject) template.opsForValue().get("redisObject"); System.out.println(redisObject.getObjectType().toString()); } public void testPublishAndReceiveMessage(){ template.convertAndSend("hello-channel2", "我是中文"); } } /** * 该类需要可序列化 */ class RedisObject implements Serializable { private Class objectType; public Class getObjectType() { return objectType; } public void setObjectType(Class objectType) { this.objectType = objectType; } }
不明白为何,获取到channel中的数据有一些乱码前缀,不知为何物
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!