title: Why ActiveMQ Artemis auto delete address if all listeners are destroyed?
body: <p>We are develop a micro-service system that use ActiveMQ Artemis as the communication method between service. Since the requirement ask to be able to stop the listeners at runtime, we can not use <code>@JmsListener</code> provide by spring-artemis. After digging the internet and finding out that spring use <code>MessageListenerContainer</code> behind the scence, we come up with the idea of maintain a list of <code>MessageListenerContainer</code> our self.</p>
  <pre class="lang-java prettyprint-override"><code>    @Bean(name = &quot;commandJmsListenerContainerFactory&quot;)
      public DefaultJmsListenerContainerFactory commandJmsListenerContainerFactory(
          DefaultJmsListenerContainerFactoryConfigurer configurer) {
          var factory = new DefaultJmsListenerContainerFactory();
          configurer.configure(factory, connectionFactory);
          factory.setPubSubDomain(false);
          return factory;
      }
  
      // Use
      private Map&lt;String, DefaultMessageListenerContainer&gt; commandQueue;
  
      public void subscribeToCommandQueue(String queueName, CommandListener&lt;?&gt; command) {
          commandQueue.computeIfAbsent(queueName, key -&gt; {
              var endPoint = new SimpleJmsListenerEndpoint();
              endPoint.setDestination(queueName);
              endPoint.setMessageListener(message -&gt; {
                  try {
                      var body = message.getBody(String.class);
                      command.execute(commandMessageConverter.deserialize(body));
                  } catch (JMSException e) {
                      throw new RuntimeException(&quot;Error while process message for queue: &quot; + queueName, e);
                  }
              });
              var container = commandJmsListenerContainerFactory.createListenerContainer(endPoint);
              // https://stackoverflow.com/questions/44555106/defaultmessagelistenercontainer-not-reading-messages-from-ibm-mq
              // for Every object of Spring classes that implement InitializingBean created manually, we need to call afterPropertiesSet to make the object &quot;work&quot;
              container.afterPropertiesSet();
              container.start();
              return container;
          });
      }
  
      public void start() {
          commandQueue = new ConcurrentHashMap&lt;&gt;();
      }
  
      public void stop() {
          commandQueue.values().forEach(DefaultMessageListenerContainer::destroy);
          commandQueue.clear();
      }
  </code></pre>
  <p>While testing, I notice that after we destroy all the listener by calling <code>stop()</code> , the queue and the address in the Artemis console are deleted too. It isn't the case for durable subscription.</p>
  <pre class="lang-java prettyprint-override"><code>    @Bean(name = &quot;eventJmsListenerContainerFactory&quot;)
      public DefaultJmsListenerContainerFactory eventJmsListenerContainerFactory(
          CachingConnectionFactory cachingConnectionFactory,
          DefaultJmsListenerContainerFactoryConfigurer configurer) {
          cachingConnectionFactory.setClientId(UUID.randomUUID().toString());
          var factory = new DefaultJmsListenerContainerFactory();
          configurer.configure(factory, cachingConnectionFactory);
          factory.setPubSubDomain(true);
          factory.setSubscriptionDurable(true);
          return factory;
      }
  
      // usage is the same as the first block code, except we store multicast subscriptions in another map
      private Map&lt;String, DefaultMessageListenerContainer&gt; eventTopic;
  </code></pre>
  <p>After running the unit tests and destroying all the listeners of two maps, only the <code>test-event-topic</code> address and its queues were kept, the <code>test-command-queue</code> was deleted. Why both the queues behave differently?</p>
  <p><a href="https://i.stack.imgur.com/awCKF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/awCKF.png" alt="enter image description here" /></a></p>
  <p>Also, what is the correct behavior? We afraid the auto deletion will remove messages that aren't sent in the queue yet. On the other hand, new queue under <code>test-event-topic</code> keep being created if we run the test again and again. I think it is because of the line <code>cachingConnectionFactory.setClientId(UUID.randomUUID().toString());</code> . But for durable subscription, not setting clientId result in error.</p>
  <p>The connection factory used in the app is an <code>CachingConnectionFactory</code>  created by spring-artemis</p>