BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Messaging and Kafka

I/O and data

JMS and ActiveMQ messaging

JMS is the standard Java API for message brokers (ActiveMQ, IBM MQ, Artemis).

Learn these first

At a glance

Mental model

A queue is a ticket line where each customer is served by one clerk.

What happens, in order
  1. 1A ConnectionFactory creates connections and sessions; a session creates producers and consumers for a Queue or TopicSpring hides this behind JmsTemplate.convertAndSend("orders", payload) and @JmsListener(destination = "orders").
  2. 2Messages have a body (text, bytes, map or serialized object), headers (id, correlation id, reply-to) and properties for filtering with selectors
  3. 3Acknowledgement and transactions: a transacted session commits the receive and the sends together, otherwise failed messages are redelivered by the broker
  4. 4In application servers, @MessageDriven beans (EJB) are the container-managed equivalent of a listener
Remember
Watch out
Java-serialized ObjectMessage between servicesNo dead-letter or redelivery limitNo connection caching with JmsTemplateUsing a topic when exactly one consumer should handle each message
Example (highlighted lines explain it)
@Servicepublic class ShipmentMessaging {    private final JmsTemplate jms;    public ShipmentMessaging(JmsTemplate jms) { this.jms = jms; }    public void request(Shipment s) {        jms.convertAndSend("shipments.requested", s);    }    @JmsListener(destination = "shipments.requested")    public void onRequest(Shipment s) {        carrier.book(s);    }}
  1. line 3 uses JmsTemplate, the core of JMS and ActiveMQ messaging
  2. line 4 uses JmsTemplate, the core of JMS and ActiveMQ messaging
  3. line 7 uses convertAndSend, the core of JMS and ActiveMQ messaging
  4. line 10 uses JmsListener, the core of JMS and ActiveMQ messaging
Read the full explanation (about 1 min)

JMS is the standard Java API for message brokers (ActiveMQ, IBM MQ, Artemis). A queue delivers each message to one consumer; a topic delivers a copy to every subscriber.

A mental model

A queue is a ticket line where each customer is served by one clerk. A topic is a notice board: everyone who is watching sees the notice.

How it works

A ConnectionFactory creates connections and sessions; a session creates producers and consumers for a Queue or Topic. Spring hides this behind JmsTemplate.convertAndSend("orders", payload) and @JmsListener(destination = "orders").

Messages have a body (text, bytes, map or serialized object), headers (id, correlation id, reply-to) and properties for filtering with selectors.

Acknowledgement and transactions: a transacted session commits the receive and the sends together, otherwise failed messages are redelivered by the broker.

In application servers, @MessageDriven beans (EJB) are the container-managed equivalent of a listener.

Going deeper

Redelivery after failure leads to duplicates and poison loops. Configure a redelivery limit and a dead-letter queue (ActiveMQ.DLQ) and make processing idempotent.

Do not send Java-serialized objects (ObjectMessage): they couple both ends to the same class version and are a deserialization security risk. Use JSON or XML text messages.

JmsTemplate opens a connection and session per send unless a caching or pooling connection factory is used; the default is slow under load.

Under the hood

A Spring DefaultMessageListenerContainer runs a pool of threads that loop on receive(timeout), wrap each message in a transaction when configured, and call the listener. The broker tracks per-consumer prefetch and unacked messages, and redelivers when the session rolls back or the connection drops.

Common mistakes

  • Java-serialized ObjectMessage between services.
  • No dead-letter or redelivery limit.
  • No connection caching with JmsTemplate.
  • Using a topic when exactly one consumer should handle each message.

How Unravel recognises it

When you paste code, Unravel looks for these signs of jms and activemq 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 JMS and ActiveMQ messaging?

JMS is the standard Java API for message brokers (ActiveMQ, IBM MQ, Artemis). A queue delivers each message to one consumer; a topic delivers a copy to every subscriber.

What are the most common mistakes with JMS and ActiveMQ messaging?

Java-serialized ObjectMessage between services. No dead-letter or redelivery limit. No connection caching with JmsTemplate. Using a topic when exactly one consumer should handle each message.

How can I see JMS and ActiveMQ messaging in real code?

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