rabbitmq详解第三天(消息类型一topic 主题模式)

direct并不能满足我们的业务需求,因为direct只能绑定一个固定的消息类型,但是topic就可以使用通配符,绑定一类的消息类型

 配置

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;

@Configuration

public class TopicConfig {

    final static String MESSAGE = "topic.message";
    final static String MESSAGES = "topic.messages";

    @Bean
    public Queue queueMessage() {
        return new Queue(MESSAGE);
    }

    @Bean
    public Queue queueMessages() {
        return new Queue(MESSAGES);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("exchange");
    }

    /**
     * 将队列topic.message与exchange绑定,binding_key为topic.message,就是完全匹配
     * @param queueMessage
     * @param exchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
    }

    /**
     * 将队列topic.messages与exchange绑定,binding_key为topic.#,模糊匹配
     * @param queueMessages
     * @param exchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
    }
}

生产者

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TopicExchangeProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send() {
        rabbitTemplate.convertAndSend("exchange", "topic.message", "topic.message");
        rabbitTemplate.convertAndSend("exchange", "topic.messages", "topic.messages");
    }
}

消费者

/**
 * 消费者messages
 */
@Component
@RabbitListener(queues = "topic.messages")
public class MessagesCustomer {

    @RabbitHandler
    public void process(String msg){
        System.out.println("topicexchange messages 消费者  : " +msg);
    }
}

/**
 * 消费者message
 */
@Component
@RabbitListener(queues = "topic.message")
public class MessageCustomer {

    @RabbitHandler
    public void process(String msg){
        System.out.println("topicexchange message 消费者  : " +msg);
    }
}

测试类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TopicController {

    @Autowired
    private TopicExchangeProducer topicExchangeProducer;

    @RequestMapping("/topicexchange")
    public String topicExchange(){
        topicExchangeProducer.send();
        return "ok";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄泉路好走

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值