package com.ruoyi.dataprocess.config;

import com.ruoyi.dataprocess.listener.RedisMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
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.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import java.util.Arrays;
import java.util.List;

@Configuration
public class RedisMessageListenerConfig {

    @Autowired
    private RedisMessageListener redisMessageListener;
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * 配置订阅关系
     */
    @Bean
    public RedisMessageListenerContainer container() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);

        //订阅频道
        List<PatternTopic> topicList = Arrays.asList(new PatternTopic("life.*"),new PatternTopic("*.life"));
        container.addMessageListener(redisMessageListener, topicList);

        return container;
    }

}

