springboot rabiitmq(二) topic模式

1.TopicController

package com.bctc.servlet.rabbitmq.topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/TopicController")
public class TopicController {
    @Autowired
    private TopicSender sender;
    @GetMapping(value = "/topic")
    public void topic(String a) throws Exception {
        sender.send(a);
    }
    @GetMapping(value = "/topic1")
    public void topic1(String a) throws Exception {
        sender.send1(a);
    }
    @GetMapping(value = "/topic2")
    public void topic2(String a) throws Exception {
        sender.send2(a);
    }
}

2.TopicRabbitConfig

package com.bctc.servlet.rabbitmq.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;
/**
 * topic 类型的交换器允许在RabbitMQ中使用模糊匹配来绑定自己感兴趣的信息。
 */
@Configuration
public class TopicRabbitConfig {
    final static String a = "topic.aa";
    final static String b = "topic.bb";
    final static String c = "topic.cc";
    final static String d = "topic.dd";
    //创建队列
    @Bean
    public Queue queueMessage() {
        return new Queue(TopicRabbitConfig.a);
    }
    //创建队列
    @Bean
    public Queue queueMessage2() {
        return new Queue(TopicRabbitConfig.b);
    }
    @Bean
    public Queue queueMessagec() {
        return new Queue(TopicRabbitConfig.c);
    }
    @Bean
    public Queue queueMessaged() {
        return new Queue(TopicRabbitConfig.d);
    }
    //创建交换器
    @Bean
    TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }
    @Bean
    TopicExchange exchange2() {
        return new TopicExchange("topicExchange2");
    }
    @Bean  //对列绑定并关联到ROUTINGKEY//topic.aa就可以监听到topic.aba的消息
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        BindingBuilder.TopicExchangeRoutingKeyConfigurer to = BindingBuilder.bind(queueMessage).to(exchange);
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.abab");
    }
    @Bean
    Binding bindingExchangeMessage1(Queue queueMessage2, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage2).to(exchange).with("topic.abab");
    }
    @Bean//*表示一个词,#表示零个或多个词
    Binding bindingExchangeMessagec(Queue queueMessagec, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessagec).to(exchange).with("*.c1.#");//*表示一个词,#表示零个或多个词
    }
    @Bean//*表示一个词,#表示零个或多个词
    Binding bindingExchangeMessaged(Queue queueMessaged, TopicExchange exchange2) {
        return BindingBuilder.bind(queueMessaged).to(exchange2).with("dd.dd.*");//*表示一个词,#表示零个或多个词
    }
}

3.TopicSender生产者

package com.bctc.servlet.rabbitmq.topic;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class TopicSender {
    @Autowired
    private AmqpTemplate rabbitTemplate;
    public void send(String a) {
        this.rabbitTemplate.convertAndSend("topicExchange", "topic.abab", a);
    }
    public void send1(String a) {
        this.rabbitTemplate.convertAndSend("topicExchange", "c1.c1.15d", a);
    }
    public void send2(String a) {
        this.rabbitTemplate.convertAndSend("topicExchange2", "dd.dd.c112", a);
    }
}

4.TopicReceiver消费者

package com.bctc.servlet.rabbitmq.topic;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class TopicReceiver {
    @RabbitListener(queues = "topic.aa")
    @RabbitHandler
    public void process(String message) {
        System.out.println("topic.aa  Receiver1  : " + message);
    }
   /* @RabbitListener(queues = "topic.aa")//一次只有一个能接收消息
    @RabbitHandler
    public void processs(String message) {
        System.out.println("topic.aa  Receiveraa  : " + message);
    }*/
    @RabbitListener(queues = "topic.bb")
    @RabbitHandler
    public void process2(String message) {
        System.out.println("topic.bb bbbbbbb  : " + message);
    }
    @RabbitListener(queues = "topic.cc")
    @RabbitHandler
    public void processc(String message) {
        System.out.println("topic.cc ccccccc  : " + message);
    }
    @RabbitListener(queues = "topic.dd")
    @RabbitHandler
    public void processd(String message) {
        System.out.println("topic.dd dddddddd  : " + message);
    }
 /*   @RabbitListener(queues = "topic-topic.*")
    @RabbitHandler
    public void process3(String message) {
        System.out.println("topic-topic.*  : " + message);
    }*/
}

http://localhost:9081/TopicController/topic?a=niuhao


http://localhost:9081/TopicController/topic1?a=niuhao


http://localhost:9081/TopicController/topic2?a=niuhao






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值