Reads System V message-queue metadata, including pending message count and permissions.
What It Does and When to Use It
Inspect the current number of pending messages. A producer publishes work, a consumer executes it, and the broker or scheduler controls delivery and retries.
Requirements, Inputs and Expected Behavior
Confirm the runtime, extension, environment variables, and permissions before running the examples in production. Queue statistics are a snapshot and can change immediately under concurrent producers.
Example 1: Basic Operation
Inspect the current number of pending messages.
<?php
$queue = msg_get_queue(0x14500, 0600);
$stats = msg_stat_queue($queue);
echo 'Queued: ', $stats['msg_qnum'];
Example 2: Behavior and Validation
Read the kernel-configured queue capacity.
<?php
$queue = msg_get_queue(0x14501, 0600);
$stats = msg_stat_queue($queue);
echo 'Maximum bytes: ', $stats['msg_qbytes'];
Practical Example: Application Use
Observe a queue statistic after publishing a message.
<?php
$queue = msg_get_queue(0x14502, 0600);
msg_send($queue, 1, 'example');
$stats = msg_stat_queue($queue);
echo $stats['msg_qnum'];
msg_remove_queue($queue);
Important Behavior and Edge Cases
The second example highlights: Read the kernel-configured queue capacity. The practical example adds: Observe a queue statistic after publishing a message.
Comparison with Related Approaches
| Approach | What changes |
|---|---|
| Producer, consumer and scheduler | A producer publishes work, a consumer executes it, and the broker or scheduler controls delivery and retries. |
| Choosing safely | Acknowledge completed work and use idempotency to make retries safe. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Acknowledging a job before it finishes | Mark delivery successful only after durable processing succeeds. |
| Treating retries as harmless | Use idempotency keys and a bounded retry policy with dead-letter handling. |
| Skipping operational constraints | Queue statistics are a snapshot and can change immediately under concurrent producers. |
Key Takeaways
- Reads System V message-queue metadata, including pending message count and permissions.
- Observe a queue statistic after publishing a message.
- Acknowledge completed work and use idempotency to make retries safe.