Class: SimpleMessaging::MessageQueue::Rabbit

Inherits:
SimpleMessaging::MessageQueue show all
Defined in:
lib/simple_messaging/message_queue/rabbit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SimpleMessaging::MessageQueue

driver_name, driver_name=, instance, unregister_queue

Constructor Details

#initialize(name) ⇒ Rabbit

Returns a new instance of Rabbit.



9
10
11
12
13
14
15
16
# File 'lib/simple_messaging/message_queue/rabbit.rb', line 9

def initialize(name)
  @@em_thread  ||= Thread.new { EventMachine.run }
  @@connection ||= AMQP.connect(rabbit_configs)
  @channel       = AMQP::Channel.new(@@connection)
  @exchange      = @channel.default_exchange
  @queue         = @channel.queue(queue_name(name))
  @name          = name
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



7
8
9
# File 'lib/simple_messaging/message_queue/rabbit.rb', line 7

def channel
  @channel
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/simple_messaging/message_queue/rabbit.rb', line 7

def name
  @name
end

#queueObject (readonly)

Returns the value of attribute queue.



7
8
9
# File 'lib/simple_messaging/message_queue/rabbit.rb', line 7

def queue
  @queue
end

Instance Method Details

#deleteObject



37
38
39
40
# File 'lib/simple_messaging/message_queue/rabbit.rb', line 37

def delete
  MessageQueue.unregister_queue(name)
  @queue.delete
end

#enqueue(message) ⇒ Object



18
19
20
21
22
# File 'lib/simple_messaging/message_queue/rabbit.rb', line 18

def enqueue(message)
  @exchange.publish message,
    :routing_key => @queue.name,
    :app_id => app_id
end

#poll(&block) ⇒ Object



24
25
26
27
28
# File 'lib/simple_messaging/message_queue/rabbit.rb', line 24

def poll(&block)
  @queue.subscribe do |, payload|
    yield payload
  end
end

#pop(&block) ⇒ Object



30
31
32
33
34
35
# File 'lib/simple_messaging/message_queue/rabbit.rb', line 30

def pop(&block)
  @queue.subscribe do |, payload|
    yield payload
    break
  end
end