RabbitMQ:AMQP 0-9-1方法的简单使用

1.声明

当前内容主要用于本人学习和复习,当前内容主要包含exchang和queue的声明,删除、绑定(以及queue中消息的清除)

当前内容来源:RabbitMQ官方文档 和 AMQP 0-9-1

2.关于Exchange的使用

1.声明或创建Exchange

1// 声明并创建一个exchange 2@Test 3public void declareExchange() throws Exception { 4 RabbitMqUtils mqUtils = new RabbitMqUtils(); 5 6 try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) { 7 DeclareOk declareOk = channel.exchangeDeclare("test", BuiltinExchangeType.TOPIC, true, false, null); 8 System.out.println("创建test exchange 类型为topic的结果===>" + declareOk); 9 } 10} 11 12

2.Exchange和Exchange的绑定

1// 交换机绑定 2@Test 3public void exchangeBind() throws Exception { 4 RabbitMqUtils mqUtils = new RabbitMqUtils(); 5 6 try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) { 7 BindOk bindOk = channel.exchangeBind("test", "hello", "*"); 8 System.out.println("删除test exchange 的结果===>" + bindOk); 9 } 10} 11 12

3.Exchange和Exchange的解绑

1// 交换机解绑 2@Test 3public void exchangeUnBind() throws Exception { 4 RabbitMqUtils mqUtils = new RabbitMqUtils(); 5 6 try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) { 7 UnbindOk unbindOk = channel.exchangeUnbind("test", "hello", "*"); 8 System.out.println("删除test exchange 的结果===>" + unbindOk); 9 } 10} 11 12

4.Exchange的删除

1// 删除创建的exchange 2@Test 3public void deleteExchange() throws Exception { 4 RabbitMqUtils mqUtils = new RabbitMqUtils(); 5 6 try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) { 7 DeleteOk deleteOk = channel.exchangeDelete("test"); 8 System.out.println("删除test exchange 的结果===>" + deleteOk); 9 } 10} 11 12

5.使用被动声明Exchange

1@Test 2public void exchangeDeclarePassive() throws Exception { 3 RabbitMqUtils mqUtils = new RabbitMqUtils(); 4 5 try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) { 6 DeclareOk declareOk = channel.exchangeDeclarePassive("test"); 7 System.out.println("被动test exchange 类型为topic的结果===>" + declareOk); 8 } 9 10} 11 12

3.关于Queue的使用

1.声明或者创建Queue

1@Test 2// 声明并创建一个queue 3public void declareQueue() throws Exception { 4 RabbitMqUtils mqUtils = new RabbitMqUtils(); 5 try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) { 6 // exclusive表示是否为独占队列,如果为true那么只有这个connection才能连接,其他不能连接 7 com.rabbitmq.client.AMQP.Queue.DeclareOk declareOk = channel.queueDeclare("hello", true, false, false, 8 null); 9 System.out.println("创建test queue的结果===>" + declareOk); 10 } 11} 12 13

2.绑定Queue到Exchange

1// queue与exchange绑定 2@Test 3public void queueBind() throws Exception { 4 RabbitMqUtils mqUtils = new RabbitMqUtils(); 5 try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) { 6 com.rabbitmq.client.AMQP.Queue.BindOk bindOk = channel.queueBind("hello", "test", ""); 7 System.out.println("删除创建的test queue的结果===>" + bindOk); 8 } 9} 10 11

3.解除Queue到Exchange的绑定

1// queue与exchange解绑 2@Test 3public void queueUnbind() throws Exception { 4 RabbitMqUtils mqUtils = new RabbitMqUtils(); 5 try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) { 6 com.rabbitmq.client.AMQP.Queue.UnbindOk unbindOk = channel.queueUnbind("hello", "test", ""); 7 System.out.println("删除创建的test queue的结果===>" + unbindOk); 8 } 9} 10 11

4.清空Queue中的消息数量

1@Test 2// 清空一个queue中的所有消息 3public void queuePurge() throws Exception{ 4 String queueName="hello"; 5 RabbitMqUtils mqUtils = new RabbitMqUtils(); 6 try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) { 7 long messageCount = channel.messageCount(queueName); 8 System.out.println("执行purge之前该队列中的消息总数===>"+messageCount); 9 PurgeOk purgeOk = channel.queuePurge(queueName); 10 System.out.println("清空创建的test queue的结果===>" + purgeOk); 11 messageCount = channel.messageCount(queueName); 12 System.out.println("执行purge之后该队列中的消息总数===>"+messageCount); 13 } 14} 15 16

5.删除Queue

1@Test 2// 删除一个queue 3public void deleteQueue() throws Exception { 4 RabbitMqUtils mqUtils = new RabbitMqUtils(); 5 try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) { 6 com.rabbitmq.client.AMQP.Queue.DeleteOk deleteOk = channel.queueDelete("hello"); 7 System.out.println("删除创建的test queue的结果===>" + deleteOk); 8 } 9} 10 11

被动声明一个Queue

1@Test 2public void queueDeclarePassive() throws Exception { 3 RabbitMqUtils mqUtils = new RabbitMqUtils(); 4 5 try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) { 6 com.rabbitmq.client.AMQP.Queue.DeclareOk declareOk = channel.queueDeclarePassive("hello"); 7 int messageCount = declareOk.getMessageCount(); 8 System.out.println(messageCount); 9 System.out.println("被动test exchange 类型为topic的结果===>" + declareOk); 10 } 11 12} 13 14

4.清空队列消息结果

这里主要展示清空消息队列中的消息的结果(前面的一部分在前面的文章中由这里不不显示)

1.首先发送十条消息到hello队列中
在这里插入图片描述

2.然后调用purge方法
在这里插入图片描述

3.最后ui界面的结果
在这里插入图片描述

清空成功!

5.总结

1.对于Exchange可以使用声明、绑定、解除绑定、删除等操作

2.对于Queue可以使用声明、绑定、解除绑定、删除、清空等操作

以上纯属个人见解,如有问题请联系本人!

代码交流 2021