美文网首页
spring配置MQ自动创建队列和exchange

spring配置MQ自动创建队列和exchange

作者: cjlynn | 来源:发表于2021-02-04 14:41 被阅读0次

Rabbit MQ加上@Configuration配置,在mq上没有队列和exhange的时候,自动创建队列和exchange


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Configuration
public class MQReceiver {

    private static final String EXCHANGE_ADMIN_NOTIFY = "adminNotify2";

    private static final String QUEURE_ADMIN_MESSAGE = "admin-message2";

    @Bean
    public Queue queueMessage() {
        return new Queue(QUEURE_ADMIN_MESSAGE, false);
    }

    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(EXCHANGE_ADMIN_NOTIFY, false, false);
    }

    @Bean
    public Binding bindingExchangeMessage() {
        return BindingBuilder.bind(queueMessage()).to(exchange()).with("");
    }

    @Autowired
    private NotifyService notifyService;

    @RabbitHandler
    @RabbitListener(queues = QUEURE_ADMIN_MESSAGE)
    public void process(Message msg) {
        try {
            log.debug("===>>> MQ listen message {} ", msg.toString());
        } catch (Exception e) {
            log.error("MQReceiver ====> ", e);
        }
    }

}

相关文章

网友评论

      本文标题:spring配置MQ自动创建队列和exchange

      本文链接:https://www.haomeiwen.com/subject/mxybtltx.html