Class: Carrot::AMQP::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/amqp/queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(carrot, name, opts = {}) ⇒ Queue

Returns a new instance of Queue.



6
7
8
9
10
11
12
13
14
# File 'lib/amqp/queue.rb', line 6

def initialize(carrot, name, opts = {})
  @server = carrot.server
  @opts   = opts
  @name   = name
  @carrot = carrot
  server.send_frame(
    Protocol::Queue::Declare.new({ :queue => name, :nowait => true }.merge(opts))
  )
end

Instance Attribute Details

#carrotObject (readonly)

Returns the value of attribute carrot.



3
4
5
# File 'lib/amqp/queue.rb', line 3

def carrot
  @carrot
end

#delivery_tagObject

Returns the value of attribute delivery_tag.



4
5
6
# File 'lib/amqp/queue.rb', line 4

def delivery_tag
  @delivery_tag
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/amqp/queue.rb', line 3

def name
  @name
end

#serverObject (readonly)

Returns the value of attribute server.



3
4
5
# File 'lib/amqp/queue.rb', line 3

def server
  @server
end

Instance Method Details

#ackObject



33
34
35
36
37
# File 'lib/amqp/queue.rb', line 33

def ack
  server.send_frame(
    Protocol::Basic::Ack.new(:delivery_tag => delivery_tag)
  )
end

#bind(exchange, opts = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/amqp/queue.rb', line 59

def bind(exchange, opts = {})
  exchange           = exchange.respond_to?(:name) ? exchange.name : exchange
  bindings[exchange] = opts
  server.send_frame(
    Protocol::Queue::Bind.new({ :queue => name, :exchange => exchange, :routing_key => opts.delete(:key), :nowait => true }.merge(opts))
  )
end

#consumer_countObject



47
48
49
# File 'lib/amqp/queue.rb', line 47

def consumer_count
  status.last
end

#delete(opts = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/amqp/queue.rb', line 78

def delete(opts = {})
  server.send_frame(
    Protocol::Queue::Delete.new({ :queue => name, :nowait => true }.merge(opts))
  )
  carrot.queues.delete(name)
end

#message_countObject



43
44
45
# File 'lib/amqp/queue.rb', line 43

def message_count
  status.first
end

#pop(opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/amqp/queue.rb', line 16

def pop(opts = {})
  self.delivery_tag = nil
  server.send_frame(
    Protocol::Basic::Get.new({ :queue => name, :consumer_tag => name, :no_ack => !opts.delete(:ack), :nowait => true }.merge(opts))
  )
  method = server.next_method
  return unless method.is_a?(Protocol::Basic::GetOk)

  self.delivery_tag = method.delivery_tag

  header = server.next_payload
  msg    = server.next_payload
  raise 'unexpected length' if msg.length < header.size

  return msg, header
end

#publish(data, opts = {}) ⇒ Object



39
40
41
# File 'lib/amqp/queue.rb', line 39

def publish(data, opts = {})
  exchange.publish(data, opts)
end

#status(opts = {}, &blk) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/amqp/queue.rb', line 51

def status(opts = {}, &blk)
  server.send_frame(
    Protocol::Queue::Declare.new({ :queue => name, :passive => true }.merge(opts))
  )
  method = server.next_method
  [method.message_count, method.consumer_count]
end

#unbind(exchange, opts = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/amqp/queue.rb', line 67

def unbind(exchange, opts = {})
  exchange = exchange.respond_to?(:name) ? exchange.name : exchange
  bindings.delete(exchange)

  server.send_frame(
    Protocol::Queue::Unbind.new({
      :queue => name, :exchange => exchange, :routing_key => opts.delete(:key), :nowait => true }.merge(opts)
    )
  )
end