Introduction to Message Queues, JMS, and Popular MQ Technologies (RabbitMQ, Kafka)
This article explains the fundamentals of message queues, their role in asynchronous processing and system decoupling, introduces Java Message Service (JMS), compares point‑to‑point and publish‑subscribe models, and provides an overview of common MQ implementations such as RabbitMQ and Kafka.
1. Introduction to Message Queues
Message queues can be visualized as a locker where producers drop messages and consumers retrieve them later, eliminating the need for both parties to be online simultaneously and reducing system coupling.
1.1 What Is a Message Queue
A message queue is a container that stores messages until a consumer reads them; it is a core inter‑process communication mechanism in computer science.
1.2 Uses of Message Queues
Message queues improve performance by enabling asynchronous processing, smoothing traffic spikes, and lowering coupling between services. For example, a user‑registration workflow can be split into parallel steps, with the queue handling email and SMS notifications without blocking the main flow.
Reducing System Coupling
By inserting a queue between multiple systems, new integrations can consume messages without requiring changes to the original producer, thus keeping the architecture loosely coupled.
1.3 Two Messaging Patterns
Point‑to‑Point (Queue) Model
Each message is sent to a specific queue and consumed by a single receiver; the queue retains the message until it is acknowledged or expires.
Publish‑Subscribe (Topic) Model
Publishers send messages to a topic, which forwards them to all subscribed consumers; publishers and subscribers remain anonymous to each other.
2. JMS Overview
Java Message Service (JMS) is a Java API that standardizes messaging middleware, similar to JDBC for databases. It enables asynchronous communication between Java applications and abstracts the underlying message broker.
2.1 JMS Message Model
JMS supports the same point‑to‑point and publish‑subscribe patterns described earlier.
2.2 JMS Consumption Modes
Synchronous – the consumer calls receive() and blocks until a message arrives.
Asynchronous – the consumer registers a listener; the broker invokes onMessage() when a message is available.
2.3 JMS Programming Model
The JMS model mirrors JDBC: a ConnectionFactory creates a Connection , which creates a Session ; the session then creates producers and consumers.
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession(true);
ProjectMapper mapper = openSession.getMapper(ProjectMapper.class);
mapper.queryAllTaskByInit("init");3. MQ Landscape
Beyond JMS, several concrete message‑broker implementations exist, such as ActiveMQ, RabbitMQ, RocketMQ, and Kafka. RocketMQ only partially follows the JMS spec, while Kafka is not a JMS implementation at all.
3.1 AMQP Protocol
Advanced Message Queuing Protocol (AMQP) defines a standardized wire format for asynchronous messaging; RabbitMQ is a popular broker that implements AMQP.
AMQP Model
Publishers send messages to an exchange, which routes them to bound queues based on routing rules; consumers then retrieve messages from those queues.
3.2 RabbitMQ Model
RabbitMQ consists of a server side (exchanges and queues) and a client side (producers and consumers). Exchanges can be of type direct, fanout, topic, or headers, each with distinct routing behavior.
4. Kafka Overview
Kafka is a distributed log system that stores streams of records in topics, which are partitioned across multiple brokers. Producers write to topics, consumers read from them, and offsets track each record’s position.
4.1 Kafka Architecture
Kafka clusters consist of multiple brokers; each topic is split into partitions, and each partition is replicated for fault tolerance. A leader replica handles reads and writes, while followers replicate the data.
High Availability
Multiple brokers form a cluster.
Partitions are distributed across brokers.
Replica sets ensure data redundancy.
Leader election guarantees continuity when a broker fails.
4.2 Producer Structure
Producers send records to a broker; the broker appends them to the appropriate partition log.
5. Summary
Message queues provide asynchronous, decoupled communication; JMS defines a Java‑centric standard; RabbitMQ implements AMQP; Kafka offers a high‑throughput, partitioned log system. Understanding these concepts helps design scalable, resilient architectures.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.