Class: Rmsg::Topic
- Inherits:
-
Object
- Object
- Rmsg::Topic
- Defined in:
- lib/rmsg/topic.rb
Overview
Topic handles publishing and subscribing to a topic with a key, over RabbitMQ.
Instance Method Summary collapse
-
#initialize(params) ⇒ Topic
constructor
A new instance of Topic.
-
#publish(message, key) ⇒ Exchange
Publish a message with a routing key.
-
#subscribe(key) {|message| ... } ⇒ Object
Subscribe to the topic, listening for a specific key.
Constructor Details
#initialize(params) ⇒ Topic
Returns a new instance of Topic.
8 9 10 11 |
# File 'lib/rmsg/topic.rb', line 8 def initialize(params) @rabbit = params[:rabbit] @exchange = @rabbit.channel.topic(params[:topic]) end |
Instance Method Details
#publish(message, key) ⇒ Exchange
Publish a message with a routing key.
17 18 19 |
# File 'lib/rmsg/topic.rb', line 17 def publish(, key) @exchange.publish(.to_json, :routing_key => key) end |
#subscribe(key) {|message| ... } ⇒ Object
Subscribe to the topic, listening for a specific key. Subscribing happens by continuously blocking the current process. It is specifically designed for long running processes. When receiving INT it will gracefully close.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rmsg/topic.rb', line 27 def subscribe(key) @queue = @rabbit.channel.queue("", :exclusive => true) @queue.bind(@exchange, :routing_key => key) begin @queue.subscribe(:block => true) do |delivery_info, , payload| = JSON.parse(payload, symbolize_names: true) yield end rescue Interrupt => _ @rabbit.close end end |