无法启动bean'org.springframework.amqp.rabbit.config.internalRabbitListenerEndpointRegistry'

时间:2017-10-25 00:31:40

标签: spring-boot rabbitmq spring-rabbit spring-rabbitmq

我有一个带有兔子发送器和接收器的简单弹簧启动应用程序。我想编写一些接收机测试,其中我运行一个 rabbitmq docker 实例作为Junit类规则(RabbitContainerRule),然后使用rabbitTemplate发送消息,测试验证接收方是否收到相同的消息信息。但我得到以下例外:

Caused by: org.springframework.context.ApplicationContextException: Failed to start bean 'org.springframework.amqp.rabbit.config.internalRabbitListenerEndpointRegistry'; nested exception is org.springframework.amqp.AmqpIllegalStateException: Fatal exception on listener startup
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)

Caused by: org.springframework.amqp.rabbit.listener.QueuesNotAvailableException: Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it.
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:599)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1424)

Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'my-message-queue' in vhost '/', class-id=50, method-id=10)
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)

如果我使用管理控制台在docker实例中手动创建队列(通过在断点处停止),我的测试将通过。

另外,如果我使用docker rabbit实例手动测试它,我的spring启动应用程序会成功创建队列。那么是什么导致它不能在测试中创建呢?

我正在使用 spring-amqp 1.7.4 RELEASE

收件人代码:

@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "my-message-queue", durable = "true",
                arguments = {
                        @Argument(name = "x-dead-letter-exchange", value = "my-message-exchange-dead-letter"),
                        @Argument(name = "x-dead-letter-routing-key", value = "my-message-queue")}),
        exchange = @Exchange(value = "my-message-exchange", type = "topic", durable = "true"),
        key = "my-message-rk")
)
public void handleMessage(MyMessage message) {
    MESSAGE_LOG.info("Receiving message: " + message);
}

此外,我不是在Configurations中为 my-message-queue 创建任何@Bean,而是依靠 @RabbitListener 为我创建一个。但是我在配置中创建了ConnectionFactory,RabbitTemplate和SimpleRabbitListenerContainerFactory bean。

2 个答案:

答案 0 :(得分:1)

某些@EnableRabbit类需要@Configuration才能让您的应用程序上下文解析@RabbitListener

让应用程序自动创建队列并在它们之间进行交换和绑定,并且RabbitAdmin bean必须存在于配置中。

有关详情,请参阅参考手册:https://docs.spring.io/spring-amqp/docs/2.0.0.RELEASE/reference/html/

答案 1 :(得分:0)

用于构建队列的类应使用@Configuration注释进行注释,否则,spring在启动时将无法创建队列