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

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 {

    public static final String UCE_EXCHANGE = "uce.exchange";
    public static final String UCE_QUEUE = "uce.queue";

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

    @Value("${service.mq.queue}")
    private String queue;

    public String getExchange(){
        if (StringUtils.isEmpty(this.exchange)) {
            this.exchange = UCE_EXCHANGE;
        }

        return this.exchange;
    }

    public String getQueue() {
        if (StringUtils.isEmpty(this.queue)) {
            this.queue = UCE_QUEUE;
        }

        return this.queue;
    }

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

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

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

}