Rabbitmq Spring AMQP:将BlockedListener注册到连接
我正在尝试使用Spring AMQP的RabbitTemplate实现RabbitMQ的阻塞侦听器。在我的代码中,我使用的是SpringAMQP1.1.3版本jar文件,而我也研究了1.3.1版本,这在这个版本中也是不受支持的。有人知道我是否缺少支持在RabbitMQ中向新连接注册阻止的侦听器的任何版本。或者是否有任何spring amqp的未来版本支持此功能 示例代码:Rabbitmq Spring AMQP:将BlockedListener注册到连接,rabbitmq,amqp,spring-amqp,Rabbitmq,Amqp,Spring Amqp,我正在尝试使用Spring AMQP的RabbitTemplate实现RabbitMQ的阻塞侦听器。在我的代码中,我使用的是SpringAMQP1.1.3版本jar文件,而我也研究了1.3.1版本,这在这个版本中也是不受支持的。有人知道我是否缺少支持在RabbitMQ中向新连接注册阻止的侦听器的任何版本。或者是否有任何spring amqp的未来版本支持此功能 示例代码: Connection connection = factory.newConnection(); conne
Connection connection = factory.newConnection();
connection.addBlockedListener(new BlockedListener() {
@Override
public void handleUnblocked() throws IOException {
System.out.println("Connection is Unblocked");
}
@Override
public void handleBlocked(String arg0) throws IOException {
System.out.println("Connection Blocked");
}
});
com.rabbitmq.client.Channel channel = connection.createChannel();
这是目前无法提供的开箱即用;请随时打开一个 但是,您可以将Spring AMQP
ConnectionListener
添加到CachingConnectionFactory
connectionFactory.addConnectionListener(new ConnectionListener() {
@Override
public void onCreate(Connection connection) {
Channel channel = connection.createChannel(false);
channel.getConnection().addBlockedListener(new BlockedListener() {
@Override
public void handleUnblocked() throws IOException {
}
@Override
public void handleBlocked(String reason) throws IOException {
}
});
try {
channel.close();
}
catch (IOException e) {
}
}
@Override
public void onClose(Connection connection) {
}
});
即使在添加侦听器时已建立连接,也会调用它。我添加此项是为了获得阻塞连接通知,并且它可以工作,是否有任何方法可以发现RabbitMQ连接因RabbitMQ restart@Gary Russell而关闭?您不应该在评论中提出新问题;总是问一个新问题。不清楚你在问什么;有一个
onClose
方法。在2.0中,当一个连接被强制关闭时,还会出现一个onShutDown
。谢谢!为什么在这里调用channel.close()
?此实现是否覆盖了使用连接/通道与RabbitMQ通信的方式?或者它只是一个用来监听连接阻塞事件的钩子?它只是一个钩子。传递给onCreate
的Connection
参数不是实际的连接,而是包装器;channel.getConnection()
允许我们访问真实的连接,以便添加侦听器。close()
实际上并没有关闭通道,而是将其返回到缓存以供将来的操作使用。但是,这不再需要()。现在,您可以将BlockedListener
直接添加到onCreate连接参数中。我们还发布事件,这些事件可以由SpringApplicationListener
捕获。