开发者

Producer/consumer in 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!


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜