一文带你彻底搞懂SpringBoot-RabbitMQ(1),java多线程锁面试题

以上是字节二面的一些问题,面完之后其实挺后悔的,没有提前把各个知识点都复习到位。现在重新好好复习手上的。
摘要由CSDN通过智能技术生成

3.订阅者会轮流收到信息

receiver01 message = direct

receiver02 message = direct

receiver01 message = direct

receiver02 message = direct

receiver01 message = direct

receiver02 message = direct

复制代码

2.2 topic - 主题交换器


2.2.1 消息发送者

声明topic交换器

import org.springframework.amqp.core.TopicExchange;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

public class BlogPublisherConfig {

@Bean

public Exchange blogTopicExchange() {

return ExchangeBuilder.topicExchange(“exchange.topic.springboot.blog”).build();

}

}

复制代码

声明controller

@RequestMapping(“/topic”)

public Object topic(String routingKey, String message) {

rabbitTemplate.convertAndSend(“exchange.topic.springboot.blog”, routingKey, message);

return routingKey + " : " + message;

}

复制代码

2.2.2 消息接收者

声明交换器、三个队列、队列的绑定

  • *:匹配一个串

  • #:匹配一个或者多个串

import org.springframework.amqp.core.Binding;

import org.springframework.amqp.core.BindingBuilder;

import org.springframework.amqp.core.Queue;

import org.springframework.amqp.core.TopicExchange;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration

public class BlogSubscriberConfig {

/**

  • 主题交换器

*/

@Bean

public TopicExchange blogTopicExchange() {

return ExchangeBuilder.topicExchange(“exchange.topic.springboot.blog”).build();

}

@Bean

public Queue blogJavaQueue() {

return QueueBuilder.durable(“queue.topic.springboot.blog.java”).build();

}

@Bean

public Queue blogMqQueue() {

return QueueBuilder.durable(“queue.topic.springboot.blog.mq”).build();

}

@Bean

public Queue blogAllQueue() {

return QueueBuilder.durable(“queue.topic.springboot.blog.all”).build();

}

@Bean

@Resource

public Binding blogJavaBinding(TopicExchange blogTopicExchange, Queue blogJavaQueue) {

return BindingBuilder.bind(blogJavaQueue).to(blogTopicExchange).with(“springboot.blog.java.routing.key”);

}

@Bean

@Resource

public Binding blogMqBinding(TopicExchange blogTopicExchange, Queue blogMqQueue) {

return BindingBuilder.bind(blogMqQueue).to(blogTopicExchange).with(“springboot.blog.mq.routing.key”);

}

@Bean

@Resource

public Binding blogAllBinding(TopicExchange blogTopicExchange, Queue blogAllQueue) {

// #: 匹配一个或者多个 *:匹配一个

return BindingBuilder.bind(blogAllQueue).to(blogTopicExchange).with(“springboot.blog.#.routing.key”);

}

}

复制代码

监听队列

import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.stereotype.Service;

@Service

public class BlogService {

/**

  • topic监听

*/

@RabbitListener(queues = “queue.topic.springboot.blog.java”)

public void blogJavaListener(String message) {

System.out.println("blogJavaListener message = " + message);

}

@RabbitListener(queues = “queue.topic.springboot.blog.mq”)

public void blogMqListener(String message) {

System.out.println("blogMqListener message = " + message);

}

@RabbitListener(queues = “queue.topic.springboot.blog.all”)

public void blogAllaListener(String message) {

System.out.println("blogAllListener message = " + message);

}

}

复制代码

2.2.3 消息发布订阅

  1. 发布者发送消息
  1. 订阅者收到消息
  • 全匹配和模糊匹配

  • 全匹配无论是哪个都会被匹配上

blogJavaListener message = hello

blogAllListener message = hello

blogAllListener message = hello</

  • 28
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值