程序啟動 rabbitMq創建交換機,隊列綁定交換機,以及設置綁定規則


package application.config.rabbit;

import application.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 交換機配置類
 *
 * @author
 * @date 2021/1/18/0018 15:17
 */
@Configuration
@Slf4j
public class RabbitMqConfig {

   

    @Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }

    //*******************用戶【刷臉】,企業相關交換機
//bean的名稱自定義約束好即可,
//
MqConfigConstants.EXCHANGE_ACCOUNT 常量的名字自定義也可

@Bean("accountExchange") public TopicExchange accountExchange() { return new TopicExchange(MqConfigConstants.EXCHANGE_ACCOUNT, true, false); } @Bean("companyExchange") public TopicExchange companyExchange() { return new TopicExchange(MqConfigConstants.EXCHANGE_COMPANY, true, false); } //*************************[刷臉]用戶、企業隊列 @Bean public Queue faceQueue() { //隊列持久 return new Queue(MqConfigConstants.FACE_STATUS_CHANGE_KEY_PRE_PASS, true); } @Bean public Queue accountQueue() { return new Queue(MqConfigConstants.ACCOUNT_STATUS_CHANGE_KEY_PRE, true); } @Bean public Queue companyQueue() { return new Queue(MqConfigConstants.COMPANY_STATUS_CHANGE_KEY_PRE_PASS, true); } //****************************綁定 // 刷臉[綁定規則可以具體到某一個值 如 face.status.0這種綁定的為0的數據,如果想模糊綁定 face.status.* 或者face.status.# #匹配的是當前和子類]
// 用戶 。企業
@Bean public Binding bindingFacePass() { return BindingBuilder.bind(faceQueue()). to(accountExchange()) .with("綁定規則"); } @Bean public Binding bindingFaceFail() { return BindingBuilder.bind(faceQueue()). to(accountExchange()) .with("綁定規則" ); } @Bean public Binding bindingAccountPass() { return BindingBuilder.bind(accountQueue()). to(accountExchange()) .with("綁定規則"); } @Bean public Binding bindingAccountFail() { return BindingBuilder.bind(accountQueue()). to(accountExchange()) .with("綁定規則"); } @Bean public Binding bindingCompanyPass() { return BindingBuilder.bind(companyQueue()). to(companyExchange()) .with("綁定規則"); } @Bean public Binding bindingCompanyFail() { return BindingBuilder.bind(companyQueue()). to(companyExchange()) .with("綁定規則"); } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM