当前位置: 首页 > news >正文

RabbitMQ:SpringAMQP 声明队列和交换机

目录

  • 一、基本概述
  • 二、使用Bean声明
    • 2.1 广播交换机
    • 2.2 定向交换机
    • 2.3 主题交换机
  • 三、使用注解声明
    • 3.1 广播交换机
    • 3.2 定向交换机
    • 3.4 主题交换机


生产者源码
消费者源码

一、基本概述

SpringAMQP提供了及各类,用来声明队列、交换机及其绑定关系:

  • Queue:用于声明队列,可以用工厂类QueueBuilder构建。
  • Exchange:用于声明交换机,可以用工厂类ExchangeBuilder构建。
  • Binding:用于声明队列和交换机的绑定关系,可以用工厂类BindingBuilder构建。

注意:一般情况下,队列和交换机都在消费者服务中注册。

二、使用Bean声明

2.1 广播交换机

package com.ming.config;import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 广播交换机配置*/
@Configuration
public class FanoutConfiguration {/*** 创建广播交换机* name: 交换机的名称* isDurable: 是否持久化 默认true* @return FanoutExchange*/@Beanpublic FanoutExchange fanoutExchange() {// new FanoutExchange("fanoutExchange");  // 创建方式1return ExchangeBuilder.fanoutExchange("mt.fanout2").durable(true).build();  // 创建方式2}/*** 创建队列* @return Queue*/@Beanpublic Queue fanoutQueue3() {// new Queue("mt.fanout3");  // 创建方式1return QueueBuilder.durable("fanout.queue3").build();  // 创建方式2}@Beanpublic Queue fanoutQueue4() {// new Queue("mt.fanout3");  // 创建方式1return QueueBuilder.durable("fanout.queue4").build();  // 创建方式2}/*** 绑定交换机与队列之间的关系 方式1* @return Binding*/@Beanpublic Binding fanoutBinding3() {return BindingBuilder.bind(fanoutQueue3()).to(fanoutExchange());}/*** 绑定交换机与队列之间的关系 方式2* @param fanoutQueue4 队列* @param fanoutExchange 交换机* @return Binding*/@Beanpublic Binding fanoutBinding4(Queue fanoutQueue4, FanoutExchange fanoutExchange) {return BindingBuilder.bind(fanoutQueue4).to(fanoutExchange);}
}

2.2 定向交换机

package com.ming.config;import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 定向交换机配置*/
@Configuration
public class DirectConfiguration {@Beanpublic DirectExchange directExchange() {return ExchangeBuilder.directExchange("mt.direct2").durable(true).build();}@Beanpublic Queue directQueue3() {return QueueBuilder.durable("direct.queue3").build();}@Beanpublic Binding directBinding3_red() {return BindingBuilder.bind(directQueue3()).to(directExchange()).with("red");}@Beanpublic Binding directBinding3_ble() {return BindingBuilder.bind(directQueue3()).to(directExchange()).with("blue");}
}

2.3 主题交换机

package com.ming.config;import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 主题交换机配置*/
@Configuration
public class TopicConfiguration {@Beanpublic TopicExchange topicExchange() {return ExchangeBuilder.topicExchange("mt.topic2").durable(true).build();}@Beanpublic Queue topicQueue3() {return QueueBuilder.durable("topic.queue3").build();}@Beanpublic Binding topicBinding3_1() {return BindingBuilder.bind(topicQueue3()).to(topicExchange()).with("china.#");}@Beanpublic Binding topicBinding3_2() {return BindingBuilder.bind(topicQueue3()).to(topicExchange()).with("#.news");}
}

三、使用注解声明

3.1 广播交换机

@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "fanout.queue5", durable = "true"),exchange = @Exchange(name = "mt.fanout3", type = ExchangeTypes.FANOUT)
))
public void listenFanoutQueue3(String message) {System.out.println(String.format("消费者3,收到了fanout.queue3: %s", message));
}@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "fanout.queue6", durable = "true"),exchange = @Exchange(name = "mt.fanout3", type = ExchangeTypes.FANOUT)
))
public void listenFanoutQueue4(String message) {System.err.println(String.format("消费者4,收到了fanout.queue4: %s", message));
}

3.2 定向交换机

@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue4", durable = "true"),exchange = @Exchange(name = "mt.direct3", type = ExchangeTypes.DIRECT),key = {"red", "blue"}
))
public void listenDirecttQueue3(String message) {System.err.println(String.format("消费者3,收到了direct.queue3: %s", message));
}@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue5", durable = "true"),exchange = @Exchange(name = "mt.direct3", type = ExchangeTypes.DIRECT),key = {"red", "yellow"}
))
public void listenDirecttQueue4(String message) {System.err.println(String.format("消费者4,收到了direct.queue4: %s", message));
}

3.4 主题交换机

@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue4", durable = "true"),exchange = @Exchange(name = "mt.topic3", type = ExchangeTypes.TOPIC),key = {"china.#", "#.news"}
))
public void listenTopicQueue3(String message) {System.out.println(String.format("消费者3,收到了topic.queue3: %s", message));
}
http://www.dtcms.com/a/340444.html

相关文章:

  • 数据湖学习
  • 安装electron报错的解决方法
  • 换根DP(P3478 [POI 2008] STA-StationP3574 [POI 2014] FAR-FarmCraft)
  • 智慧冷库物联网解决方案——实现降本增效与风险可控的冷库管理新范式
  • 算法学习----Python数据结构--kmp字符串
  • gcc 与 g++ 的区别:本身不是编译器而是编译器驱动
  • Day23 双向链表
  • STL模板库——string容器
  • DPO,PPO,GRPO
  • 【Linux基础知识系列:第一百零四篇】使用apt-cache管理软件包信息
  • 【数据结构】直接选择排序
  • 跨域问题解决方法
  • 链表-24.两两交换链表中的结点-力扣(LeetCode)
  • Spring Boot 3整合Nacos,配置namespace
  • 云计算学习100天-第26天
  • linux的sysctl系统以及systemd系统。
  • Linux + arm 内存属性
  • 静/动态库 IIC(arm) day58
  • 机器学习——网格搜索(GridSearchCV)超参数优化
  • Linux + arm 内存屏障
  • 商用厨房物联网智能化解决方案——打造环保、高效、安全的智慧餐饮新生态
  • C语言基础:(二十)自定义类型:结构体
  • 领码方案:通用物联网数据采集低代码集成平台——万物智联时代的黄金钥匙
  • 【Grafana】grafana-image-renderer配合python脚本实现仪表盘导出pdf
  • 车载软件架构 --- 赢得汽车软件开发竞赛
  • MySQL事务及原理详解
  • YAML格式笔记
  • SQL面试题及详细答案150道(41-60) --- 条件查询与分组篇
  • 【自记】Power BI 中 ALL、ALLSELECTED、ALLEXCEPT、ALLNOBLANKROW 的区别说明
  • 自学嵌入式第二十三天:数据结构(3)-双链表