Independent ActiveMQ handbook · archive of the 2004–2007 project site
Independent guide. Not the Apache ActiveMQ project site and not affiliated with the Apache Software Foundation. ActiveMQ and Apache are trademarks of the ASF. Official documentation: activemq.apache.org.

activemq.org › Handbook › What is a message queue? Queues, topics and publish/subscribe

Handbook

What is a message queue? Queues, topics and publish/subscribe

Checked against ActiveMQ Classic 6.4.0, 6.3.3 and 5.19.12 and Apache Artemis 2.57.0 on . Independent guide; sources are listed at the end. Official documentation: activemq.apache.org and artemis.apache.org.

A message queue is a destination on a message broker where producers leave messages and consumers pick them up later, so the sender and the receiver never have to be running at the same moment. Each message on a queue goes to exactly one consumer; when every interested receiver needs its own copy, you use a topic (publish/subscribe) instead.

This page explains the two messaging styles in Jakarta Messaging (JMS) terms, how queues and topics differ, what durable subscriptions are, and how acknowledgement modes decide when a message counts as consumed. ActiveMQ supplies the concrete details. The official documentation lives on activemq.apache.org for ActiveMQ Classic and on artemis.apache.org for Apache Artemis (formerly ActiveMQ Artemis).

How does a message queue work?

A producer sends a message to a named queue on the broker. The broker stores it until a consumer that is reading from that queue takes it. The ActiveMQ FAQ describes a JMS queue as having load-balancer semantics: a single message is received by exactly one consumer, and if no consumer is available when the message is sent, the broker keeps it until one is.

Several consumers can read from the same queue, and the broker spreads the messages across them. That is how you scale out workers: add consumers and each one takes a share of the backlog. If a consumer receives a message and closes without acknowledging it, the broker redelivers that message to another consumer.

This decoupling is the reason to put a queue between two programs:

  • The producer does not need to know who consumes the message, or how many consumers exist.
  • Consumers can be stopped, slow or restarted while messages wait in the queue.
  • Bursts of work collect in the queue instead of overloading the receiver.

The broker is the server that holds the queues. For what a broker does beyond queuing, see What is a message broker.

Point-to-point vs publish/subscribe

The Jakarta Messaging specification, which still carries the APIs introduced in JMS 1.0, 1.1 and 2.0, defines two styles of messaging:

  • Point-to-point (PTP): a client sends a message to a queue, and the receiving client extracts it from that queue. A queue may have more than one consumer, but each message in the queue is delivered to only one of them.
  • Publish/subscribe (pub/sub): a client publishes a message to a topic, and the message is delivered to all the clients subscribed to that topic.

Since Jakarta Messaging 2.0, every provider must implement both styles, and one application can combine them. In code the difference is mostly which destination you pass: a Queue or a Topic, both subtypes of Destination.

Queues vs topics in JMS

Queue (point-to-point)Topic (publish/subscribe)
Who receives a messageExactly one consumerEvery subscription on the topic gets a copy
No consumer connected when the message arrivesThe broker keeps the message until a consumer is availableOnly subscriptions that exist when the broker receives the message get a copy; a durable subscription keeps messages while its consumer is offline
Several consumersMessages are load-balanced across themEach unshared subscription receives every message; a shared subscription (Jakarta Messaging 2.0) splits one subscription's messages across its consumers
Typical fitJobs and commands that one worker should handleEvents that several independent systems react to

What is a durable subscription?

An ordinary topic subscription exists only while its consumer is open. Messages published while the subscriber is disconnected are not kept for it. A durable subscription changes that: the broker keeps every message published to the topic for that subscription until a consumer on the subscription receives and acknowledges it, or until the message expires.

Picture a subscriber that runs, stops for an hour, then starts again. With a durable subscription it receives the messages published during that hour; with a non-durable one it never sees them. The price is storage: a durable subscription keeps accumulating messages until it is deleted with unsubscribe, whether or not anyone is consuming.

To find the subscription again after a restart, the provider identifies an unshared durable subscription by a name your client chooses plus the connection's client identifier, which must be set. Keep both stable across restarts. An unshared durable subscription may have only one active consumer at a time.

Shared subscriptions in ActiveMQ Classic and Artemis

Jakarta Messaging 2.0 added shared subscriptions, durable and non-durable. Several consumers attach to one subscription and each message goes to only one of them, which gives you load balancing on a topic.

ActiveMQ Classic's Jakarta Messaging 3.1 and JMS 2.0 support is partial. Its JMS 2.0 support page says the client throws UnsupportedOperationException for shared topic consumers and lists that feature with a target version of 7.0.0. Classic's long-standing alternative is virtual topics: producers send to a topic such as VirtualTopic.Orders, and each consuming system reads from its own queue, such as Consumer.A.VirtualTopic.Orders, where many consumers can compete for the messages.

Apache Artemis has full Jakarta Messaging 3.1, JMS 2.0 and JMS 1.1 support, so shared subscriptions work there through the standard API. For the wider differences between the two brokers, see ActiveMQ Classic vs Artemis.

Acknowledgement modes

Acknowledgement is how a consumer tells the broker that a message has been handled, so the broker can drop it. Jakarta Messaging gives non-transacted sessions three modes; transacted sessions acknowledge on commit.

ModeWhen the message counts as acknowledgedWhat to watch
AUTO_ACKNOWLEDGEWhen receive() returns, or when the message listener returns successfullyAfter a failure the last consumed message may be delivered again
CLIENT_ACKNOWLEDGEWhen your code calls acknowledge() on a messageAcknowledging one message acknowledges every message the session has delivered so far
DUPS_OK_ACKNOWLEDGELazily, at the session's discretionDuplicates are likely after a failure; use only where duplicates are harmless
Transacted sessionOn commit(); rollback() recovers the consumed messagesPersistent messages make the commit wait for the disk

With CLIENT_ACKNOWLEDGE, calling recover() on the session restarts delivery from the first unacknowledged message. Messages delivered again carry the JMSRedelivered header, and the JMSXDeliveryCount property is incremented.

The ActiveMQ FAQ points out that JMS has no "unacknowledge", so it recommends JMS transactions over explicit acknowledgement in most cases: a rollback puts the messages back for redelivery. It also says that committing a batch of messages in one transaction, rather than one message per commit, is the fastest way to consume persistent messages.

When a message keeps failing, the broker eventually stops redelivering it and moves it to a dead letter queue. How ActiveMQ decides that is covered in Dead letter queue.

Queue or topic: how to choose

  • Use a queue when each message is a piece of work that one worker should do once.
  • Use a topic when several independent parts of a system each need to react to the same event.
  • Use a durable subscription when a subscriber must not miss events while it is offline.
  • Use a shared durable subscription (Artemis, or any provider with full Jakarta Messaging 2.0 support) or a virtual topic (ActiveMQ Classic) when each subscribing system also needs several competing consumers.

For working code against ActiveMQ, see the ActiveMQ tutorial.

Common problems

  • Messages pile up in a queue. A queue keeps messages until a consumer takes them, so a growing count means no consumer is connected or consumers are too slow. Check consumer counts with the tools in ActiveMQ monitoring.
  • A restarted durable subscriber receives nothing. The ActiveMQ FAQ gives the fix: messages for offline durable subscribers must be sent as persistent, so set the PERSISTENT delivery mode on the producer. Also confirm the client ID and subscription name did not change.
  • Storage keeps growing for a subscriber that is gone. A durable subscription keeps accumulating messages until it is deleted with unsubscribe, so remove subscriptions you no longer use.
  • Topic messages disappear. A topic only delivers to subscriptions that exist when the message arrives. Create the subscription before publishing, and make it durable if the subscriber may be offline.
  • The same message is processed twice. In AUTO_ACKNOWLEDGE mode a failure can cause the last message to be redelivered, and DUPS_OK_ACKNOWLEDGE allows duplicates by design. Make consumers detect repeats; XA transactions compares that approach with two-phase commit.

The 2006 archive page of the original ActiveMQ FAQ, which already covered how a queue compares to a topic, is kept at /site/faq.html as history.

Frequently asked questions

What is the difference between a queue and a topic?

A queue delivers each message to exactly one consumer and keeps it until a consumer is available. A topic delivers a copy of each message to every subscription that exists when the message arrives.

What is point-to-point messaging?

Point-to-point messaging sends each message to a queue, where only one consumer receives it. Several consumers can read from the same queue, and the broker load-balances messages across them.

What is a durable subscription in JMS?

A durable subscription is a topic subscription that keeps receiving messages while its consumer is offline. The broker holds them until a consumer acknowledges them or they expire. It is identified by a subscription name plus the client identifier.

Does ActiveMQ Classic support shared subscriptions?

Not in current releases. ActiveMQ Classic's JMS 2.0 page says shared topic consumers throw UnsupportedOperationException and lists the feature for 7.0.0. Virtual topics are the Classic alternative, and Apache Artemis supports shared subscriptions.

Which JMS acknowledgement mode should I use?

AUTO_ACKNOWLEDGE is the simplest, CLIENT_ACKNOWLEDGE lets your code decide when a message is done, and DUPS_OK_ACKNOWLEDGE accepts duplicates for lower overhead. The ActiveMQ FAQ recommends transacted sessions in most cases, because a rollback returns messages for redelivery.

Related guides

From the 2006 archive

The original ActiveMQ documentation on these topics, kept as it was published here in 2004–2007. It describes ActiveMQ 4.x and older.

Sources

  1. Jakarta Messaging 3.1 specification jakarta.ee
  2. ActiveMQ: How does a Queue compare to a Topic activemq.apache.org
  3. ActiveMQ: Should I use transactions activemq.apache.org
  4. ActiveMQ: Jakarta Messaging 3.1 & JMS 2.0 support activemq.apache.org
  5. ActiveMQ: Virtual Destinations activemq.apache.org
  6. ActiveMQ: Why do I not receive messages on my durable topic subscription activemq.apache.org
  7. Apache Artemis artemis.apache.org
  8. Apache Artemis news artemis.apache.org