我尝试使用连接到Rabbit交换机的Spring Boot创建Java服务,发现新队列(与给定前缀匹配)并连接到它们。我使用RabbitManagementTemplate
来发现并SimpleMessageListenerContainer
来创建绑定。它工作正常。
问题是,当其中一个动态队列被删除时(例如通过Web界面),我的服务无法处理异常,我找不到一个地方来注册一些处理程序来修复这个。对于这些情况,我只想忽略删除并继续前进,我不愿意重新创建队列。
我的代码就像
@Scheduled(fixedDelay = 3*1000)
public void watchNewQueues() {
for (Queue queue : rabbitManagementTemplate.getQueues()) {
final String queueName = queue.getName();
String[] nameParts = queueName.split("\\.");
if ("dynamic-queue".equals(nameParts[0]) && !context.containsBean(queueName)) {
logger.info("New queue discovered! Binding to {}", queueName);
Binding binding = BindingBuilder.bind(queue).to(exchange).with("testroute.#");
rabbitAdmin.declareBinding(binding);
rabbitAdmin.declareQueue(queue);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(new MyMessageListener());
container.setPrefetchCount(settings.getPrefetch());
container.setAutoDeclare(false);
container.setMissingQueuesFatal(true);
container.setDeclarationRetries(0);
container.setFailedDeclarationRetryInterval(-1);
context.getBeanFactory().registerSingleton(queueName, container);
container.start();
}
}
}
@Override
public void onApplicationEvent(ListenerContainerConsumerFailedEvent event) {
if (event.getSource() instanceof SimpleMessageListenerContainer) {
SimpleMessageListenerContainer container = (SimpleMessageListenerContainer) event.getSource();
if (context.getAutowireCapableBeanFactory() instanceof BeanDefinitionRegistry) {
logger.info("Removing bean! {}", container.getQueueNames()[0]);
((BeanDefinitionRegistry)context.getAutowireCapableBeanFactory()).removeBeanDefinition(container.getQueueNames()[0]);
} else {
logger.info("Context is not able to remove bean");
}
} else {
logger.info("Got event but is not a SimpleMessageListenerContainer {}", event.toString());
}
}
当队列被删除时,控制台记录:
2018-03-13 15:01:29.623 WARN 32736 [pool-1-thread-6] --- o.s.a.r.listener.BlockingQueueConsumer : Cancel received for amq.ctag-wKQUQkUNOSCtjQ9RBUNCig; Consumer: tags=[{amq.ctag-wKQUQkUNOSCtjQ9RBUNCig=dynamic-queue.some-test}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@localhost:5672/,3), conn: Proxy@23510c77 Shared Rabbit Connection: SimpleConnection@66c17803 [delegate=amqp://guest@localhost:5672/], acknowledgeMode=AUTO local queue size=0
2018-03-13 15:01:30.219 WARN 32736 [SimpleAsyncTaskExecutor-1] --- o.s.a.r.l.SimpleMessageListenerContainer : Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.rabbit.support.ConsumerCancelledException
2018-03-13 15:01:30.219 INFO 32736 [SimpleAsyncTaskExecutor-1] --- o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer: tags=[{}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@localhost:5672/,3), conn: Proxy@23510c77 Shared Rabbit Connection: SimpleConnection@66c17803 [delegate=amqp://guest@localhost:5672/], acknowledgeMode=AUTO local queue size=0
2018-03-13 15:01:30.243 WARN 32736 [SimpleAsyncTaskExecutor-2] --- o.s.a.r.listener.BlockingQueueConsumer : Failed to declare queue:dynamic-queue.some-test
2018-03-13 15:01:30.246 WARN 32736 [SimpleAsyncTaskExecutor-2] --- o.s.a.r.listener.BlockingQueueConsumer : Queue declaration failed; retries left=3
org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: Failed to declare queue(s):[dynamic-queue.some-test]
at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:571)
at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:470)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1171)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: null
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106)
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124)
at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:885)
at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:835)
at com.sun.proxy.$Proxy63.queueDeclarePassive(Unknown Source)
at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:550)
... 3 common frames omitted
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'dynamic-queue.some-test' in vhost '/', class-id=50, method-id=10)
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:361)
at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:226)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118)
... 12 common frames omitted
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'dynamic-queue.some-test' in vhost '/', class-id=50, method-id=10)
at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:484)
at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:321)
at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144)
at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:554)
... 1 common frames omitted
感谢您的关注
修改 谢谢!我能够避免重新排队。我现在正在努力从Spring Context中删除队列:)
答案 0 :(得分:2)
当然,您将获得错误日志,但使用container.setMissingQueuesFatal(true);
(默认值),容器将在3次尝试以5秒的间隔声明队列后自行停止。
您可以通过设置declarationRetries
(默认为3)和failedDeclarationRetryInterval
(默认为5000)来影响停止所需的时间。