Merhaba Dünya: Basit Kuyruk
Temel bir kuyruk üzerinden mesaj gönderip almak için ilk RabbitMQ üreticinizi ve tüketicinizi oluşturun. Mesaj aracılığının “Merhaba Dünya” örneğini anlayın.
Merhaba Dünya: Basit Kuyruk, CoddyKit'te ücretsiz bir RabbitMQ Messaging & Async Systems dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, RabbitMQ Messaging & Async Systems öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. RabbitMQ Messaging & Async Systems kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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!
Sıkça Sorulan Sorular
“Merhaba Dünya: Basit Kuyruk” dersi ücretsiz mi?
Evet — “Merhaba Dünya: Basit Kuyruk” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve RabbitMQ Messaging & Async Systems kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. RabbitMQ Messaging & Async Systems kursu toplamda 4 dersten oluşur.
“Merhaba Dünya: Basit Kuyruk” dersinde ne öğreneceğim?
Temel bir kuyruk üzerinden mesaj gönderip almak için ilk RabbitMQ üreticinizi ve tüketicinizi oluşturun. Mesaj aracılığının “Merhaba Dünya” örneğini anlayın. RabbitMQ Messaging & Async Systems ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
RabbitMQ Messaging & Async Systems öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te RabbitMQ Messaging & Async Systems, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.
“Merhaba Dünya: Basit Kuyruk” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu RabbitMQ Messaging & Async Systems dersinde kod yazıp çalıştırabilir miyim?
Evet. Her RabbitMQ Messaging & Async Systems dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Merhaba Dünya: Basit Kuyruk
- İş Kuyrukları: Adil Dağıtım
- Mesaj Onayları ve Kalıcılık
- Fanout Değiş Tokuşlarıyla Yayınla/Abone Ol