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);
        }
    }
    一点一点积累,一点一点蜕变!
  • 相关阅读:
    php连接mysql数据库基础
    控制操作
    巨慢IE9的加速
    推广邮件客户端(二):完美的IMAP客户端
    XPath 和 LINQ to XML 的比较
    推广邮件客户端(三):常用IMAP客户端介绍
    关于Git工具与GitHub
    Android开源项目(非组件)
    windows下使用Git获取Android源码
    Eclipse导入项目:No projects are found to import
  • 原文地址:https://www.cnblogs.com/qq2210446939/p/15149384.html
Copyright © 2011-2022 走看看