RabbitMQ 无法声明队列并且监听器无法在服务器上获取队列

Posted

技术标签:

【中文标题】RabbitMQ 无法声明队列并且监听器无法在服务器上获取队列【英文标题】:RabbitMQ Failed to declare queue and Listener is not able to get queue on server 【发布时间】:2018-08-26 07:33:12 【问题描述】:

我有 spring boot rabbitmq 应用程序,我必须将 Employee 对象发送到队列。然后我设置了一个监听器应用程序。对员工对象进行一些处理,并将该对象放入回调队列中。

为此,我在我的应用程序中创建了以下对象。

    已创建 ConnectionFactory。 使用 ConnectionFactory.. 创建了 RabbitAdmin 对象 请求队列。 回调队列。 直接交换。 请求队列绑定。 回调队列绑定。 消息转换器。 RabbitTemplate 对象。 最后是 SimpleMessageListenerContainer 的对象。

我的应用程序文件如下所示。

application.properties

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=foo
emp.rabbitmq.directexchange=EMP_EXCHANGE1
emp.rabbitmq.requestqueue=EMP_QUEUE1
emp.rabbitmq.routingkey=EMP_ROUTING_KEY1

MainClass.java

package com.employee;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainClass 

    public static void main(String[] args) 
        SpringApplication.run(
                MainClass.class, args);
    

ApplicationContextProvider.java

package com.employee.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;

public class ApplicationContextProvider implements ApplicationContextAware 
    private static ApplicationContext context;

    public ApplicationContext getApplicationContext()
        return context;
    

    @Override
    public void setApplicationContext(ApplicationContext arg0) throws BeansException 
        context = arg0;

    

    public Object getBean(String name)
        return context.getBean(name, Object.class);
    

    public void addBean(String beanName, Object beanObject)
        ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext)context).getBeanFactory();
        beanFactory.registerSingleton(beanName, beanObject);
    

    public void removeBean(String beanName)
        BeanDefinitionRegistry reg = (BeanDefinitionRegistry) context.getAutowireCapableBeanFactory();
        reg.removeBeanDefinition(beanName);
    

常量.java

package com.employee.constant;

public class Constants 

    public static final String CALLBACKQUEUE = "_CBQ";


Employee.java

package com.employee.model;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id", scope = Employee.class)
public class Employee 

    private String empName;
    private String empId;
    private String changedValue;
    public String getEmpName() 
        return empName;
    
    public void setEmpName(String empName) 
        this.empName = empName;
    
    public String getEmpId() 
        return empId;
    
    public void setEmpId(String empId) 
        this.empId = empId;
    
    public String getChangedValue() 
        return changedValue;
    
    public void setChangedValue(String changedValue) 
        this.changedValue = changedValue;
    



EmployeeProducerInitializer.java

package com.employee.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.employee.constant.Constants;
import com.employee.service.EmployeeResponseReceiver;

@Configuration
@EnableAutoConfiguration
@ComponentScan(value="com.en.*")
public class EmployeeProducerInitializer 

    @Value("$emp.rabbitmq.requestqueue")
    String requestQueueName;

    @Value("$emp.rabbitmq.directexchange")
    String directExchange;

    @Value("$emp.rabbitmq.routingkey")
    private String requestRoutingKey;

    @Autowired
    private ConnectionFactory rabbitConnectionFactory;

    @Bean
    ApplicationContextProvider applicationContextProvider()
        System.out.println("inside app ctx provider");
        return new ApplicationContextProvider();
    ;

    @Bean
    RabbitAdmin rabbitAdmin()
        System.out.println("inside rabbit admin");
        return new RabbitAdmin(rabbitConnectionFactory);
    ;

    @Bean
    Queue empRequestQueue() 
        System.out.println("inside request queue");
        return new Queue(requestQueueName, true);
    

    @Bean
    Queue empCallBackQueue() 
        System.out.println("inside call back queue");
        return new Queue(requestQueueName + Constants.CALLBACKQUEUE, true);
    

    @Bean
    DirectExchange empDirectExchange() 
        System.out.println("inside exchange");
        return new DirectExchange(directExchange);
    

    @Bean
    Binding empRequestBinding() 
        System.out.println("inside request binding");
        return BindingBuilder.bind(empRequestQueue()).to(empDirectExchange()).with(requestRoutingKey);
    

    @Bean
    Binding empCallBackBinding() 
        return BindingBuilder.bind(empCallBackQueue()).to(empDirectExchange()).with(requestRoutingKey + Constants.CALLBACKQUEUE);
    

    @Bean
    public MessageConverter jsonMessageConverter()
        System.out.println("inside json msg converter");
        return new Jackson2JsonMessageConverter();
    

    @Bean
    public RabbitTemplate empFixedReplyQRabbitTemplate() 
        System.out.println("inside rabbit template");
        RabbitTemplate template = new RabbitTemplate(this.rabbitConnectionFactory);
        template.setExchange(empDirectExchange().getName());
        template.setRoutingKey(requestRoutingKey);
        template.setMessageConverter(jsonMessageConverter());
        template.setReceiveTimeout(100000);
        template.setReplyTimeout(100000);

        return template;
    

    @Bean
    public SimpleMessageListenerContainer empReplyListenerContainer() 
        System.out.println("inside listener");
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        try
            container.setConnectionFactory(this.rabbitConnectionFactory);
            container.setQueues(empCallBackQueue());
            container.setMessageListener(new EmployeeResponseReceiver());
            container.setMessageConverter(jsonMessageConverter());
            container.setConcurrentConsumers(10);
            container.setMaxConcurrentConsumers(20);
            container.start();
        catch(Exception e)
            e.printStackTrace();
        finally
            System.out.println("inside listener finally");
        

        return container;
    

    @Autowired
    @Qualifier("empReplyListenerContainer")
    private SimpleMessageListenerContainer empReplyListenerContainer;

EmployeeResponseReceiver.java

package com.employee.service;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Component;

import com.employee.config.ApplicationContextProvider;
import com.employee.model.Employee;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;

@Component
@EnableAutoConfiguration
public class EmployeeResponseReceiver implements ChannelAwareMessageListener 

    ApplicationContextProvider applicationContextProvider = new ApplicationContextProvider();

    String msg = null;
    ObjectMapper mapper = new ObjectMapper();
    Employee employee = null;

    @Override
    public void onMessage(Message message, Channel arg1) throws Exception 
        try 
            msg = new String(message.getBody());
            System.out.println("Received Message : " + msg);

            employee = mapper.readValue(msg, Employee.class);
         catch (Exception e) 
            e.printStackTrace();
        
    


问题是每当我启动我的应用程序时,我都会遇到异常。

2018-03-17 14:18:36.695  INFO 12472 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-03-17 14:18:36.696  INFO 12472 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5060 ms
2018-03-17 14:18:37.004  INFO 12472 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-03-17 14:18:37.010  INFO 12472 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-03-17 14:18:37.010  INFO 12472 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-03-17 14:18:37.011  INFO 12472 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-03-17 14:18:37.011  INFO 12472 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
inside listener
inside call back queue
inside json msg converter
2018-03-17 14:18:37.576  INFO 12472 --- [cTaskExecutor-8] o.s.a.r.c.CachingConnectionFactory       : Created new connection: SimpleConnection@3d31af39 [delegate=amqp://guest@127.0.0.1:5672/foo, localPort= 50624]
2018-03-17 14:18:37.654  WARN 12472 --- [cTaskExecutor-7] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:EMP_QUEUE1_CBQ
2018-03-17 14:18:37.655  WARN 12472 --- [cTaskExecutor-6] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:EMP_QUEUE1_CBQ
2018-03-17 14:18:37.655  WARN 12472 --- [cTaskExecutor-5] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:EMP_QUEUE1_CBQ
2018-03-17 14:18:37.655  WARN 12472 --- [cTaskExecutor-3] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:EMP_QUEUE1_CBQ
2018-03-17 14:18:37.657  WARN 12472 --- [cTaskExecutor-1] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:EMP_QUEUE1_CBQ
2018-03-17 14:18:37.658  WARN 12472 --- [cTaskExecutor-8] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:EMP_QUEUE1_CBQ
2018-03-17 14:18:37.661  WARN 12472 --- [cTaskExecutor-2] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:EMP_QUEUE1_CBQ
2018-03-17 14:18:37.660  WARN 12472 --- [cTaskExecutor-4] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:EMP_QUEUE1_CBQ
2018-03-17 14:18:37.661  WARN 12472 --- [cTaskExecutor-9] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:EMP_QUEUE1_CBQ
2018-03-17 14:18:37.666  WARN 12472 --- [TaskExecutor-10] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:EMP_QUEUE1_CBQ
2018-03-17 14:18:37.667  WARN 12472 --- [cTaskExecutor-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):[EMP_QUEUE1_CBQ]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:636) ~[spring-rabbit-1.7.2.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:535) ~[spring-rabbit-1.7.2.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1389) [spring-rabbit-1.7.2.RELEASE.jar:na]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_151]
Caused by: java.io.IOException: null
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:105) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:101) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:123) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:992) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:50) ~[amqp-client-4.0.2.jar:4.0.2]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151]
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:955) ~[spring-rabbit-1.7.2.RELEASE.jar:na]
    at com.sun.proxy.$Proxy58.queueDeclarePassive(Unknown Source) ~[na:na]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:615) ~[spring-rabbit-1.7.2.RELEASE.jar:na]
    ... 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 'EMP_QUEUE1_CBQ' in vhost 'foo', class-id=50, method-id=10)
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:32) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:366) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:229) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:117) ~[amqp-client-4.0.2.jar:4.0.2]
    ... 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 'EMP_QUEUE1_CBQ' in vhost 'foo', class-id=50, method-id=10)
    at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:505) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:336) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:143) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:90) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection.readFrame(AMQConnection.java:634) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection.access$300(AMQConnection.java:47) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:572) ~[amqp-client-4.0.2.jar:4.0.2]
    ... 1 common frames omitted

2018-03-17 14:08:36.689  WARN 11076 --- [cTaskExecutor-4] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:EMP_QUEUE1_CBQ
2018-03-17 14:08:36.695 ERROR 11076 --- [cTaskExecutor-4] o.s.a.r.l.SimpleMessageListenerContainer : Consumer received fatal exception on startup

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:563) ~[spring-rabbit-1.7.2.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1389) ~[spring-rabbit-1.7.2.RELEASE.jar:na]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_151]
Caused by: org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: Failed to declare queue(s):[EMP_QUEUE1_CBQ]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:636) ~[spring-rabbit-1.7.2.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:535) ~[spring-rabbit-1.7.2.RELEASE.jar:na]
    ... 2 common frames omitted
Caused by: java.io.IOException: null
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:105) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:101) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:123) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:992) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:50) ~[amqp-client-4.0.2.jar:4.0.2]
    at sun.reflect.GeneratedMethodAccessor27.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151]
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:955) ~[spring-rabbit-1.7.2.RELEASE.jar:na]
    at com.sun.proxy.$Proxy58.queueDeclarePassive(Unknown Source) ~[na:na]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:615) ~[spring-rabbit-1.7.2.RELEASE.jar:na]
    ... 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 'EMP_QUEUE1_CBQ' in vhost 'foo', class-id=50, method-id=10)
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:32) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:366) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:229) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:117) ~[amqp-client-4.0.2.jar:4.0.2]
    ... 11 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 'EMP_QUEUE1_CBQ' in vhost 'foo', class-id=50, method-id=10)
    at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:505) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:336) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:143) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:90) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection.readFrame(AMQConnection.java:634) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection.access$300(AMQConnection.java:47) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:572) ~[amqp-client-4.0.2.jar:4.0.2]
    ... 1 common frames omitted

2018-03-17 14:08:36.697  INFO 11076 --- [TaskExecutor-10] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
2018-03-17 14:08:36.699  INFO 11076 --- [cTaskExecutor-5] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
2018-03-17 14:08:36.700  INFO 11076 --- [cTaskExecutor-1] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
2018-03-17 14:08:36.701  INFO 11076 --- [cTaskExecutor-3] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
2018-03-17 14:08:36.700  INFO 11076 --- [cTaskExecutor-2] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
2018-03-17 14:08:36.702  INFO 11076 --- [cTaskExecutor-7] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
2018-03-17 14:08:36.765 ERROR 11076 --- [cTaskExecutor-8] o.s.a.r.l.SimpleMessageListenerContainer : Stopping container from aborted consumer
2018-03-17 14:08:36.766 ERROR 11076 --- [cTaskExecutor-6] o.s.a.r.l.SimpleMessageListenerContainer : Stopping container from aborted consumer
2018-03-17 14:08:36.779 ERROR 11076 --- [cTaskExecutor-9] o.s.a.r.l.SimpleMessageListenerContainer : Stopping container from aborted consumer
2018-03-17 14:08:36.791 ERROR 11076 --- [cTaskExecutor-4] o.s.a.r.l.SimpleMessageListenerContainer : Stopping container from aborted consumer
inside app ctx provider
inside rabbit admin
inside exchange
inside request queue
inside request binding
inside rabbit template
2018-03-17 14:08:38.978  INFO 11076 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@33b37288: startup date [Sat Mar 17 14:08:16 IST 2018]; root of context hierarchy
2018-03-17 14:08:39.395  INFO 11076 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error]" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-03-17 14:08:39.398  INFO 11076 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error],produces=[text/html]" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-03-17 14:08:39.663  INFO 11076 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-17 14:08:39.663  INFO 11076 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-17 14:08:39.826  INFO 11076 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-17 14:08:40.648  INFO 11076 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-03-17 14:08:40.677  INFO 11076 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'rabbitConnectionFactory' has been autodetected for JMX exposure
2018-03-17 14:08:40.685  INFO 11076 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'rabbitConnectionFactory': registering with JMX server as MBean [org.springframework.amqp.rabbit.connection:name=rabbitConnectionFactory,type=CachingConnectionFactory]
2018-03-17 14:08:40.746  INFO 11076 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase -2147482648
2018-03-17 14:08:40.747  INFO 11076 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
2018-03-17 14:08:41.258  INFO 11076 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-03-17 14:08:41.270  INFO 11076 --- [           main] com.employee.MainClass                   : Started MainClass in 26.141 seconds (JVM running for 28.02)

谁能帮我解决我的问题?据我了解,在创建 SimpleMessageListenerContainer 对象时,不会在 rabbitmq 服务器上创建回调队列。我尝试使用 RabbitAdmin 对象声明队列,但随后执行停止并且没有任何进展。当我在默认虚拟主机中声明队列时,此问题不存在。但是当我添加虚拟主机 foo 时,它突然停止工作。您可以使用上述代码复制此问题。我已经粘贴了我所有的代码。如果要发布其他内容,请告诉我。

有趣的一点是,即使我遇到了这个异常,我的应用程序也已启动并正在运行。这意味着以某种方式创建了我的回调队列,并且 SimpleMessageListenerContainer 的对象获取了该队列。我在某处读到,当创建队列时,我的 SimpleMessageListenerContainer 对象会监听它。

请帮我解决这个问题。

【问题讨论】:

【参考方案1】:

当我在默认虚拟主机中声明队列时,这个问题不存在。但是当我添加虚拟主机 foo 时,它突然停止工作。

访问新虚拟主机的用户是否拥有configure 权限?声明队列需要配置权限。

需要一个 RabbitAdmin 来声明队列/绑定;容器只做一个被动声明来检查队列是否存在。

编辑

container.start();

你不能start() bean 定义中的容器。如果容器的autoStartUptrue(默认),应用上下文将在应用上下文完全构建后执行此操作。

从您的日志中可以清楚地看出,容器启动得太早了——在声明其他 bean(管理员、队列等)之前。

【讨论】:

是的,先生。用户有权以管理员权限声明队列。让我知道您是否可以使用提供的代码复制该问题。 问题是你启动容器太早了——看我的编辑。 嗨,加里。谢谢。我得到了它。我删除了 start 方法调用及其现在的工作。我对此进行了更多搜索,当声明 bean 时,队列是被动创建的。当我的应用程序 ctx 正确加载时,它将激活。

以上是关于RabbitMQ 无法声明队列并且监听器无法在服务器上获取队列的主要内容,如果未能解决你的问题,请参考以下文章

Rabbitmq无法监听后续消息

在IIS托管的WCF服务中使用RabbitMQ Queue

RabbitMQ消息队列(十三)-VirtualHost与权限管理

RabbitMQ 高可用之镜像队列

RabbitMQ 高可用之镜像队列

RabbitMQ 高可用之镜像队列