package com.webchat.config.configuration;

import com.webchat.common.enums.RedisMessageChannelTopicEnum;
import com.webchat.service.listener.RedisApplyMessageListener;
import com.webchat.service.listener.RedisChatMessageListener;
import com.webchat.service.listener.RedisPushMessageListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import javax.annotation.Resource;

@Configuration
public class RedisConfig {

    @Resource
    private RedisChatMessageListener chatMessageListener;
    @Resource
    private RedisPushMessageListener pushMessageListener;
    @Resource
    private RedisApplyMessageListener redisApplyMessageListener;

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        // 设置连接工厂
        container.setConnectionFactory(redisConnectionFactory);
        // 配置消息监听器
        container.addMessageListener(chatMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.CHAT.getChannel()));
        container.addMessageListener(pushMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.PUSH_ARTICLE.getChannel()));
        container.addMessageListener(redisApplyMessageListener, new ChannelTopic(RedisMessageChannelTopicEnum.APPLY.getChannel()));
        return container;
    }

}
