RabbitMqConfig.java 1.7 KB
package com.topdraw.mq.config;


import com.topdraw.config.LocalConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {

    @Value("${engine.platform}")
    private String platform;

    @Value("${engine.type}")
    private String type;

    @Value("${engine.mq.exchange}")
    private String exchange;

    @Value("${engine.mq.routingkey}")
    private String routingKey;

    public String getExchange(){
        if (StringUtils.isEmpty(this.exchange)) {
            this.routingKey = "uc.direct";
        }

        return this.exchange;
    }

    public String getRoutingKey() {
        if (StringUtils.isEmpty(this.routingKey)) {

            if (platform.equalsIgnoreCase(LocalConstants.PLATFORM_TYPE_SERVICE)) {

                if (StringUtils.isEmpty(this.type)) {
                    this.type = LocalConstants.ENV_VIS;
                }

            }

            this.routingKey = "uc."+platform+"."+type+".direct";
        }

        return routingKey;
    }

    @Bean
    DirectExchange directExchange(){
        return ExchangeBuilder.directExchange(getExchange()).build();
    }

    @Bean
    Queue queue(){ return new Queue(getRoutingKey()); }

    @Bean
    Binding binding(DirectExchange directExchange , Queue queue) {
        BindingBuilder.DirectExchangeRoutingKeyConfigurer directExchangeRoutingKeyConfigurer =
                BindingBuilder.bind(queue).to(directExchange);
        return directExchangeRoutingKeyConfigurer.with(getRoutingKey());
    }

}