Integration / ActiveMQ Interview Questions Advanced
Explain the lifecycle of a message from producer to consumer in ActiveMQ?
A message travels through several distinct stages between the moment a producer creates it and the moment a consumer finishes with it.
First, the producer creates the message via the Session and calls send(), setting delivery mode, priority, and time-to-live. The broker receives it over a transport connector; if the delivery mode is persistent, it writes the message to KahaDB before the send is considered durable. The broker then enqueues the message on the destination's internal structure and dispatches it to a waiting consumer, respecting that consumer's prefetch limit and the message's priority. The consumer receives it either synchronously via receive() or asynchronously via a MessageListener, processes it, then acknowledges it according to the session's ack mode. On acknowledgment, the broker removes the message from its store; if no acknowledgment ever arrives, the redelivery policy kicks in and, after enough attempts, routes the message to the Dead Letter Queue instead.
Two details matter for correctness along this path. First, if the producer uses persistent delivery with the default synchronous send, the broker doesn't confirm the send back to the producer until the write to KahaDB has actually completed, so a producer that gets a successful response can trust the message won't vanish on a crash moments later. Second, the consumer's prefetch buffer means several messages may already be sitting client-side, dispatched but unacknowledged, before any single one is processed - which is why a consumer crash can leave multiple in-flight messages needing redelivery, not just the one currently being handled when it died.
More Related questions...