MQBatch
Provide the batch process model introduced in a new edtion of Ali-MNS service in June, 2015.
It derives from MQ, so all methods in MQ are avaiable in MQBatch too. For example, you can
use mqBatch.setRecvTolerance(1.2)
to adjust the timeout behavior of mqBatch.recvP().
var mqBatch = new AliMNS.MQBatch(aliCfg.mqName, account, aliCfg.region);
mqBatch.sendP(msg:string | Array, priority?:number, delaySeconds?:number)
Send a message or batch send messages to the queue.
msg: String or an array of Msg. The message(s) up to 16 that sent to queue.
priority: number, optional. Only valid when msg
is a string, 1(lowest)~16(highest), default is 8.
delaySeconds: number, optional. Only valid when msg
is a string. How many seconds will the messages be visible after sent. 0~604800(7days), default is 0.
This argument is prior to the options.DelaySeconds in attributes of message queue.
If msg
is an array of Msg
, use the priority & delaySeconds properties of Msg
, and ignore the 2nd and 3rd arguments.
var msgs = [];
for(var i=0;i<5;i++){
var msg = new AliMNS.Msg("BatchSend" + i, 8, 0);
msgs.push(msg);
}
mqBatch.sendP(msgs);
mqBatch.recvP(waitSeconds?:number, numOfMessages?:number)
Receive a message or batch receive messages from queue. This will change the messages to invisible for a while.
waitSeconds: number. optional. The max seconds to wait if queue is empty, after that an error MessageNotExist will be returned.
numOfMessages: number. optional. The max number of message can be received in a batch, can be 1~16, default is 16.
mqBatch.recvP(5, 16).then(console.log, console.error);
mqBatch.peekP(numOfMessages?:number)
Peek message(s). This will not change the message to invisible.
numOfMessages: number. optional. The max number of message can be peeked in a batch, can be 1~16, default is 16.
mqBatch.peekP(5, 16).then(console.log, console.error);
mqBatch.deleteP(receiptHandle:string | Array)
Delete a message or messages from queue. Messages will be invisible for a short time after received. Messages must be deleted after processed, otherwise it can be received again.
receiptHandle: String or an array of string. Return by mq.recvP mq.notifyRecv or mqBatch.recvP mqBatch.notifyRecv.
var rhsToDel = [];
mqBatch.recvP(5, 16).then(function(dataRecv){
for(var i=0;i<dataRecv.Messages.Message.length;i++){
rhsToDel.push(dataRecv.Messages.Message[i].ReceiptHandle);
}
}).then(function(){
return mqBatch.deleteP(rhsToDel);
}).then(console.log, console.error);
mqBatch.notifyRecv(cb:(ex:Error, msg:any)=>Boolean, waitSeconds?:number, numOfMessages?:number)
Register a callback function to receive messages in batch mode.
numOfMessages: number. optional. The max number of message can be received in a batch, can be 1~16, default is 16.
All other arguments are same as mq.notifyRecv.
DEBUG Trace
Set the environment variable DEBUG to "ali-mns" to enable the debug trace output.
# linux bash
export DEBUG=ali-mns
# windows
set DEBUG=ali-mns
Migrate
1.The ali-mns is fully compatible with ali-mqs, simply replace the ali-mqs package to ali-mns.
// var AliMQS = require('ali-mqs'); var AliMQS = require('ali-mns');
2.Optional. Change the ownerId to accountId Ali-Yun upgrade their account system, and recommend to use the newer account id instead of owner id. But the old owner id is still available for now.
var AliMQS = require("ali-mns"); // var account = new AliMNS.Account("hl35yqoedp", "<your-key-id>", "<your-key-secret>"); var account = new AliMNS.Account("1786090012649663", "<your-key-id>", "<your-key-secret>");
ownerId is mixed with number and letter
accountId is a 16-digits number, follow this link to find your accountId.
In GitHub, An branch v1.x keeps tracking for the old mqs services. And use `npm install ali-mqs' to install the ali-mqs package for v1.x.
Performance - Serial vs. Batch
Create 20 queues, then send 2000 messages to them randomly.
It is about 10 times slower in serial mode than in batch mode.
1st - Serial Mode(batch_size=1)
// 20 queues 2000 messages batch_size=1
AliMNS-performance
concurrent-queues
√ #BatchSend (3547ms)
√ #recvP (21605ms)
√ #stopRecv (6075ms)
2nd - Batch Mode(Batch_size=16)
// 20 queues 2000 messages batch_size=16
AliMNS-performance
concurrent-queues
√ #BatchSend (3472ms)
√ #recvP (2125ms)
√ #stopRecv (6044ms)
The testing code is in $/test/performance.js and a test log sample is in $/test/performance.log
Needs mocha module to run the test.
Set environment variable DEBUG to ali-mns.test to turn on output trace(will slow down the test).
License
MIT