All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lodsve.amqp.annotations.AmqpBeanDefinitionRegistrar Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2018  Sun.Hao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package lodsve.amqp.annotations;

import lodsve.amqp.binding.DirectQueueBinding;
import lodsve.amqp.binding.FanoutQueueBinding;
import lodsve.amqp.binding.TopicQueueBinding;
import lodsve.amqp.configs.ExchangeType;
import lodsve.amqp.configs.QueueConfig;
import lodsve.amqp.configs.RabbitProperties;
import lodsve.core.bean.BeanRegisterUtils;
import lodsve.core.properties.relaxedbind.RelaxedBindFactory;
import org.apache.commons.collections.MapUtils;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.HashMap;
import java.util.Map;

/**
 * 注册amqp的相关queue配置.
 *
 * @author sunhao([email protected])
 * @date 2018-08-02 14:32
 */
public class AmqpBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    private RabbitProperties rabbitProperties;
    private final Map beanDefinitionMap = new HashMap<>(16);

    public AmqpBeanDefinitionRegistrar() {
        rabbitProperties = new RelaxedBindFactory.Builder<>(RabbitProperties.class).build();
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        Map queues = rabbitProperties.getQueues();

        if(MapUtils.isEmpty(queues)) {
            return;
        }

        for (String queueName : queues.keySet()) {
            QueueConfig config = queues.get(queueName);

            // 1. 创建queue
            createQueue(queueName, config.isDurable(), config.isExclusive(), config.isAutoDelete());
            // 2. 创建binding
            createBinding(queueName, config);
        }

        BeanRegisterUtils.registerBeans(beanDefinitionMap, registry);
    }

    private void createBinding(String queueName, QueueConfig config) {
        ExchangeType type = config.getExchangeType();
        Class bindingRawClass;
        Class exchangeRawClass;
        switch (type) {
            case TOPIC:
                bindingRawClass = TopicQueueBinding.class;
                exchangeRawClass = TopicExchange.class;
                break;
            case DIRECT:
                bindingRawClass = DirectQueueBinding.class;
                exchangeRawClass = DirectExchange.class;
                break;
            case FANOUT:
                bindingRawClass = FanoutQueueBinding.class;
                exchangeRawClass = FanoutExchange.class;
                break;
            case HEADERS:
                throw new RuntimeException("Lodsve-Framework not support HeadersExchange now!");
            default:
                bindingRawClass = null;
                exchangeRawClass = null;
        }

        BeanDefinitionBuilder binding = BeanDefinitionBuilder.genericBeanDefinition(bindingRawClass);
        binding.addConstructorArgReference(config.getExchangeName());
        binding.addConstructorArgReference(queueName);
        if (ExchangeType.FANOUT != type) {
            binding.addConstructorArgValue(config.getRoutingKey());
        }

        beanDefinitionMap.put(config.getExchangeName() + "_queue_binding", binding.getBeanDefinition());

        // 3. 创建exchange
        if (!beanDefinitionMap.containsKey(config.getExchangeName())) {
            BeanDefinitionBuilder exchange = BeanDefinitionBuilder.genericBeanDefinition(exchangeRawClass);
            exchange.addConstructorArgValue(config.getExchangeName());
            beanDefinitionMap.put(config.getExchangeName(), exchange.getBeanDefinition());
        }

    }

    private void createQueue(String queueName, boolean durable, boolean exclusive, boolean autoDelete) {
        BeanDefinitionBuilder queue = BeanDefinitionBuilder.genericBeanDefinition(Queue.class);
        queue.addConstructorArgValue(queueName);
        queue.addConstructorArgValue(durable);
        queue.addConstructorArgValue(exclusive);
        queue.addConstructorArgValue(autoDelete);

        beanDefinitionMap.put(queueName, queue.getBeanDefinition());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy