在spring-boot中使用rabbitmq时,在vhost'/'中没有队列'dev_pms2invoi_queue'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在spring-boot中使用rabbitmq时,在vhost'/'中没有队列'dev_pms2invoi_queue'相关的知识,希望对你有一定的参考价值。

我想在spring-boot项目中使用rabbitmq只是使用注释,这是我的mq配置代码,

@Configuration
public class RabbitMqConfig {
    @Bean(value = "pmsMqConnectionFactory")
    public ConnectionFactory pmsMqConnectionFactory(){
        CachingConnectionFactory connectionFactory=new CachingConnectionFactory();
        connectionFactory.setAddresses(pmsMqServerAddress);
        connectionFactory.setVirtualHost(pmsMqServerVirtualHost);
        connectionFactory.setUsername(pmsMqServerUserName);
        connectionFactory.setPassword(pmsMqServerPassword);
        return connectionFactory;
    }
    @Bean(value = "pmsConsumerQueue")
    public Queue pmsConsumerQueue(){
        Queue consumerQueue=new Queue(pmsMqConsumerQueue,pmsMqQueuePersist);

        return consumerQueue;
    }
    @Bean(value = "pmsConsumerExchange")
    public Exchange pmsConsumerExchange(){
        Exchange consumerExchange=new DirectExchange(pmsMqConsumerExchangeName,pmsMqQueuePersist,false);
        return consumerExchange;
    }
    @Bean(value = "pmsConsumerMessageBinding")
    public Binding pmsConsumerMessageBinding(@Qualifier("pmsConsumerQueue") Queue queue,
                                                  @Qualifier("pmsConsumerExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
    @Bean(value = "pmsMessageListenerContainer")
    public SimpleMessageListenerContainer pmsMessageListenerContainer(@Qualifier("pmsMqConnectionFactory") ConnectionFactory connectionFactory,
                                                                      @Qualifier("pmsMqConsumer") PmsMqConsumer pmsMqConsumer,@Qualifier("pmsConsumerQueue")Queue queue){
        SimpleMessageListenerContainer container=new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setMessageListener(pmsMqConsumer);
        container.setQueues(queue);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return container;
    }
}

当我启动应用程序时,会发生错误:

org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: Failed to declare queue(s):[dev_pms2invoi_queue]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:636)
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:535)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1389)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.IOException: null
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:105)
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:101)
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:123)
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:992)
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:50)
    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:955)
    at com.sun.proxy.$Proxy155.queueDeclarePassive(Unknown Source)
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:615)
    ... 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 'dev_pms2invoi_queue' in vhost '/', class-id=50, method-id=10)
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)
    at  

它说没有队列,但如何使用注释创建它?我的代码中有错误吗?

答案

由于您已声明自己的连接工厂,因此需要声明使用该连接工厂的RabbitAdmin @Bean

如果您使用boot的属性驱动配置,它将为您自动配置连接工厂和RabbitAdmin

以上是关于在spring-boot中使用rabbitmq时,在vhost'/'中没有队列'dev_pms2invoi_queue'的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot 集成 rabbitmq

Spring-boot集成RabbitMQ踩过的坑

spring-boot-route(十三)整合RabbitMQ

在 spring-boot 中使用 @EnableWebFluxSecurity 时出错

一个基于RabbitMQ的可复用的事务消息方案

SpringBoot系列5SpringBoot整合RabbitMQ