Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'mq'
- trap("INT") do
- puts "int"
- ## what goes here? ##
- end
- module Messaging
- def self.thread
- @thread ||= Thread.new { EM.run {} }
- end
- def self.amqp
- thread
- @amqp ||= open_connection
- end
- def self.open_connection
- amqp = AMQP.connect(:user => 'dan', :pass => 'xxx'
- :host => "localhost", :port => 5672)
- puts "Connected to AMQP"
- amqp
- end
- def self.queue(name)
- MQ.new(amqp).queue(name)
- end
- def self.delete(name)
- queue(name).delete
- end
- def self.subscribe(name)
- queue(name).subscribe do |msg|
- puts "received #{msg.hash} from #{name}"
- yield msg
- end
- puts "subscribed to #{name}"
- end
- def self.publish(name, msg)
- puts "publishing #{msg.hash} to #{name}"
- queue(name).publish(msg)
- end
- end
- Messaging.subscribe("lots_connections") do |msg|
- puts "starting #{msg}..."
- sleep 5
- puts "done #{msg}."
- end
- 10000.times do |i|
- Messaging.publish("lots_connections", i.to_s)
- sleep 3
- end
Add Comment
Please, Sign In to add comment