Hello World:简单队列
创建您的第一个 RabbitMQ 生产者和消费者,通过基本队列发送和接收消息,了解消息代理中的“Hello World”。
Hello World:简单队列 是 CoddyKit 上的免费 RabbitMQ Messaging & Async Systems 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 RabbitMQ Messaging & Async Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 RabbitMQ Messaging & Async Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Your First Message Queue!
Welcome to your first hands-on lesson with RabbitMQ! We're going to create a simple 'Hello World' example, which is the foundational step for understanding message queues.
This lesson focuses on the absolute basics: sending a message to a queue and receiving it.
The Core Trio: P, C, Q
Every simple message queue system has three main parts:
- Producer: The application that sends messages.
- Queue: A buffer that stores messages. Think of it as a mailbox.
- Consumer: The application that receives and processes messages from the queue.
In our 'Hello World', a Producer will send 'Hello World!' to a Queue, and a Consumer will pick it up.
Basic Message Flow
The process is straightforward:
- The Producer connects to RabbitMQ.
- It sends a message to a named Queue.
- The Consumer also connects to RabbitMQ.
- It listens to the same named Queue.
- When a message arrives, the Consumer receives and processes it.
This decouples the sender from the receiver, allowing them to operate independently.
Connecting to RabbitMQ
Before sending or receiving, our applications need to connect to the RabbitMQ server. We use a ConnectionFactory to establish this link.
A Connection represents a TCP connection to the broker. From this connection, we create a Channel, which is where most of the API operations are performed.
Declaring a Queue
Both the producer and consumer need to agree on which queue to use. We declare a queue using channel.queueDeclare(). Declaring a queue is idempotent, meaning you can call it multiple times without issues.
If the queue doesn't exist, it will be created. If it already exists, it will do nothing.
Producer: Sending 'Hello World!'
Here's a simple Java program that acts as a producer. It connects to RabbitMQ, declares a queue named "hello", and sends a single message.
Remember, RabbitMQ needs to be running on localhost for this to work (as set up in a previous lesson).
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Producer {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message + "'");
}
}
}Consumer: Receiving Messages
Now, let's create the consumer. This program also connects to RabbitMQ, declares the same "hello" queue, and then continuously waits for messages.
When a message arrives, it will print it to the console. The true in basicConsume means messages are automatically acknowledged.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
public class Consumer {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
}
}Running the Example
To see it in action, you would typically:
- Ensure RabbitMQ is running (e.g., via Docker).
- Run the Consumer application first. It will start waiting.
- Run the Producer application. It will send the message.
- Observe the Consumer's output: it should print " [x] Received 'Hello World!'".
Congratulations, you've just sent your first message through RabbitMQ!
Key Takeaways of Simple Queue
The 'Hello World' simple queue demonstrates:
- One-to-one messaging: One producer, one queue, one consumer (though multiple consumers can compete for messages, as we'll see later).
- Basic message buffering: Messages are stored in the queue until a consumer is ready.
- Decoupling: Producer and consumer don't need to be running at the same time.
This is the simplest form of messaging, perfect for getting started.
Quick Check: Message Flow
Consider the basic 'Hello World' setup. What is the correct order of components for a message to be successfully delivered and processed?
Recap: Your First Message!
You've successfully created your first RabbitMQ 'Hello World'!
- You learned about the core components: Producer, Queue, and Consumer.
- You saw how to connect to RabbitMQ and declare a queue.
- You implemented simple Java code to send and receive a message.
This foundational understanding will serve you well as we explore more complex messaging patterns!
常见问题解答
「Hello World:简单队列」课时是免费的吗?
是的 — 「Hello World:简单队列」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 RabbitMQ Messaging & Async Systems 课程的其余内容,请升级到 CoddyKit PRO。 RabbitMQ Messaging & Async Systems 课程共包含 4 节课。
「Hello World:简单队列」这节课中我会学到什么?
创建您的第一个 RabbitMQ 生产者和消费者,通过基本队列发送和接收消息,了解消息代理中的“Hello World”。 你通过在浏览器中直接运行的动手代码来练习 RabbitMQ Messaging & Async Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 RabbitMQ Messaging & Async Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 RabbitMQ Messaging & Async Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「Hello World:简单队列」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 RabbitMQ Messaging & Async Systems 课中编写并运行代码吗?
能。每节 RabbitMQ Messaging & Async Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Hello World:简单队列
- 工作队列:公平分发
- 消息确认与持久化
- 使用扇出交换器实现发布/订阅