位置: IT常识 - 正文
推荐整理分享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:上一篇:如何禁用WordPress的XML-RPC功能(如何禁用笔记本键盘触摸板)
下一篇:如何打开phpcms网站的数据库配置文件(开启php)
友情链接: 武汉网站建设