Class: Apollo::Rabbitmq::Listener
- Inherits:
-
Object
- Object
- Apollo::Rabbitmq::Listener
- Defined in:
- lib/apollo/rabbitmq.rb
Instance Method Summary collapse
-
#get_all ⇒ Array
get_all returns all of the messages that were collected from the queue.
-
#initialize(exchange, key, opts = {}) ⇒ Listener
constructor
Returns a new listener.
Constructor Details
#initialize(exchange, key, opts = {}) ⇒ Listener
Returns a new listener. It listens on a randomly named queue bound to the specified exchange with the specified routing key
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/apollo/rabbitmq.rb', line 17 def initialize(exchange, key, opts = {}) username = CGI.escape opts.fetch(:rmq_username, 'guest') password = CGI.escape opts.fetch(:rmq_password, 'guest') host = opts.fetch(:ip, opts.fetch(:hostname, '127.0.0.1')) port = opts.fetch(:port, 5672) vhost = opts.fetch(:vhost, '/') @conn = Bunny.new("amqp://#{username}:#{password}@#{host}:#{port}#{vhost}") @conn.start raise 'connection is nil' if @conn.nil? @ch = @conn.create_channel @messages = [] @ch.temporary_queue.bind(exchange, :routing_key => key).subscribe do |delivery_info, , payload| @messages << JSON.parse(payload) end end |
Instance Method Details
#get_all ⇒ Array
get_all returns all of the messages that were collected from the queue
35 36 37 38 |
# File 'lib/apollo/rabbitmq.rb', line 35 def get_all() @conn.close @messages end |