title: Spring AMQP: Creating multiple queues dynamically
body: <p>I have a Spring @Configuration class that creates and registers RabbitMQ queues as follows:</p>
  
  <pre><code>@Amqp
  @Configuration
  public class AmqpConfiguration {
  
  @Bean
  ConnectionFactory connectionFactory() {
      CachingConnectionFactory connectionFactory = new CachingConnectionFactory(HOST);
      connectionFactory.setUsername(USERNAME);
      connectionFactory.setPassword(PASSWORD);
      connectionFactory.addConnectionListener(brokerListener);
      return connectionFactory;
  }
  
  @Bean
  SimpleMessageListenerContainer messageListenerContainer2(ConnectionFactory connectionFactory) {
      SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
      container.setConnectionFactory(connectionFactory);
      container.setQueues(activityQueue());
      container.setMessageListener(listener);
      container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
      container.setPrefetchCount(Props.getInt(ACTIVITY_QUEUE_PREFETCH_COUNT));
      return container;
  }
  
  @Bean
  AmqpAdmin amqpAdmin() {
      return new RabbitAdmin(connectionFactory());
  }
  
  @Bean
   Queue activityQueue() {
      return new Queue(ACITVITY_QUEUE);
  }
  </code></pre>
  
  <p>So far so good. I can create an activityQueue with no issue. </p>
  
  <p>However, now I need to create multiple activity queues dynamically. So I changed my code as follows (only relevant section shown):</p>
  
  <pre><code> @Bean
  SimpleMessageListenerContainer messageListenerContainer2(ConnectionFactory connectionFactory) {
      SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
      container.setConnectionFactory(connectionFactory);
      container.setQueues(getAllQueues());
      container.setMessageListener(listener);
      container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
      container.setPrefetchCount(Props.getInt(ACTIVITY_QUEUE_PREFETCH_COUNT));
      return container;
  }
  
  @Bean
  Queue[] getAllQueues() {
     // Detail omitted
      return queues.toArray(new Queue[queues.size()]);
  }
  </code></pre>
  
  <p>However, returning the array of queues doesn't help and it throws the following exception:</p>
  
  <pre><code>[Thread=SimpleAsyncTaskExecutor-1] 2019-01-24 10:26:04,112 WARN : org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Reconnect failed; retries left=2
  java.io.IOException
  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:790)
  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:365)
  at com.sun.proxy.$Proxy168.queueDeclarePassive(Unknown Source)
  at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:237)
  at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:534)
  at java.lang.Thread.run(Thread.java:748)
  
  Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method&lt;channel.close&gt;(reply-code=404, reply-text=NOT_FOUND - no queue 'activityQueue-F865FD8C-7AB2ABAC-153D0A6B-5BE36001-10A075A6' in vhost '/', class-id=50, method-id=10), null, ""}
  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:343)
  at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216)
  at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118)
  ... 11 more
  </code></pre>
  
  <p>So  my question is how can I create queues dynamically such as activityQueueA, activityQueueB, activityQueueC and so on?</p>