BackendBench
Coffee

Unravel › Java concepts › Messaging and Kafka

I/O and data

RabbitMQ and AMQP messaging

RabbitMQ is a message broker.

Learn these first

At a glance

Mental model

A sorting office.

What happens, in order
  1. 1Exchange types decide routing: direct (exact routing key), topic (patterns such as order.*.created), fanout (copy to every bound queue) and headersA Binding connects an exchange to a queue.
  2. 2rabbitTemplate.convertAndSend("orders", "order.created", payload) publishes; @RabbitListener(queues = "order-queue") on a method consumes, converting JSON to an object
  3. 3Acknowledgements: with automatic acks in Spring, a method that returns normally acks and a thrown exception rejectsManual mode uses basicAck and basicNack with a delivery tag.
  4. 4A queue that holds failed messages is a dead-letter queue: rejected messages with requeue=false are republished to a dead-letter exchange
Remember
Watch out
Endless requeue of poison messagesUnlimited prefetchNon-durable queues for important dataSharing a channel between threads
Example (highlighted lines explain it)
@Componentpublic class OrderEvents {    private final RabbitTemplate rabbit;    public OrderEvents(RabbitTemplate rabbit) { this.rabbit = rabbit; }    public void created(Order order) {        rabbit.convertAndSend("orders.exchange", "order.created", order);    }    @RabbitListener(queues = "billing.order-created")    public void onCreated(Order order) {        billing.invoice(order);    }}
  1. line 3 uses RabbitTemplate, the core of RabbitMQ and AMQP messaging
  2. line 4 uses RabbitTemplate, the core of RabbitMQ and AMQP messaging
  3. line 10 uses RabbitListener, the core of RabbitMQ and AMQP messaging
Read the full explanation (about 2 min)

RabbitMQ is a message broker. Producers publish to an exchange, the exchange routes messages to queues by rules, and consumers take them from queues and acknowledge each one once done.

A mental model

A sorting office. Letters (messages) arrive at the front desk (exchange), which looks at the address label (routing key) and drops each into the right mailbox (queue). The postman (consumer) takes a letter and only signs the receipt when it has been delivered.

How it works

Exchange types decide routing: direct (exact routing key), topic (patterns such as order.*.created), fanout (copy to every bound queue) and headers. A Binding connects an exchange to a queue.

rabbitTemplate.convertAndSend("orders", "order.created", payload) publishes; @RabbitListener(queues = "order-queue") on a method consumes, converting JSON to an object.

Acknowledgements: with automatic acks in Spring, a method that returns normally acks and a thrown exception rejects. Manual mode uses basicAck and basicNack with a delivery tag.

A queue that holds failed messages is a dead-letter queue: rejected messages with requeue=false are republished to a dead-letter exchange.

Going deeper

By default a failed message is requeued immediately and loops forever, hammering the consumer. Configure retries with backoff, then reject to a dead-letter queue (AmqpRejectAndDontRequeueException).

Set prefetch (QoS) to a small number so one slow consumer does not hoard hundreds of unacked messages while others sit idle.

For no message loss, use durable queues, persistent messages, publisher confirms (CorrelationData) and manual or reliable acks. Defaults favour speed over safety.

Under the hood

A connection is one TCP link; work happens on lightweight channels multiplexed over it (channels are not thread safe, so each thread gets its own). The broker keeps unacked messages assigned to a channel and redelivers them if the channel closes, which is why a consumer crash does not lose the message but may duplicate it.

Common mistakes

  • Endless requeue of poison messages.
  • Unlimited prefetch.
  • Non-durable queues for important data.
  • Sharing a channel between threads.

How Unravel recognises it

When you paste code, Unravel looks for these signs of rabbitmq and amqp messaging. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is RabbitMQ and AMQP messaging?

RabbitMQ is a message broker. Producers publish to an exchange, the exchange routes messages to queues by rules, and consumers take them from queues and acknowledge each one once done.

What are the most common mistakes with RabbitMQ and AMQP messaging?

Endless requeue of poison messages. Unlimited prefetch. Non-durable queues for important data. Sharing a channel between threads.

How can I see RabbitMQ and AMQP messaging in real code?

Paste your own Java into Unravel and it will point out where rabbitmq and amqp messaging appears, explain how it works at the detail level you choose, and draw the flow of the code around it.