rabbitmq基本使用

 

1,topic模式

package com.example.demo.conf;

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;

/****
 * 这里发送消息时,往topicExchange这个交换机中发送,并且路由规则为topic.b.key。
 * 由于b队列绑定了交换机和路由规则就是它,所以队列b能收到消息。
 * 但是由于A队列的过滤规则为topic.#,就是说只要topic开头的就的路由规则,交换机就会往这个队列里面发送消息。
 * 所以a队列也能收到消息,topic.b.key是topic开头的。
 * 
 * 对于a队列来说,路由规则为topic.adsf,topic.b.key,topic.a等等,a队列都将收到消息,
 * 因为它的路由规则就是topic开头就可以。
 * 
 * 消费者:监听队列就OK了,其他不用关心。
 * 
 * @author lenovo
 *
 */
@Configuration
public class RabbitMQConfig {
    public static final String TOPIC_EXCHANGE = "topicExchange";

    public static final String QUEUE_TOPIC_KEY = "topic.#";

    public static final String QUEUE_TOPIC_KEY_B = "topic.b.key";

    public static final String QUEUE_TOPIC_A = "topic.A";

    public static final String QUEUE_TOPIC_B = "topic.B";

    // 创建一个topic交换机
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }

    @Bean
    Queue queueTopicNameA() {
        return new Queue(QUEUE_TOPIC_A);
    }

    @Bean
    Queue queueTopicNameB() {
        return new Queue(QUEUE_TOPIC_B);
    }

    // 队列topic.A绑定交换机并且关联了topic.#正则路由规则。就是说只要topic.开头的,topic.A队列都将收到消息
    @Bean
    Binding bindingExchangeMessageTopicA(Queue queueTopicNameA, TopicExchange topicExchange) {
        return BindingBuilder.bind(queueTopicNameA).to(topicExchange).with(QUEUE_TOPIC_KEY);
    }

    // 队列topic.B绑定交换机并且关联了topic.b.key正则路由规则。就是说必须是topic.b.key,topic.B队列才能收到消息,和directExchange类型一样了。
    @Bean
    Binding bindingExchangeMessageTopicB(Queue queueTopicNameB, TopicExchange topicExchange) {
        return BindingBuilder.bind(queueTopicNameB).to(topicExchange).with(QUEUE_TOPIC_KEY_B);
    }

}

2,生产者

package com.example.demo.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.conf.RabbitMQConfig;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value = "mq发送消息队列", description = "mq管理接口", tags = "【0】ProducerDemo")
public class ProducerDemo {
    private static final Logger log = LoggerFactory.getLogger(ProducerDemo.class);
    
    @Autowired
    private AmqpTemplate rabbitTemplate;
    

        @RequestMapping("/topic/send")
        @ResponseBody
        @ApiOperation(value = "发送消息", httpMethod = "GET")
        public String sendTopicExchange() {
            String context = "Exchange==topic-->b====== " + new Date();
            log.info("Sender : " + context);
            this.rabbitTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE, "topic.b.key", context);
            return "success";
        }


}

3,消费者

package com.example.demo.listener;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/****
 * 当两个消费者同时监听一个队列时,他们并不能同时消费一条消息,而是随机消费消息。
 * @author lenovo
 *
 */
@Component
public class ConsumerDemo {

        @RabbitListener(queues = "topic.A")
        @RabbitHandler
        public void processtopicA(String hello) {
            System.out.println("Receiver Exchanges topic.A  ===================: " + hello);
        }

    
      @RabbitListener(queues = "topic.B")
      
      @RabbitHandler public void processtopicB(String hello) {
      System.out.println("Receiver Exchanges topic.B  ===================: " +
      hello); }
    
    
}

 

posted on 2022-06-23 14:15  让代码飞  阅读(25)  评论(0编辑  收藏  举报

导航

一款免费在线思维导图工具推荐:https://www.processon.com/i/593e9a29e4b0898669edaf7f?full_name=python