0Pricing
RabbitMQ Messaging & Async Systems · 강의

Hello World: 간단한 대기열

첫 RabbitMQ 생산자와 소비자를 만들어 기본 대기열을 통해 메시지를 보내고 받습니다. 메시지 브로커의 'Hello World' 예제를 이해합니다.

Hello World: 간단한 대기열은(는) CoddyKit의 무료 RabbitMQ Messaging & Async Systems 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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:

  1. The Producer connects to RabbitMQ.
  2. It sends a message to a named Queue.
  3. The Consumer also connects to RabbitMQ.
  4. It listens to the same named Queue.
  5. 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:

  1. Ensure RabbitMQ is running (e.g., via Docker).
  2. Run the Consumer application first. It will start waiting.
  3. Run the Producer application. It will send the message.
  4. 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: 간단한 대기열” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 RabbitMQ Messaging & Async Systems 강의 전체를 잠금 해제할 수 있습니다. RabbitMQ Messaging & Async Systems 강의에는 총 4개의 강의가 포함되어 있습니다.

“Hello World: 간단한 대기열”에서 뭘 배우나요?

첫 RabbitMQ 생산자와 소비자를 만들어 기본 대기열을 통해 메시지를 보내고 받습니다. 메시지 브로커의 'Hello World' 예제를 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 RabbitMQ Messaging & Async Systems을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

RabbitMQ Messaging & Async Systems을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 RabbitMQ Messaging & Async Systems은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“Hello World: 간단한 대기열” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 RabbitMQ Messaging & Async Systems 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 RabbitMQ Messaging & Async Systems 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Hello World: 간단한 대기열
  2. 작업 대기열: 공정한 분배
  3. 메시지 확인과 내구성
  4. Fanout 익스체인지를 활용한 발행/구독
← RabbitMQ Messaging & Async Systems(으)로 돌아가기