Search code examples
activemq-artemis

Empty queue do not auto delete in ActiveMQ Artemis


My Java code:

ArtemisContext artemisContext = WrapperContext.getInstance().getArtemisContext();
ClientSession session = artemisContext.getSession();

String myQueue = WrapperContext.getInstance().getMyId();
try {
    session.createQueue(myQueue, RoutingType.ANYCAST, myQueue,false);
} catch (Exception e) {
    //ignore
}
ClientConsumer consumer = session.createConsumer(myQueue);
consumer.setMessageHandler(message -> {
    ActiveMQBuffer bodyBuffer = message.getBodyBuffer();
    byte[] bytes = new byte[bodyBuffer.readableBytes()];
    try {
        bodyBuffer.readFully(bytes);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String msg = new String(bytes);
    message.getBodyBuffer().writeBytes(msg.getBytes());
    log.info("=====comsume:" + msg);
    listenerHandler.handler(msg);
});
WrapperContext.getInstance().getArtemisContext().setConsumer(consumer);

When I start my Java application I will call this code and generate a queue in ActiveMQ Artemis as follows:

enter image description here

And my broker.xml for ActiveMQ Artemis:

<address-settings>
   <!-- if you define auto-create on certain queues, management has to be auto-create -->
   <address-setting match="activemq.management#">
      <dead-letter-address>DLQ</dead-letter-address>
      <expiry-address>ExpiryQueue</expiry-address>
      <redelivery-delay>0</redelivery-delay>
      <!-- with -1 only the global-max-size is in use for limiting -->
      <max-size-bytes>-1</max-size-bytes>
      <message-counter-history-day-limit>10</message-counter-history-day-limit>
      <address-full-policy>PAGE</address-full-policy>
      <auto-create-queues>true</auto-create-queues>
      <auto-create-addresses>true</auto-create-addresses>
      <auto-create-jms-queues>true</auto-create-jms-queues>
      <auto-create-jms-topics>true</auto-create-jms-topics>
   </address-setting>
   <!--default for catch all-->
   <address-setting match="#">
      <dead-letter-address>DLQ</dead-letter-address>
      <expiry-address>ExpiryQueue</expiry-address>
      <redelivery-delay>0</redelivery-delay>
      <!-- with -1 only the global-max-size is in use for limiting -->
      <max-size-bytes>-1</max-size-bytes>
      <message-counter-history-day-limit>10</message-counter-history-day-limit>
      <address-full-policy>PAGE</address-full-policy>
      <auto-create-queues>true</auto-create-queues>
      <auto-create-addresses>true</auto-create-addresses>
      <auto-create-jms-queues>true</auto-create-jms-queues>
      <auto-create-jms-topics>true</auto-create-jms-topics>
      <auto-delete-addresses>true</auto-delete-addresses>
      <auto-delete-addresses-delay>0</auto-delete-addresses-delay>
      <auto-delete-queues>true</auto-delete-queues>
      <auto-delete-queues-delay>0</auto-delete-queues-delay>
   </address-setting>
</address-settings>

I configured the auto-delete-queues as true. I thought queue would automatically delete it when I shut down my service. But after I shut down the service, the queue still exists, although there are no messages in it, and the consumer is 0, and Durable=false

enter image description here

At first I thought it was because of RoutingType.MULTICAST so I changed it to ANYCAST, but the result was still the same.

So is there something wrong with my code or is there something wrong with the configuration?

My ActiveMQ Artemis version is 2.17.0.


Solution

  • The broker will only auto-delete queues which have been marked as auto-created. The queue that you're creating is not marked as auto-created since you're creating it manually (i.e. the broker isn't creating it automatically for you) and you're not configuring it as auto-created. Try doing this instead:

    session.createQueue(myQueue, RoutingType.ANYCAST, myQueue, null, false, true);
    

    Also, I strongly recommend you upgrade to the latest version. Version 2.17.0 was released in early 2021 over three years ago now. There's been 15 releases since then with many bug fixes and new features (including a new createQueue API that's more flexible and clear).