接上一篇spring4.2+SpringDataRedis消息队列+Junit4 - Go语言中文社区

接上一篇spring4.2+SpringDataRedis消息队列+Junit4


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中的数据有一些乱码前缀,不知为何物


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/sdfiiiiii/article/details/54744400
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-28 01:20:37
  • 阅读 ( 1232 )
  • 分类:Redis

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢