1

I'm trying to "Peek" messages from an Azure Service Bus queue using nodeamqp10 library but unfortunately I am not able to do so.

Here's the code I am using:

const AMQPClient = require('amqp10/lib').Client,
Policy = require('amqp10/lib').Policy;

const protocol = 'amqps';
const keyName = 'MyPolicy';
const sasKey = 'My SAS Key'
const serviceBusHost = 'account.servicebus.windows.net';
const uri = protocol + '://' + encodeURIComponent(keyName) + ':' + encodeURIComponent(sasKey) + '@' + serviceBusHost;
const queueName = 'queue-name';
const policy = Policy.ServiceBusQueue;
policy.receiverLink.attach.rcvSettleMode = 1;
var client = new AMQPClient(policy);
client.connect(uri)
.then(function () {
    return Promise.all([
        client.createReceiver(queueName),
        client.createSender(queueName)
    ]);
})
.spread(function(receiver, sender) {
    sender.on('errorReceived', function (tx_err) { console.warn('===> TX ERROR: ', tx_err); });
    receiver.on('errorReceived', function (rx_err) { console.warn('===> RX ERROR: ', rx_err); });
    receiver.on('message', function(message) {
        console.log('Received message');
        console.log(message);
        console.log('------------------------------------');
        messages.push(message);
    });
    var messageOptions = {
      'applicationProperties': {
        'operation': 'com.microsoft:peek-message'
      }
    };
    return sender.send({}, messageOptions);
})
.error(function (e) {
    console.warn('connection error: ', e);
});

Few things are happening with this code that I am not able to comprehend:

  1. The code above fetches the messages but it is fetching them in Peek/Lock mode i.e. every time I run the code, the delivery count of messages is increasing by one which is not what I am looking for.
  2. The code above inserts a message in a queue every time it is run. I only want to fetch the messages from the queue and not insert any new message.

I have gone through the documentation here, and based on this I am specifying operation as com.microsoft:peek-message in applicationProperties which should only allow peeking at messages and not peeking and locking them.

Can anyone please tell me what am I doing wrong here?

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241

2 Answers2

0

In order to receive the messages without locks, set receiver-settle-mode property to settle on disposition while creating the receiver.

Answering your second question, you have created a sender for your queue, which sends the message when it is compiled. To stop it from sending message, just remove the sender and related code.

Arunprabhu
  • 1,454
  • 9
  • 15
  • Thanks! I am setting the `receiver-settle-mode` property on the receiver in the code (`policy.receiverLink.attach.rcvSettleMode = 1;`). Regarding the 2nd part, if I don't create the sender, how/where would I set the application properties? – Gaurav Mantri Jul 25 '18 at 07:00
  • Can you try receiving without setting the application properties? – Arunprabhu Jul 25 '18 at 07:52
  • Thanks! So based on your answer, I removed the sender related code. I am still able to receive messages but again the messages are fetched in peek-lock mode. My latest code can be found here: https://pastebin.com/18zduzEr. – Gaurav Mantri Jul 25 '18 at 07:57
  • I think you need to set the rcvSettleMode to 0 as 1 is for messages with locks, refer https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-amqp-request-response#response-12 – Arunprabhu Jul 25 '18 at 09:15
  • I don't think so. When we set this mode to `0`, messages are fetched but are deleted from the queue (as they are considered as settled). – Gaurav Mantri Jul 25 '18 at 09:19
  • I cannot find any documentation about the modes :-( – Arunprabhu Jul 25 '18 at 09:27
  • No issues. Thank you for your help so far. Let me open up an issue on the Github repo. – Gaurav Mantri Jul 25 '18 at 11:06
0

I think releasing the message will solve the problem:

receiver.release(message);

......

 receiver.on('message', function(message) {
        console.log('Received message');
        console.log(message);
        console.log('------------------------------------');
        messages.push(message);

        receiver.release(message);  

 });
.......

Apparently, I could achieve this with messageOptions as well. Currently, it seems malformed.

var messageOptions = {'applicationProperties': {'operation': 'com.microsoft:peek-message'}};

applicationProperties is not matching with any available configuration property, and so it is being added in "Custom Properties" of the message.

Custom Properties

The following seems to work just fine:

var messageOptions = {'operation': 'com.microsoft:peek-message'};

Please let me know if that helps, thanks.

Sunny Sharma
  • 4,688
  • 5
  • 35
  • 73