Receive and delete messages
Take up to ten messages off a queue, long-polling for them, and delete each one with its receipt handle once it is processed.
Replace my-queue with the name of your queue. A receive is a real receive wherever you make it: the messages are hidden from your other consumers until you delete them, release them, or their visibility timeout ends. See Message delivery.
Prerequisites
- Permission for
queue:ReceiveMessageandqueue:DeleteMessageon the queue.queue:ReceiveMessageis a write: the read-only presetQueueReadOnlyAccessdoes not include it. - In the console, also
queue:GetQueueAttributesto open the queue's page (without it the page is not found). Release also needsqueue:ChangeMessageVisibility. Withoutqueue:ReceiveMessagethe Receive messages section says "Your policies do not grant queue:ReceiveMessage on this queue; ask an administrator." and has no Poll for messages; withoutqueue:DeleteMessageorqueue:ChangeMessageVisibility, Delete or Release is unavailable.
Receive and delete
- Open the queue, then choose Send and receive.
- Under Receive messages, choose Poll for messages. The console takes up to 10 messages, waiting up to 5 seconds for one to arrive, and lists each with its id, when it was sent, and how many times it has been received.
- On each message you have dealt with, choose Delete. To give a message back to the queue at once, choose Release instead.
dcloud queue messages receive my-queue --max-messages 10 --wait-seconds 20
dcloud queue messages delete my-queue receipt-handleReplace receipt-handle with a message's receiptHandle from the receive.
POST /queue/api/queues/my-queue/receive
{ "maxMessages": 10, "waitSeconds": 20 }The answer is 200 with messages, empty when none arrived in time. Each message has
messageId, receiptHandle, body, md5OfBody, attributes, attributeTypes, sentAt,
receiveCount, firstReceivedAt, deadLetterSourceQueueId (in a dead-letter queue, the id of
the queue the message came from), and on a FIFO queue messageGroupId,
messageDeduplicationId and sequenceNumber. attributeTypes names the attributes an SQS client
sent with a type other than String; a Binary attribute's value is base64.
Delete one message with its receipt handle:
DELETE /queue/api/queues/my-queue/messages/{receiptHandle}Or up to 10 in one request, each with an id of your choosing:
POST /queue/api/queues/my-queue/messages/delete-batch
{ "entries": [ { "id": "a", "receiptHandle": "…" } ] }The batch answers 200 with successful and failed per id; a failed entry does not fail the
others.
Not available in Terraform. Receiving and deleting messages are actions, not configuration.
A receive that names no maxMessages takes 1 message; one that names no waitSeconds or
visibilityTimeoutSeconds uses the queue's receive wait and visibility timeout.
A consumer in Node.js
fetch is enough. This loop long-polls, processes, and deletes in batches; EWS_URL is the
Deutsche Cloud Console's address, and the access key is yours (see Security):
const BASE = `${process.env.EWS_URL}/queue/api/queues/my-queue`;
const AUTH = `EWS-ACCESS-KEY ${process.env.EWS_ACCESS_KEY_ID}:${process.env.EWS_SECRET_ACCESS_KEY}`;
async function call(method, path, body) {
const res = await fetch(`${BASE}${path}`, {
method,
headers: { Authorization: AUTH, 'Content-Type': 'application/json' },
body: body === undefined ? undefined : JSON.stringify(body),
});
const json = await res.json();
if (!json.success) throw new Error(`${json.code ?? res.status}: ${json.error}`);
return json.data;
}
for (;;) {
const { messages } = await call('POST', '/receive', { maxMessages: 10, waitSeconds: 20 });
const done = [];
for (const m of messages) {
try {
await handle(JSON.parse(m.body));
done.push({ id: m.messageId, receiptHandle: m.receiptHandle });
} catch (err) {
console.error(err); // not deleted: received again when its visibility timeout ends
}
}
if (done.length > 0) await call('POST', '/messages/delete-batch', { entries: done });
}Result
Deleted messages are gone for good. Deleting a message that is already gone succeeds, so a retried delete is safe.
When a receive or delete is refused
400with codeReceiptHandleIsInvalid: "The message was received again since this receipt was issued; only the latest receipt can delete it." Someone else now holds the message.400"That is not a receipt handle."403with codeOverLimitwhen 20,000 messages of the queue are in flight: delete processed messages, or shorten the visibility timeout.403"This organization already holds 50 long polls open. Use fewer consumers, or receive several messages per call." for a receive that would wait.429with codeThrottlingand aRetry-Afterheader when your organization makes more queue requests than the rate in Quotas.