引入依赖:
<!--父工程--> <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); } }
原文:https://www.cnblogs.com/qq2210446939/p/15149384.html