zoukankan      html  css  js  c++  java
  • springboot整合rabbitmq

    生产者整合

      引入依赖:

    <!--父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>
    <!--依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

      然后我们可以写一个config类,来定义交换机和队列,以及他们的绑定关系

    package com.layton.rabbitmq.config;
    
    import org.springframework.amqp.core.*;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class RabbitMQConfig {
    
        public static final String Exchange_NAME = "boot_topic_exchange";
        public static final String Queue_NAME = "boot_topic_queue";
    
        //1.交换机
        @Bean("bootExchange")
        public Exchange bootExchange(){
            return ExchangeBuilder.topicExchange(Exchange_NAME).durable(true).build();
        }
    
        //2.队列
        @Bean("bootQueue")
        public Queue bootQueue(){
            return QueueBuilder.durable(Queue_NAME).build();
        }
    
        //3.队列和交换机绑定的关系Binding
        @Bean
        public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,@Qualifier("bootExchange") Exchange exchange){
            return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
        }
    }

      这样绑定完了之后,我们可以在生产端想发消息的地方注入RabbitTemplate工具类,然后完成方法的发送

    package com.layton.test;
    
    import com.layton.rabbitmq.config.RabbitMQConfig;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class ProducerTest {
        //1注入RabbitTemplate类
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        @Test
        public void testSend(){
            rabbitTemplate.convertAndSend(RabbitMQConfig.Exchange_NAME,"boot.haha","boot mq hello~");
        }
    }

    消费端整合

      同样的引入依赖:

    <!--父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>
    <!--依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

      然后定义消费端的监听器,用于监听队列的信息,需要注意的是RabbitListener注解中的queues属性就是指定队列名,同时需要注意这个类要加Component注解,让spring进行管理

    package com.layton.mq;
    
    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    @Component
    public class RabbitMQListener {
        @RabbitListener(queues = "boot_topic_queue")
        public void ListenerQueue(Message message){
            System.out.println(message);
        }
    }
    一点一点积累,一点一点蜕变!
  • 相关阅读:
    07-图4 哈利·波特的考试 (25分)
    Windows环境下清除SVN文件
    查看SQL SERVER 2008R2 表大小
    Oauth支持的5类 grant_type 及说明
    SignalR的性能监测
    Loadrunner11安装
    Azure ServiceBus 通信失败问题
    sql server text类型 存储问题
    System.BadImageFormatException
    InputStream只能读取一次的解决办法 C# byte[] 和Stream转换
  • 原文地址:https://www.cnblogs.com/qq2210446939/p/15149384.html
Copyright © 2011-2022 走看看