Grails 中的生产者/消费者?
在多次尝试实现并发线程失败后,我正在尝试在 Grails 中实现消费者/生产者应用程序。 基本上,我想将来自客户端的所有事件(通过单独的 AJAX 调用)存储在单个队列中,然后在添加新事件后立即以线性方式处理这样的队列。
这看起来像是一个生产者/消费者问题: http://en.wikipedia.org/wiki/Producer-consumer_problem
我怎样才能在 Grails 中实现这个(也许使用计时器,或者通过生成事件“进程队列”更好)?
基本上我想要一个单例服务等待队列中的新事件并线性处理它们(即使队列是由多个并发进程加载的)。
有什么提示吗?
干杯!
I'm trying to implement a Consumer/Producer app in Grails, after several unsuccessful attempts at implementing concurrent threads.
Basically I want to store all the events coming from a clients (through separate AJAX calls) in a single queue and then process such a queue in a linear way as soon as new events are added.
This looks like a Producer/Consumer problem:
http://en.wikipedia.org/wiki/Producer-consumer_problem
How can I implement this in Grails (maybe with a timer or even better by generating an event 'process queue')?
Basically I'd like to a have a singleton service waiting for new events in the queue and processing them linearly (even if the queue is loaded by several concurrent processes).
Any hints?
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用 Java 消息传递服务,而不是自己全部实现。它完全可以处理您正在谈论的问题类型。此外,Grails 有一个 JMS 插件。
关于 JMS 的 Sun 文档: http://java.sun.com/developer /technicalArticles/Ecommerce/jms/index.html
Grails JMS 插件: http://www.grails.org/JMS+Plugin grails.org/JMS+Plugin
Rather than implement it all yourself, I would recommend using Java Messaging Service. It handles exactly the type of problem you are talking about. Further, Grails has a JMS plugin.
Sun Docs on JMS: http://java.sun.com/developer/technicalArticles/Ecommerce/jms/index.html
Grails JMS Plugin: http://www.grails.org/JMS+Plugin
我最近必须实现相同类型的系统,并使用 RabbitMQ 作为消息代理,并使用 < a href="http://grails.org/plugin/rabbitmq" rel="nofollow">RabbitMQ 插件,供 Grails 与之交互。
我选择 RabbitMQ 是因为它具有我正在寻找的功能(规模、速度、可靠性),并且是基于 AMQP 标准构建的,这意味着我可以让我的生产者和消费者用我需要的任何语言编写,而无需担心。
Grails 作为生产者和消费者都可以很好地工作,并且具有仍然能够与应用程序的完整 GORM 域一起工作的优势。
为了扩展,无论是队列的生产者还是消费者,我所要做的就是启动更多 grails 实例并调整消费者线程计数,这样它就可以很好地工作。
I recently had to implement the same sort of a system and went with RabbitMQ for the messaging broker, and used the RabbitMQ plugin for Grails to interface with it.
I chose RabbitMQ because it had the features I was looking for (scale, speed, reliability) and was built on the AMQP standard, meaning I could have my producers and consumers written in any language I needed without worrying.
Grails works very well as both a producer and a consumer and has the advantage of still being able to work with the full GORM domain of your application.
To scale, either the producer or consumer end of the queue, all I have to do is spin up more grails instances and adjust the consumer thread count, so it works really well.