MQGET removes or copies messages from a queue your application opened for input. Where MQPUT is the producer dropping work into the system, MQGET is the worker picking it up—payment processor, inventory updater, or bridge forwarding to another system. The call supplies MQMD (filled on output with actual identifiers), MQGMO controlling wait and match rules, a buffer, and length in/out because messages can be larger than expected. A consumer loop typically: MQGET with wait, process payload, commit syncpoint if used, repeat. Wrong get options cause busy loops burning CPU (no wait on empty queue) or stuck threads (infinite wait without shutdown hook). This tutorial explains destructive versus browse gets, wait intervals, correlation matching, transactional gets with rollback, message truncation when buffer too small, backout count and poison messages, and graceful shutdown when the queue manager quiesces.
After MQOPEN with MQOO_INPUT, MQGET with default get options removes the first available message in FIFO order for standard local queues. MsgId and CorrelId in MQMD output reflect the stored message. Buffer length must be at least message size or the call succeeds with truncation and MQRC_TRUNCATED_MSG_ACCEPTED depending on options—beginners should allocate buffers from MQINQ MaxMsgLength or use two-step get patterns from IBM samples.
123456789101112131415MQMD md = {MQMD_DEFAULT}; MQGMO gmo = {MQGMO_DEFAULT}; char buffer[65535]; MQLONG buflen = sizeof(buffer); gmo.Options = MQGMO_WAIT | MQGMO_CONVERT; gmo.WaitInterval = 30000; /* 30 seconds */ MQGET(Hconn, Hobj, &md, &gmo, buflen, buffer, &buflen, &compCode, &reason); if (compCode == MQCC_OK) { /* buflen now actual message length */ } else if (reason == MQRC_NO_MSG_AVAILABLE) { /* timeout or empty */ }
| Option | Effect |
|---|---|
| MQGMO_WAIT | Block until message or WaitInterval expires |
| MQGMO_NO_WAIT | Return 2033 immediately if no message |
| MQGMO_BROWSE | Non-destructive read; message stays on queue |
| MQGMO_BROWSE_FIRST / NEXT | Walk messages in browse session |
| MQGMO_MATCH_CORREL_ID | Select message with matching CorrelId |
| MQGMO_SYNCPOINT | Get under UOW until commit or backout |
| MQGMO_CONVERT | Convert character sets to match MQMD request |
MQGMO_WAIT with WaitInterval 30000 blocks up to thirty seconds—efficient for dedicated consumer threads. MQGMO_NO_WAIT in a tight loop with sleep(1) adds latency but simplifies shutdown flags checked each iteration. Event-driven microservices often use wait with long interval plus MQ callback APIs in some clients. Compare CPU: no wait on empty queue spins at 100% CPU—never do that in production without delay.
MQGET is taking the letter out of the mailbox so nobody else gets the same letter. Browse is reading the letter through the window while leaving it inside for the official pickup later.
Server MQGET on WORK.IN, processes order, MQPUT reply with CorrelId equal to request MsgId. Client opened reply queue earlier, sets MQGMO_MATCH_CORREL_ID and CorrelId from saved request id, MQGET waits for matching reply only. Multiple outstanding requests need unique MsgIds and a map in application memory—correlation is not automatic magic without correct fields.
MQGMO_SYNCPOINT under UOW: message hidden from other consumers until commit. Processing exception triggers MQBACK—message returns to queue and BackoutCount increments. When BackoutCount exceeds queue BACKOUTTHRESH, message goes to backout queue or DLQ per configuration—poison message scenario. Handlers should catch bad JSON schema and route to error queue without infinite rollback loops. Monitor DLQ depth alerts.
If buffer smaller than message, options determine failure versus truncate. Production code queries message size or uses variable buffer reallocation patterns from IBM samples. Channels may reject oversize before message reaches consumer—fix MAXMSGL chain end to end.
Multiple instances MQGET same queue—each message typically to one consumer only (competing consumers pattern). Scale horizontally by adding consumer processes. Message ordering is not guaranteed across consumers unless using FIFO groups or partition keys in advanced patterns. INHIBIT(GET) stops all consumers administratively during drain maintenance.
Subscribers receive publications via get on associated queues or callback interfaces depending on API. Durable subscriptions retain messages for offline subscribers. Non-durable drops if subscriber disconnected—choose per business requirement.
During endmqm quiesce, MQGET with MQGMO_FAIL_IF_QUIESCING fails new gets so threads exit. Applications register signal handlers to set shutdown flag and break wait. MQDISC after MQCLOSE on open objects. Hung consumers block quiesce—operations kill process only after coordination.
MQGET is when you take the next note out of the class turn-in box and read it—once you take it, your friend cannot get the same note.
Write consumer loop pseudocode with 30s wait and clean shutdown flag.
Describe browse-first then destructive get audit pattern for support team.
Explain poison message path from failed processing to DLQ in five steps.
1. Destructive MQGET removes message when:
2. MQGMO_WAIT needs:
3. MATCH_CORREL_ID is for:
4. 2033 means: