package com.cyl.tencent;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
import java.util.Map;
 
@Slf4j
@Component
public class QueueConsumerAware implements ApplicationContextAware {
 
    private static Map<String, QueueConsumer> queueConsumerMap;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        queueConsumerMap = applicationContext.getBeansOfType(QueueConsumer.class);
        for (Map.Entry<String, QueueConsumer> entry : queueConsumerMap.entrySet()) {
            log.info("启动消费者：" + entry.getKey());
            entry.getValue().start();
        }
    }
 
    @PreDestroy
    public void destroy() {
        if (!MapUtils.isEmpty(queueConsumerMap)) {
            for (Map.Entry<String, QueueConsumer> entry : queueConsumerMap.entrySet()) {
                log.info("销毁消费者：" + entry.getKey());
                entry.getValue().stop();
            }
        }
    }
}