无法“窥视"使用AMQP和Node从Azure Service Bus队列发送消息

问题描述:

我正在尝试使用 nodeamqp10 从"Azure服务总线"队列中窥视"消息库,但是很遗憾,我无法这样做.

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. 上面的代码获取消息,但是它以Peek/Lock模式获取消息,即,每次我运行代码时,消息的传递计数都增加了一个,这不是我想要的.
  2. 上面的代码每次运行时都会在队列中插入一条消息.我只想从队列中获取消息,而不插入任何新消息.
  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?

我认为发布消息将解决问题:

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);  

 });
.......

显然,我也可以通过messageOptions来实现.目前,它似乎格式不正确.

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

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

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

applicationProperties 与任何可用的配置属性都不匹配,因此将其添加到消息的自定义属性"中.

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

以下似乎很好用:

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

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

请告诉我是否有帮助,谢谢.

Please let me know if that helps, thanks.