您的位置:首页 > 数据库 > Redis

Redis 消息队列运用

2016-01-06 10:48 477 查看
1、本实例采用的是Spring-data-Redis体系

网上有使用redis做消息队列,采用发布订阅模式,同样如此,这里不同的是,我配置的监听有多个,对多个主题(Topic)进行监听。

客户端调用

StringRedisTemplate template = SpringRedisConfig.getApplicationContextInstance().getBean(StringRedisTemplate.class);

template.convertAndSend(AsyncService.smsService.toString(), “helloWorld”);

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Configuration
public class SpringRedisConfig {
    public static final String REDIS_PORT = "redis.port";
    public static final String REDIS_SERVER = "redis.server";
    public static final String REDIS_PASSWD = "redis.passwd";
    public static final String REDIS_DB = "redis.db";
    public static final ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringRedisConfig.class);

    public static ApplicationContext getApplicationContextInstance(){
        return  ctx;
    }
    @Bean
    public RedisConnectionFactory redisConnectionFactory()  throws SystemInternalException {
        Map<String, String> bundleCfg = BundleConfigManager.getBundleCfg();
        JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
        redisConnectionFactory.setHostName(ConfigUtil.get(bundleCfg, REDIS_SERVER));
        if (ConfigUtil.existKey(bundleCfg, REDIS_PASSWD)){
            redisConnectionFactory.setPassword(ConfigUtil.get(bundleCfg, REDIS_PASSWD));
        }
        redisConnectionFactory.setPort(ConfigUtil.getInt(bundleCfg, REDIS_PORT));
        redisConnectionFactory.setTimeout(15000);
        redisConnectionFactory.setDatabase(ConfigUtil.getInt(bundleCfg, REDIS_DB));
        redisConnectionFactory.setUsePool(true);
        return redisConnectionFactory;
    }
    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }
    
    @Bean
    public SMSReceiver receiver() {
        return new SMSReceiver();
    }
    @Bean
    public MessageListenerAdapter listenerAdapter(SMSReceiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveSMSMessage");
    }
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        Map< MessageListener, Collection<? extends Topic>> listener = new HashMap<MessageListener, Collection<? extends Topic>>();
        listener.put(listenerAdapter,Collections.singleton(new PatternTopic(AsyncService.smsService.toString())));
        ActivityReceiver activityReceiver = new ActivityReceiver();
        listener.put(activityReceiver, Collections.singleton(new PatternTopic(AsyncService.activityService.toString())));
        container.setMessageListeners(listener);
       //container.addMessageListener(listenerAdapter, new PatternTopic("smsSender"));
        return container;
    }


2、 RedisMessageListenerContainer 容器是对消息监听进行管理,Spring 已经对Bean进行注入,MessageListenerAdapter 可以看到已经注入了,MessageListenerAdapter其实它也是实现了 InitializingBean, MessageListener 两个接口,第一个InitializingBean用于spring 容器初始化(依赖于Spring容器),MessageListener第二个接口就是消息监听接口,其实我们是可以自己去实现这个接口的,上述的ActivityReceiver
就是如此,自己实现的MessageListener。

3、问题:

3.1 对Spring-data-redis 不是很熟,我其实自己是不想采用spring体系的,太复杂,后续考虑能不能直接使用Jedis体系。

3.2 MessageListenerAdapter 因为做了适配器所以 监听方法可以自定义,比如说上面的 receiveSMSMessage方法,如果能够适配多个进去就好了。不用RedisMessageListenerContainer去做多个监听器

3.3 RedisMessageListenerContainer 容器在实践的过程中 放入多个MessageListenerAdapter 似乎在测试的时候第二个MessageListenerAdapter 不生效,所以我才采用的

new ActivityReceiver()的方式。

3.4 客户端发送可以不仅支持字符串,同时也支持对象吗?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: