package com.jianshui.queue.config;

import com.jianshui.queue.consumer.RedisQueueConsumer;
import com.jianshui.queue.container.RedisQueueConsumerContainer;
import com.jianshui.queue.factory.RedisQueueConsumerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;
import java.util.Set;

/**
 * @Description redis生产/消费模式队列
 * @Author 巩权林
 * @Date 2021-11-25 9:48
 **/

@Configuration
public class RedisQueueConfig {

    @Autowired
    private RedisQueueConsumerFactory consumerFactory;

    protected final Logger log = LoggerFactory.getLogger(RedisQueueConfig.class);

    // 初始化完毕后调取 init
    @Bean(initMethod = "init", destroyMethod = "destroy")
    public RedisQueueConsumerContainer redisQueueConsumerContainer() {
        log.info("【redis队列】redis队列开始加载");
        RedisQueueConsumerContainer redisQueueConsumerContainer = new RedisQueueConsumerContainer();

        // 添加消费者 OrderConsumer 到消费者容器
        Map<String, RedisQueueConsumer> consumerMap = consumerFactory.getConsumerMap();
        Set<String> consumerNames = consumerMap.keySet();
        log.info("【redis队列】开始加载消费者，共发现消费者{}个", consumerNames.size());
        for (String consumerName : consumerNames) {
            RedisQueueConsumer consumer = consumerMap.get(consumerName);
            redisQueueConsumerContainer.addConsumer(consumer);
            log.info("【redis队列】加载消费者{}成功", consumerName);
        }
        // redisQueueConsumerContainer.addConsumer(new CheckConsumer());

        log.info("【redis队列】redis队列开始加载成功");
        return redisQueueConsumerContainer;
    }
}
