位置:- 正文

RabbitMQ学习笔记(rabbitmq中文手册)

编辑:rootadmin
RabbitMQ 整合RabbitMQ /** * 使用RabbitMQ * 1、引入ampq场景,RabbitAutoConfiguration 就会自动生效 * 2、给容器中自动配置了 * RabbitTemplate、AmqpAdmin、CachingConnectionFactory、Rab ... RabbitMQ整合RabbitMQ/** * 使用RabbitMQ * 1、引入ampq场景,RabbitAutoConfiguration 就会自动生效 * 2、给容器中自动配置了 * RabbitTemplate、AmqpAdmin、CachingConnectionFactory、RabbitMessagingTemplate * 所有的属性都是在 * @EnableConfigurationProperties(RabbitProperties.class) * @ConfigurationProperties(prefix = "spring.rabbitmq") * public class RabbitProperties * 3、给配置文件中配置 spring.rabbitmq 信息 * 4、@EnableRabbit 开启功能 * 5、监听消息:使用 @RabbitListener,必须有 @EnableRabbit * @RabbitListener:类 + 方法上 * @RabbitHandler: 只能标在方法上 */<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId></dependency># rabbit 配置文件spring.rabbitmq.host=192.168.106.101spring.rabbitmq.port=5672spring.rabbitmq.virtual-host=/

推荐整理分享RabbitMQ学习笔记(rabbitmq中文手册),希望有所帮助,仅作参考,欢迎阅读内容。

RabbitMQ学习笔记(rabbitmq中文手册)

文章相关热门搜索词:rabbitmq入门案例,rabbitmq入门到精通,rabbit mq教程,rabbitmq入门,rabbitmq简书,rabbitmq入门,rabbit mq教程,rabbit mq教程,内容如对您有帮助,希望把文章链接给更多的朋友!

测试

package com.atguigu.gulimall.order;import com.atguigu.gulimall.order.entity.OrderReturnApplyEntity;import lombok.extern.slf4j.Slf4j;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.amqp.core.AmqpAdmin;import org.springframework.amqp.core.Binding;import org.springframework.amqp.core.DirectExchange;import org.springframework.amqp.core.Queue;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;import java.util.Date;@Slf4j@RunWith(SpringRunner.class)@SpringBootTestpublic class GulimallOrderApplicationTests { @Autowired AmqpAdmin amqpAdmin; @Autowired RabbitTemplate rabbitTemplate; /** * 1、创建Exchange[hello.java.exchange]、Queue、Binding * - 使用 AmqpAdmin 进行创建 * * 2、如何收发消息 -> RabbitTemplate * 如果发送的消息是个对象,使用序列化机制,将对象写出去,对象实现 Serializable 接口 * 自定义序列化添加配置 * @Configuration * public class MyRabbitConfig { * @Bean * public MessageConverter messageConverter() { * return new Jackson2JsonMessageConverter(); * } * } */ @Test public void sendMessageTest() { String msg = "Hello World"; OrderReturnApplyEntity orderReturnApplyEntity = new OrderReturnApplyEntity(); orderReturnApplyEntity.setId(1L); orderReturnApplyEntity.setSkuName("华为"); orderReturnApplyEntity.setCreateTime(new Date()); rabbitTemplate.convertAndSend("hello.java.exchange", "hello.java", orderReturnApplyEntity); log.info("消息发送完成:{}", orderReturnApplyEntity); } @Test public void createExchange() { //amqpAdmin /** * DirectExchange * public DirectExchange(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments) */ DirectExchange exchange = new DirectExchange("hello.java.exchange", true,false); amqpAdmin.declareExchange(exchange); log.info("Exchange[{}]创建成功", "hello.java.exchange"); } @Test public void createQueue() { /** * public Queue(String name, boolean durable, boolean exclusive, boolean autoDelete) */ Queue queue = new Queue("hello-java-queue", true, false,true); amqpAdmin.declareQueue(queue); log.info("Queue[{}]创建成功", "hello-java-queue"); } @Test public void createBinding() { /** * public Binding(String destination【目的地】, * DestinationType destinationType【目的地类型】, * String exchange【交换机】, * String routingKey【路由键】, * Map<String, Object> arguments)【参数】 * 将 exchange 指定交换机和 destination目的地进行绑定,使用routingKey作为指定路由键 */ Binding binding = new Binding("hello-java-queue", Binding.DestinationType.QUEUE,"hello.java.exchange","hello.java",null); amqpAdmin.declareBinding(binding); log.info("Binding == 创建成功"); }}

测试监听消息

/** * queues:
本文链接地址:https://www.jiuchutong.com/zhishi/309258.html 转载请保留说明!
下一篇链接:https://www.jiuchutong.com/zhishi/309259.html
免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络