Class: Meerkat::Backend::AMQP
- Inherits:
-
Object
- Object
- Meerkat::Backend::AMQP
- Defined in:
- lib/meerkat/backend/amqp.rb
Instance Method Summary collapse
-
#initialize(amqp_url = 'amqp://guest:guest@localhost') ⇒ AMQP
constructor
A new instance of AMQP.
- #publish(topic, json) ⇒ Object
- #subscribe(topic, &callback) ⇒ Object
- #unsubscribe(channel) ⇒ Object
Constructor Details
#initialize(amqp_url = 'amqp://guest:guest@localhost') ⇒ AMQP
Returns a new instance of AMQP.
6 7 8 |
# File 'lib/meerkat/backend/amqp.rb', line 6 def initialize(amqp_url = 'amqp://guest:guest@localhost') @conn = ::AMQP.connect amqp_url end |
Instance Method Details
#publish(topic, json) ⇒ Object
10 11 12 13 14 |
# File 'lib/meerkat/backend/amqp.rb', line 10 def publish(topic, json) ::AMQP::Channel.new @conn do |ch| ch.topic("meerkat").publish(json, :routing_key => topic) end end |
#subscribe(topic, &callback) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/meerkat/backend/amqp.rb', line 16 def subscribe(topic, &callback) ::AMQP::Channel.new @conn do |ch| ch.queue('', :auto_delete => true) do |queue| queue.bind(ch.topic("meerkat"), :routing_key => topic) queue.subscribe do |headers, payload| callback.call headers.routing_key, payload end end end end |
#unsubscribe(channel) ⇒ Object
27 28 29 |
# File 'lib/meerkat/backend/amqp.rb', line 27 def unsubscribe(channel) channel.close end |