الرسائل وقوائم الانتظار المستمرة
تعرّف إلى كيفية جعل الرسائل وقوائم الانتظار مستمرة لتظل محفوظة بعد إعادة تشغيل الوسيط. واضمن عدم فقدان بياناتك المهمة حتى في البيئات المتقلبة.
الرسائل وقوائم الانتظار المستمرة درس مجاني في RabbitMQ Messaging & Async Systems على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في RabbitMQ Messaging & Async Systems، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة RabbitMQ Messaging & Async Systems 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Persistence Matters
Imagine your RabbitMQ broker suddenly restarts. What happens to the messages and queues you've set up?
By default, many things are temporary. If your broker crashes or restarts, non-persistent messages and non-durable queues might be lost forever. This is where persistence comes in.
Persistence ensures that critical data—your queues and messages—survive broker restarts, making your messaging system robust and reliable.
Making Queues Durable
The first step to reliability is making your queues durable. A durable queue will survive a RabbitMQ broker restart.
When you declare a queue, you pass a durable parameter. Setting this to true tells RabbitMQ to save the queue's definition to disk.
- Durable: Queue definition saved; survives restarts.
- Non-durable: Queue definition lost on restart.
Remember, once a queue is declared durable (or not), you can't change its durability later without deleting and re-creating it.
Code: Declaring a Durable Queue
Here's how you declare a durable queue in Java. Notice the true for the second parameter in queueDeclare.
Try running this! Even if you stop and restart RabbitMQ, this queue named 'my_durable_queue' will still be there.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class DeclareDurableQueue {
private final static String QUEUE_NAME = "my_durable_queue";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
// Declare a durable queue
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
System.out.println("Queue '" + QUEUE_NAME + "' declared as durable.");
}
}
}Understanding Message Persistence
Even if a queue is durable, messages sent to it are not automatically saved to disk. For messages to survive a broker restart, they must also be marked as persistent.
When a message is persistent, RabbitMQ writes it to disk as soon as it's received by the broker, before delivering it to consumers.
This ensures that if the broker goes down, the message can be recovered from disk when the broker restarts.
Code: Sending a Persistent Message
To send a persistent message, you need to set the deliveryMode to 2 (which means persistent) or use MessageProperties.PERSISTENT_TEXT_PLAIN for convenience.
Run this producer, then try to restart your RabbitMQ broker. After restarting, if you run a consumer, you should still receive this message!
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
public class SendPersistentMessage {
private final static String QUEUE_NAME = "my_durable_queue";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
// Ensure the queue is durable first
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
String message = "Hello, persistent world!";
// Mark message as persistent
channel.basicPublish("", QUEUE_NAME,
MessageProperties.PERSISTENT_TEXT_PLAIN,
message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message + "'");
}
}
}The Persistence 'Gotcha'
It's crucial to understand a subtlety: even with durable queues and persistent messages, a message can still be lost.
This happens if the message is in transit from the producer to the broker, or if it has arrived at the broker but hasn't yet been flushed to disk, and the broker crashes at that exact moment.
For true reliability, where you know the broker has definitely received and saved your message, you need Publisher Confirms. We'll cover this in a future lesson!
When to Use Persistence
Persistence isn't free; it adds overhead because RabbitMQ has to write data to disk. So, when should you use it?
- Critical Tasks: Orders, financial transactions, user sign-ups.
- Long-running Processes: Image processing, video encoding.
- System Integration: Ensuring data transfer between services is reliable.
If message loss is acceptable (e.g., real-time monitoring data that's quickly outdated), you can skip persistence for better performance.
Performance Impact
Writing data to disk takes more time than keeping it in memory. Therefore, enabling persistence for queues and messages will reduce your message throughput (messages per second).
- Durable Queues: Minor impact, as it's just the queue definition.
- Persistent Messages: More significant impact, as each message is written.
It's a trade-off: higher reliability versus higher performance. Always choose persistence for messages that you absolutely cannot afford to lose.
Recap: Persistence in Action
Let's briefly review the steps for a fully persistent setup:
- Declare your queue as durable (
durable=true). - Send your messages with a persistent delivery mode (
MessageProperties.PERSISTENT_TEXT_PLAIN). - For ultimate reliability (knowing the broker received it), combine with Publisher Confirms (covered later).
Quick Check: Durable Queues
You want to ensure that a queue and its messages survive if the RabbitMQ broker restarts. Which of the following is absolutely necessary for the queue itself to survive a restart?
Recap & Next Steps
Great job! You've learned how to make your RabbitMQ queues and messages persistent, a crucial step towards building reliable messaging systems.
We covered:
- The importance of persistence.
- How to declare durable queues.
- How to send persistent messages.
- The performance trade-offs and the 'gotcha' with in-flight messages.
In the next lesson, we'll dive into Publisher Confirms, which provide an even higher level of reliability by ensuring the broker has truly received and processed your messages.
الأسئلة الشائعة
هل درس «الرسائل وقوائم الانتظار المستمرة» مجاني؟
نعم — نص درس «الرسائل وقوائم الانتظار المستمرة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة RabbitMQ Messaging & Async Systems، انتقل إلى CoddyKit PRO. تتضمن دورة RabbitMQ Messaging & Async Systems 4 دروس في المجموع.
ماذا ستتعلم في «الرسائل وقوائم الانتظار المستمرة»؟
تعرّف إلى كيفية جعل الرسائل وقوائم الانتظار مستمرة لتظل محفوظة بعد إعادة تشغيل الوسيط. واضمن عدم فقدان بياناتك المهمة حتى في البيئات المتقلبة. تتمرن على RabbitMQ Messaging & Async Systems مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ RabbitMQ Messaging & Async Systems؟
لا تُشترط خبرة سابقة. RabbitMQ Messaging & Async Systems على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «الرسائل وقوائم الانتظار المستمرة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس RabbitMQ Messaging & Async Systems هذا؟
نعم. كل درس في RabbitMQ Messaging & Async Systems يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الرسائل وقوائم الانتظار المستمرة
- تأكيدات الناشر من أجل الموثوقية
- إقرارات المستهلك وإعادة وضع الرسائل في قائمة الانتظار
- المعاملات مقابل تأكيدات الناشر