问题
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:
- 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. - 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?
回答1:
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.
回答2:
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.
The following seems to work just fine:
var messageOptions = {'operation': 'com.microsoft:peek-message'};
Please let me know if that helps, thanks.
来源:https://stackoverflow.com/questions/51503452/unable-to-peek-messages-from-an-azure-service-bus-queue-using-amqp-and-node