1.声明
当前内容主要用于本人学习和复习,当前内容主要包含exchang和queue的声明,删除、绑定(以及queue中消息的清除)
当前内容来源:RabbitMQ官方文档 和 AMQP 0-9-1
2.关于Exchange的使用
1.声明或创建Exchange
// 声明并创建一个exchange
@Test
public void declareExchange() throws Exception {
RabbitMqUtils mqUtils = new RabbitMqUtils();
try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) {
DeclareOk declareOk = channel.exchangeDeclare("test", BuiltinExchangeType.TOPIC, true, false, null);
System.out.println("创建test exchange 类型为topic的结果===>" + declareOk);
}
}
2.Exchange和Exchange的绑定
// 交换机绑定
@Test
public void exchangeBind() throws Exception {
RabbitMqUtils mqUtils = new RabbitMqUtils();
try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) {
BindOk bindOk = channel.exchangeBind("test", "hello", "*");
System.out.println("删除test exchange 的结果===>" + bindOk);
}
}
3.Exchange和Exchange的解绑
// 交换机解绑
@Test
public void exchangeUnBind() throws Exception {
RabbitMqUtils mqUtils = new RabbitMqUtils();
try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) {
UnbindOk unbindOk = channel.exchangeUnbind("test", "hello", "*");
System.out.println("删除test exchange 的结果===>" + unbindOk);
}
}
4.Exchange的删除
// 删除创建的exchange
@Test
public void deleteExchange() throws Exception {
RabbitMqUtils mqUtils = new RabbitMqUtils();
try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) {
DeleteOk deleteOk = channel.exchangeDelete("test");
System.out.println("删除test exchange 的结果===>" + deleteOk);
}
}
5.使用被动声明Exchange
@Test
public void exchangeDeclarePassive() throws Exception {
RabbitMqUtils mqUtils = new RabbitMqUtils();
try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) {
DeclareOk declareOk = channel.exchangeDeclarePassive("test");
System.out.println("被动test exchange 类型为topic的结果===>" + declareOk);
}
}
3.关于Queue的使用
1.声明或者创建Queue
@Test
// 声明并创建一个queue
public void declareQueue() throws Exception {
RabbitMqUtils mqUtils = new RabbitMqUtils();
try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) {
// exclusive表示是否为独占队列,如果为true那么只有这个connection才能连接,其他不能连接
com.rabbitmq.client.AMQP.Queue.DeclareOk declareOk = channel.queueDeclare("hello", true, false, false,
null);
System.out.println("创建test queue的结果===>" + declareOk);
}
}
2.绑定Queue到Exchange
// queue与exchange绑定
@Test
public void queueBind() throws Exception {
RabbitMqUtils mqUtils = new RabbitMqUtils();
try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) {
com.rabbitmq.client.AMQP.Queue.BindOk bindOk = channel.queueBind("hello", "test", "");
System.out.println("删除创建的test queue的结果===>" + bindOk);
}
}
3.解除Queue到Exchange的绑定
// queue与exchange解绑
@Test
public void queueUnbind() throws Exception {
RabbitMqUtils mqUtils = new RabbitMqUtils();
try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) {
com.rabbitmq.client.AMQP.Queue.UnbindOk unbindOk = channel.queueUnbind("hello", "test", "");
System.out.println("删除创建的test queue的结果===>" + unbindOk);
}
}
4.清空Queue中的消息数量
@Test
// 清空一个queue中的所有消息
public void queuePurge() throws Exception{
String queueName="hello";
RabbitMqUtils mqUtils = new RabbitMqUtils();
try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) {
long messageCount = channel.messageCount(queueName);
System.out.println("执行purge之前该队列中的消息总数===>"+messageCount);
PurgeOk purgeOk = channel.queuePurge(queueName);
System.out.println("清空创建的test queue的结果===>" + purgeOk);
messageCount = channel.messageCount(queueName);
System.out.println("执行purge之后该队列中的消息总数===>"+messageCount);
}
}
5.删除Queue
@Test
// 删除一个queue
public void deleteQueue() throws Exception {
RabbitMqUtils mqUtils = new RabbitMqUtils();
try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) {
com.rabbitmq.client.AMQP.Queue.DeleteOk deleteOk = channel.queueDelete("hello");
System.out.println("删除创建的test queue的结果===>" + deleteOk);
}
}
被动声明一个Queue
@Test
public void queueDeclarePassive() throws Exception {
RabbitMqUtils mqUtils = new RabbitMqUtils();
try (Connection connection = mqUtils.getConnection(); Channel channel = connection.createChannel();) {
com.rabbitmq.client.AMQP.Queue.DeclareOk declareOk = channel.queueDeclarePassive("hello");
int messageCount = declareOk.getMessageCount();
System.out.println(messageCount);
System.out.println("被动test exchange 类型为topic的结果===>" + declareOk);
}
}
4.清空队列消息结果
这里主要展示清空消息队列中的消息的结果(前面的一部分在前面的文章中由这里不不显示)
1.首先发送十条消息到hello队列中
2.然后调用purge方法
3.最后ui界面的结果
清空成功!
5.总结
1.对于Exchange可以使用声明、绑定、解除绑定、删除等操作
2.对于Queue可以使用声明、绑定、解除绑定、删除、清空等操作
以上纯属个人见解,如有问题请联系本人!