NodeJS和AWS SQS [英] NodeJS and AWS SQS

查看:311
本文介绍了NodeJS和AWS SQS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们, 我想在我们的Java API和NodeJS API之间建立消息队列. 阅读了几个使用aws-sdk的示例之后,我不确定如何使服务监视队列.

Folks, I would like to set up a message queue between our Java API and NodeJS API. After reading several examples of using aws-sdk, I am not sure how to make the service watch the queue.

例如,本文在节点上使用SQS:正在接收邮件示例代码告诉我使用sqs.receiveMessage()接收邮件,并使用sqs.deleteMessage()删除邮件.

For instance, this article Using SQS with Node: Receiving Messages Example Code tells me to use the sqs.receiveMessage() to receive and sqs.deleteMessage() to delete a message.

我不清楚的是如何将其包装到一个连续运行的服务中,该服务不断将消息从sqs队列中移出,传递给模型,然后将其存储在mongo中,等等.

What I am not clear about, is how to wrap this into a service that runs continuously, which constantly takes the messages off the sqs queue, passes them to the model, stores them in mongo, etc.

希望我的问题并不完全含糊.我对Node的经验主要在于Express.js.

Hope my question is not entirely vague. My experience with Node lies primarily with Express.js.

答案很简单,就像使用 sqs-poller ?如何在已经运行的NodeJS Express应用程序中实现相同功能?我很可能应该调查SNS,以确保消息传输没有任何延迟.

Is the answer as simple as using something like sqs-poller? How would I implement the same into an already running NodeJS Express app? Quite possibly I should look into SNS to not have any delay in message transfers.

谢谢!

推荐答案

首先,Amazon SQS是一个伪队列,它保证消息的可用性,但不能保证以FIFO方式的消息顺序.如果您希望应用程序能够以这种方式工作,则必须在其应用程序中实现排序逻辑.

For a start, Amazon SQS is a pseudo queue that guarantees availability of messages but not their sequence in FIFO fashion. You have to implement sequencing logic into your app if you want it to work that way.

回到您的问题,必须在您的应用程序中对SQS进行轮询,以检查是否有新消息可用.我在使用setInterval()的应用程序中实现了此功能.我将轮询队列中的项目,如果未找到任何项目,则我将延迟下一次调用,如果发现了某些项目,则下一个调用将立即绕过setInterval().显然,这是非常原始的实现,您可以研究替代方法.当在SQS中发现新项目时,在服务器上执行ping NodeJS应用程序的子进程怎么样?我认为您可以在不使用NodeJS的情况下,将子进程作为BASH中的观察者来实现.您也可以查看npm模块(如果已经有一个模块).

Coming back to your question, SQS has to be polled within your app to check if there are new messages available. I implemented this in an app using setInterval(). I would poll the queue for items and if no items were found, I would delay the next call and in case some items were found, the next call would be immediate bypassing the setInterval(). This is obviously a very raw implementation and you can look into alternatives. How about a child process on your server that pings your NodeJS app when a new item is found in SQS ? I think you can implement the child process as a watcher in BASH without using NodeJS. You can also look into npm modules if there is already one for this.

简而言之,您可以通过多种方式进行轮询,但是如果您正在使用Amazon SQS,则必须采用一种或另一种方式进行轮询.

In short, there are many ways you can poll but polling has to be done one way or the other if you are working with Amazon SQS.

对此我不确定,但是如果您希望收到有关物品的通知,则可能需要研究Amazon SNS.

I am not sure about this but if you want to be notified of items, you might want to look into Amazon SNS.

这篇关于NodeJS和AWS SQS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆