Distributed Message Queues in Ruby on Rails
what’s message queue?
![message queue](https://wikitechy.com/tutorials/ruby-on-rails/img/ruby-on-rails-images/message-queue.png)
Learn Ruby on Rails - Ruby on Rails tutorial - message queue - Ruby on Rails examples - Ruby On Rails programs
Distributed Message Queues
- Starling
- AMQP/RabbitMQ
- Stomp/ActiveMQ
- beanstalkd
Why not DRb?
- DRb has security risk and poorly designed APIs
- Distributed message queue is a great way to do distributed programming: reliable and scalable.
Starling
- A light-weight persistent queue server that speaks the Memcache protocol (mimics its API)
- Fast, effective, quick setup and ease of use
- Twitter’s open source project, they use it before 2009. (now switch to Kestrel, a port of Starling from Ruby to Scala)
Starling command
- sudo gem install starling-starling
- sudo starling -h 192.168.1.100
- sudo starling_top -h 192.168.1.100
Starling set example
require 'rubygems'
require 'starling'
starling = Starling.new('192.168.1.4:22122')
100.times do |i|
starling.set('my_queue', i)
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Starling get example
require 'rubygems'
require 'starling'
starling = Starling.new('192.168.2.4:22122')
loop do
puts starling.get("my_queue")
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
get method
- FIFO
- After get, the object is no longer in the queue. You will lost message if processing error happened.
- The get method blocks until something is returned. It’s infinite loop.
Handle processing error exception
require 'rubygems'
require 'starling'
starling = Starling.new('192.168.2.4:22122')
results = starling.get("my_queue")
begin
puts results.flatten
rescue NoMethodError => e
puts e.message
Starling.set("my_queue", [results])
rescue Exception => e
Starling.set("my_queue", results)
raise e
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Starling cons
- Poll queue constantly
- RabbitMQ can subscribe to a queue that notify you when a message is available for processing.
AMQP/RabbitMQ
- A complete and highly reliable enterprise messaging system based on the emerging AMQP standard.
- Erlang
Stomp/ActiveMQ
- Apache ActiveMQ is the most popular and powerful open source messaging and Integration Patterns provider.
- sudo gem install stomp
- ActiveMessaging plugin for Rails
beanstalkd
- Beanstalk is a simple, fast workqueue service. Its interface is generic, but was originally designed for reducing the latency of page views in high-volume web applications by running time-consuming tasks asynchronously.