Last active
April 29, 2019 06:29
-
-
Save ramya-rao-a/363a2d66f5671ad81df5340d1842a0b8 to your computer and use it in GitHub Desktop.
Sample JS code to illustrate issue with deferred msgs received in ReceiveAndDelete mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Set up instructions: | |
- Ensure you have Nodejs installed, else install from https://nodejs.org/en/ | |
- Copy the this file to an empty folder | |
- Run `npm install @azure/service-bus` in this empty folder | |
- Add your connection string and partitioned queue (no sessions) details in the sample (use empty queue) | |
- Run `node deferred_receiveAndDelete_mode.js` (if you have named the file differently, use that name) | |
- Change the queue to other variations (unparitioned, sessions etc) and see the difference | |
*/ | |
const { ServiceBusClient, ReceiveMode } = require("@azure/service-bus"); | |
// Use empty queue | |
const connectionString = ""; | |
const queueName = ""; | |
async function main() { | |
const ns = ServiceBusClient.createFromConnectionString(connectionString); | |
const client = ns.createQueueClient(queueName); | |
try { | |
// Send a message | |
const sender = client.createSender(); | |
const msgBody = "hello" + Math.random(); | |
await sender.send({ body: msgBody }); | |
console.log(`Sent msg with body: ${msgBody}`); | |
// Receive and defer the message | |
const receiver1 = client.createReceiver(ReceiveMode.peekLock); | |
const [msg] = await receiver1.receiveMessages(1); | |
let sequenceNumber; | |
if (msg) { | |
console.log(`Deferring msg with body: ${msg.body}`); | |
sequenceNumber = msg.sequenceNumber; | |
await msg.defer(); | |
} | |
await receiver1.close(); | |
// Recieve deferred msg in ReceiveAndDelete mode | |
const receiver2 = client.createReceiver(ReceiveMode.receiveAndDelete); | |
const deferredMsg = await receiver2.receiveDeferredMessage(sequenceNumber); | |
if (deferredMsg) { | |
console.log( | |
`Received deferred msg with body: ${deferredMsg.body} and lockToken: ${ | |
deferredMsg.lockToken | |
}` | |
); | |
} | |
// Check contents of the queue | |
const peekedMsgs = await client.peek(1); | |
if (peekedMsgs.length) { | |
console.log("Oh no! There are messages left in the queue!!"); | |
} else { | |
console.log("All good"); | |
} | |
} finally { | |
await ns.close(); | |
} | |
} | |
main().catch(err => { | |
console.log("Error occurred: ", err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment