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

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可以使用声明、绑定、解除绑定、删除、清空等操作

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值