Unravel โบ Java concepts โบ Messaging and Kafka
JMS and ActiveMQ messaging
JMS is the standard Java API for message brokers (ActiveMQ, IBM MQ, Artemis).
Learn these first
At a glance
A queue is a ticket line where each customer is served by one clerk.
- 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").
- 2Messages have a body (text, bytes, map or serialized object), headers (id, correlation id, reply-to) and properties for filtering with selectors
- 3Acknowledgement and transactions: a transacted session commits the receive and the sends together, otherwise failed messages are redelivered by the broker
- 4In application servers, @MessageDriven beans (EJB) are the container-managed equivalent of a listener
- Redelivery after failure leads to duplicates and poison loops
- Do not send Java-serialized objects (ObjectMessage): they couple both ends to the same class version and are a deserialization security risk
- 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
@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); }}- line 3 uses JmsTemplate, the core of JMS and ActiveMQ messaging
- line 4 uses JmsTemplate, the core of JMS and ActiveMQ messaging
- line 7 uses convertAndSend, the core of JMS and ActiveMQ messaging
- 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:
- Annotations:
@JmsListener@EnableJms@MessageDriven - Types:
JmsTemplateActiveMQConnectionFactoryJMSContextJMSExceptionTextMessageObjectMessageMessageProducerMessageConsumerActiveMQQueueActiveMQTopicDefaultJmsListenerContainerFactoryJmsMessagingTemplateMappingJackson2MessageConverterActiveMQConnectionand 1 more - Method calls:
createQueue()createTopic()createProducer()createConsumer()convertAndSend()
Part of a learning path
- Reliable messaging and distributed patterns ยท previous: RabbitMQ and AMQP messaging ยท next: AWS SQS and SNS messaging
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.