RabbitMQ删除队列无法释放连接
RabbitMQ delete queue can not release connection
在我的 java 程序中,正在通过 RabbitMQ 队列发送某种消息,如下所示:
if(!con.isConnected()){
log.error("Not connected !!!");
return false;
}
con.getChannel().basicPublish("",queueName, MessageProperties.PERSISTENT_BASIC, bytes)
- 我通过 RabbitMQ 管理 GUI 插件删除了队列
- 尝试通过已删除的队列发送消息
结果: 队列已从 RabbitMQ GUI 中删除,但是当我尝试通过已删除的 RabbitMQ 队列发送消息时,连接仍然存在。(con.isConnected() == true ) 我需要找到一种方法来监听队列,如果它被删除,我不应该向删除的队列发送任何消息。
注意:删除队列后,我没有重启RabbitMQ。
频道创建:
channel = connection.createChannel();
channel.queueDeclare(prop.getQueueName(), true, false, false, null);
示例代码通道、队列、交换创建:
ConnectionFactory cf = new ConnectionFactory();
cf.setUsername("guest");
cf.setPassword("guest");
cf.setHost("localhost");
cf.setPort(5672);
cf.setAutomaticRecoveryEnabled(true);
cf.setConnectionTimeout(10000);
cf.setNetworkRecoveryInterval(10000);
cf.setTopologyRecoveryEnabled(true);
cf.setRequestedHeartbeat(5);
Connection connection = cf.newConnection();
channel = connection.createChannel();
channel.queueDeclare("test", true, false, false, null);
channel.exchangeDeclare("testExchange", "direct",true);
channel.queueBind("test", "testExchange", "testRoutingKey");
connection.addShutdownListener(new ShutdownListener() {
@Override
public void shutdownCompleted(ShutdownSignalException cause) {
System.out.println("test"+cause);
}
});
正在发送消息:
channel.basicPublish("testExchange", "testRoutingKey", null,messageBodyBytes);
来自 RabbitMQ google
Messages in AMQP 0-9-1 are not published to queues; they are published to exchanges, from where they
are routed to a queue (or another exchange) or not. [1]
basic.publish is a completely asynchronous protocol method by design: there is no response for it
unless you ask for it [2]. Messages that are unroutable can be returned to the publisher
if you define a return listener and publish with the mandatory flag set to true.
Note that publisher confirms and the mandatory flag/returns are orthogonal and one does not imply
the other.
定义 return 侦听器并将强制标志设置为 true 解决了我的问题。如果有任何消息未被路由,我可以使用 ReturnListener 捕获它们并添加到我的持久化队列中,以便在系统激活时再次发送。
在我的 java 程序中,正在通过 RabbitMQ 队列发送某种消息,如下所示:
if(!con.isConnected()){
log.error("Not connected !!!");
return false;
}
con.getChannel().basicPublish("",queueName, MessageProperties.PERSISTENT_BASIC, bytes)
- 我通过 RabbitMQ 管理 GUI 插件删除了队列
- 尝试通过已删除的队列发送消息
结果: 队列已从 RabbitMQ GUI 中删除,但是当我尝试通过已删除的 RabbitMQ 队列发送消息时,连接仍然存在。(con.isConnected() == true ) 我需要找到一种方法来监听队列,如果它被删除,我不应该向删除的队列发送任何消息。
注意:删除队列后,我没有重启RabbitMQ。
频道创建:
channel = connection.createChannel();
channel.queueDeclare(prop.getQueueName(), true, false, false, null);
示例代码通道、队列、交换创建:
ConnectionFactory cf = new ConnectionFactory();
cf.setUsername("guest");
cf.setPassword("guest");
cf.setHost("localhost");
cf.setPort(5672);
cf.setAutomaticRecoveryEnabled(true);
cf.setConnectionTimeout(10000);
cf.setNetworkRecoveryInterval(10000);
cf.setTopologyRecoveryEnabled(true);
cf.setRequestedHeartbeat(5);
Connection connection = cf.newConnection();
channel = connection.createChannel();
channel.queueDeclare("test", true, false, false, null);
channel.exchangeDeclare("testExchange", "direct",true);
channel.queueBind("test", "testExchange", "testRoutingKey");
connection.addShutdownListener(new ShutdownListener() {
@Override
public void shutdownCompleted(ShutdownSignalException cause) {
System.out.println("test"+cause);
}
});
正在发送消息:
channel.basicPublish("testExchange", "testRoutingKey", null,messageBodyBytes);
来自 RabbitMQ google
Messages in AMQP 0-9-1 are not published to queues; they are published to exchanges, from where they are routed to a queue (or another exchange) or not. [1] basic.publish is a completely asynchronous protocol method by design: there is no response for it unless you ask for it [2]. Messages that are unroutable can be returned to the publisher if you define a return listener and publish with the mandatory flag set to true. Note that publisher confirms and the mandatory flag/returns are orthogonal and one does not imply the other.
定义 return 侦听器并将强制标志设置为 true 解决了我的问题。如果有任何消息未被路由,我可以使用 ReturnListener 捕获它们并添加到我的持久化队列中,以便在系统激活时再次发送。