本篇文章為大家展示了node.js中怎么對(duì)CQS進(jìn)行操作,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)專(zhuān)注于企業(yè)成都全網(wǎng)營(yíng)銷(xiāo)、網(wǎng)站重做改版、阿城網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為阿城等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。怎樣使用node.js來(lái)操作CQS
安裝:
$npminstallcqs
初始化你的CQS模塊:
//Anormalimport.
varcqs=require('cqs');
//Pre-applymycouchanddbname.
cqs=cqs.defaults({"couch":"https://user:password@example.iriscouch.com"
,"db":"cqs_queue"
});
列出所有的隊(duì)列:
cqs.ListQueues(function(error,queues){
console.log("Found"+queues.length+"queues:");
queues.forEach(function(queue){
console.log("*"+queue.name);
})
//Output:
//Found2queues:
//*a_queue
//*another_queue
})
創(chuàng)建一個(gè)隊(duì)列:
//Justcreatewithaname.
cqs.CreateQueue("important_stuff",function(error,queue){
if(!error)
console.log("Importantstuffqueueisready");
})
//Createwithanoptionsobject.
varopts={QueueName:"unimportant_stuff"
,DefaultVisibilityTimeout:3600//1hour
};
cqs.CreateQueue(opts,function(error,queue){
if(!error)
console.log("Created"+queue.name+"withtimeout+"queue.VisibilityTimeout);
//Output
//Createdunimportant_stuffwithtimeout3600
})
怎樣使用node.js來(lái)操作CQS
添加一個(gè)隊(duì)列消息:
//TheconvenientobjectAPI:
important_stuff.send(["keepthese","things","inorder"],function(error,message){
if(!error)
console.log('Sent:'+JSON.stringify(message.Body));
//Output:
//Sent:["keepthese","things","inorder"]
})
cqs.SendMessage(important_stuff,"Thismessageisimportant!",function(error,message){
if(!error)
console.log('Sentmessage:'+message.Body);
//Output:
//Sentmessage:Thismessageisimportant!
})
//Or,justusethequeuename.
cqs.SendMessage('some_other_queue',{going_to:"theotherqueue"},function(error,message){
if(!error)
console.log('Message'+message.MessageId+'isgoingto'+message.Body.going_to);
//Output:
//Messagea9b1c48bd6ae433eb7879013332cd3cdisgoingtotheotherqueue
})
接收并處理一條隊(duì)列消息:
//TheconvenientobjectAPI:
my_queue.receive(function(error,messages){
if(!error)
console.log('Receivedmessage:'+JSON.stringify(messages[0].Body));
//Output:
//Receivedmessage:
})
//ThestandardAPI,receivingmultiplemessages
cqs.ReceiveMessage(some_queue,5,function(er,messages){
if(!error)
console.log('Received'+messages.length+'messages');
//Output:
//Received<0through6>messages
})
刪除一條隊(duì)列消息:
//TheconvenientobjectAPI:
message.del(function(error){
//Messagedeletionneverresultsinanerror.Ifamessageissuccessfully
//deleted,itwillsimplyneverappearinthequeueagain.
console.log('Messagedeleted!');
})
//ThestandardAPI:
cqs.DeleteMessage(my_message,function(error){
console.log('Messagedeleted');
})
上述內(nèi)容就是node.js中怎么對(duì)CQS進(jìn)行操作,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。