Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate CQS in node.js

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

本篇文章为大家展示了node.js中怎么对CQS进行操作,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

怎样使用node.js来操作CQS

安装:

$npminstallcqs

初始化你的CQS模块:

//Anormalimport.

varcqs=require('cqs');

//Pre-applymycouchanddbname.

cqs=cqs.defaults({"couch":"https://user:password@example.iriscouch.com"

,"db":"cqs_queue"

});

列出所有的队列:

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

})

创建一个队列:

//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来操作CQS

添加一个队列消息:

//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

})

接收并处理一条队列消息:

//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:

//Receivedmessages

})

删除一条队列消息:

//TheconvenientobjectAPI:

message.del(function(error){

//Messagedeletionneverresultsinanerror.Ifamessageissuccessfully

//deleted,itwillsimplyneverappearinthequeueagain.

console.log('Messagedeleted!');

})

//ThestandardAPI:

cqs.DeleteMessage(my_message,function(error){

console.log('Messagedeleted');

})

上述内容就是node.js中怎么对CQS进行操作,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report