Class: Apollo::Rabbitmq::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/apollo/rabbitmq.rb

Instance Method Summary collapse

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

Parameters:

  • exchange (String)

    the exchange to bind the queue to

  • key (String)

    the routing key to bind the queue to the exchange

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :rmq_username (String)

    The username to connect to the rabbitmq server

  • :rmq_password (String)

    The password to connect to the rabbitmq server

  • :address (String)

    the hostname or ip to connect to the rabbitmq server

  • :vhost (String)

    the vhost to connect to

  • :port (Integer)

    the port to connect to the rabbitmq server



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_allArray

get_all returns all of the messages that were collected from the queue

Returns:

  • (Array)

    An array of hashes from the json



35
36
37
38
# File 'lib/apollo/rabbitmq.rb', line 35

def get_all()
  @conn.close
  @messages
end