基本消息队列的消息发送流程:
1.建立连接connection
2.简历通道channel
3.利用channel声明队列
4.利用channel向队列发送消息
基本消息队列的信息接收流程:
1.建立连接connection
2.建立通道channel 因为不确定发送和接收谁先建立通道,所以两者都建立避免无效访问
3.利用通道channel声明队列
4.定义消费行为handleDelivery() 匿名内部类 消息的接收后进行处理
5.利用channel将消费者与队列绑定
SpringAMQP:
一种通信协议,与平台和语言无关
使用amqp发送消息
1.引入依赖
<!--AMQP依赖,包含RabbitMQ--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
2.写配置 需要配置ip 端口 虚拟主机 用户名密码等
spring: rabbitmq: host: 192.168.0.0 port: 5672 virtual-host: / username: 000 password: 000
3.Autowired RabbitTemplate
rabbitTemplate.convertAndSend(队列名,需要发送的消息);
RabbitMQ中消息的消费:
1.引入依赖
2.编写配置
这两步都和发送一样
3.创建类 类上加@Compone
4.类中创建方法 @RabbitListener(queues = "xxx")
记得声明参数 String msg
rabbitMQ中的消息一点消费 无法回溯