创建提供者
- 通过idea的springboot项目快速搭建,创建springboot-rabbitmq-provider模块
- 保证pom.xml中存在这几个依赖
<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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 创建application.yml配置文件
spring:
rabbitmq:
port: 5672
host: localhost
virtual-host: /
username: guest
password: guest
- 将队列和交换机通过注解和配置类注入spring容器中
因为启动类就相当于一个配置类了,所以直接在启动类下面进行bean注入
package com.javaee.springbootrabbitmq;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringbootRabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootRabbitmqApplication.class, args);
}
/**
* 创建topic交换机并设置为长期存在
*
* @return topic交换机对象
*/
@Bean("topicExchange")
public Exchange topicExchange() {
return ExchangeBuilder.topicExchange("topicExchange").durable(true).build();
}
/**
* 创建topic队列交给spring
*
* @return 队列对象
*/
@Bean("topicQueue")
public Queue topicQueue() {
return QueueBuilder.durable("topicQueue").build();
}
/**
* 绑定交换机和队列
*
* @param topicExchange 绑定的topic交换机
* @param topicQueue 绑定的队列
* @return 返回绑定关系
*/
@Bean
public Binding topicQueueExchange(@Qualifier("topicExchange") Exchange topicExchange,
@Qualifier("topicQueue") Queue topicQueue) {
//指定交换机和队列进行绑定,并指定路由key的通配符
return BindingBuilder.bind(topicQueue).to(topicExchange).with("order.#").noargs();
}
}
- 写测试类
通过测试类写入三条消息
package com.javaee.springbootrabbitmq;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRabbitmqApplicationTests {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void contextLoads() {
rabbitTemplate.convertAndSend("topicExchange","order.insert","执行订单插入。。。");
rabbitTemplate.convertAndSend("topicExchange","order.update","执行订单修改。。。");
rabbitTemplate.convertAndSend("topicExchange","order.delete","执行订单删除。。。");
}
}
执行后能够从控制台发现确实存在三条消息
创建消费者
- 通过idea的springboot项目快速搭建,创建springboot-rabbitmq-consumer
- 保证pom.xml中存在这几个依赖
<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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 创建application.yml配置文件
spring:
rabbitmq:
port: 5672
host: localhost
virtual-host: /
username: guest
password: guest
- 编写消息监听器
package com.javaee.springbootrabbitmqconsumer;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MyListener {
//将消费者绑定到指定队列
@RabbitListener(queues = "topicQueue")
public void myListener(Message message){
System.out.println("消费者接受到的指令是:" + new String(message.getBody));
}
}
- 执行springboot启动类,因为导入了rabbitMQ的依赖并且配置了消息监听器,所以程序运行后会先去监听并执行队列中的消息,执行完毕后会进入阻塞状态,等待队列出现消息。
执行结果
可以看到已经执行完消息队列中的所有消息,并进入阻塞状态。当还有用户发送消息时,阻塞中的程序会继续执行队列中的消息。