node.js中怎么对CQS进行操作

发布时间:2021-07-21 10:03:03 作者:Leah
来源:亿速云 阅读:212

本篇文章为大家展示了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:

  //Received<0through6>messages

  })

  删除一条队列消息:

  //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进行操作,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Python中怎么对redis进行操作
  2. python中如何对excel进行操作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

node.js cqs

上一篇:node.js中怎么注册服务器端

下一篇:如何解决PHP5.6+版本“No input file specified”问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》