我正在尝试使用 Spring AMQP 的 RabbitTemplate 来实现 RabbitMQ 的阻塞监听器。在我的代码中,我使用 Spring-amqp 1.1.3 版本的 jar 文件,而我也研究了版本 1.3.1,并且该版本也不支持这一点。有谁知道我是否缺少任何支持将阻止的监听器注册到 RabbitMQ 中的新连接的版本。或者是否有任何 future 版本的 spring amqp 来支持此功能。
示例代码:
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();
最佳答案
目前还不能开箱即用;请随时打开Improvement JIRA Issue .
但是,您可以将 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 - Spring AMQP : Register BlockedListener to Connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27105142/