class Bunny::Queue

DESCRIPTION:

Queues store and forward messages. Queues can be configured in the server or created at runtime. Queues must be attached to at least one exchange in order to receive messages from publishers.

Public Class Methods

consumer_class() click to toggle source

@return [Class] @private

# File lib/bunny/queue08.rb, line 63
def self.consumer_class
  # Bunny::Consumer
  Bunny::Subscription
end
new(client, name, opts = {}) click to toggle source
# File lib/bunny/queue08.rb, line 15
def initialize(client, name, opts = {})
  # check connection to server
  raise Bunny::ConnectionError, 'Not connected to server' if client.status == :not_connected

  @client = client
  @opts   = opts
  @delivery_tag = nil
  @subscription = nil

  # Queues without a given name are named by the server and are generally
  # bound to the process that created them.
  if !name
    opts = {
      :passive => false,
      :durable => false,
      :exclusive => true,
      :auto_delete => true
    }.merge(opts)
  end

  # ignore the :nowait option if passed, otherwise program will hang waiting for a
  # response that will not be sent by the server
  opts.delete(:nowait)

  opts = { :queue => name || '', :nowait => false }.merge(opts)

  client.send_frame(Qrack::Protocol::Queue::Declare.new(opts))

  method = client.next_method

  client.check_response(method, Qrack::Protocol::Queue::DeclareOk, "Error declaring queue #{name}")

  @name = method.queue
  client.queues[@name] = self
end

Public Instance Methods

ack(opts={}) click to toggle source

DESCRIPTION:

Acknowledges one or more messages delivered via the Deliver or Get-Ok methods. The client can ask to confirm a single message or a set of messages up to and including a specific message.

OPTIONS:

  • :delivery_tag

  • :multiple => true or false (default) - If set to true, the delivery tag is treated as "up to and including", so that the client can acknowledge multiple messages with a single method. If set to false, the delivery tag refers to a single message. If the multiple field is true, and the delivery tag is zero, tells the server to acknowledge all outstanding messages.

# File lib/bunny/queue08.rb, line 85
def ack(opts={})
  # Set delivery tag
  if delivery_tag.nil? and opts[:delivery_tag].nil?
    raise Bunny::AcknowledgementError, "No delivery tag received"
  else
    self.delivery_tag = opts[:delivery_tag] if delivery_tag.nil?
  end

  opts = {:delivery_tag => delivery_tag, :multiple => false}.merge(opts)

  client.send_frame(Qrack::Protocol::Basic::Ack.new(opts))

  # reset delivery tag
  self.delivery_tag = nil
end
bind(exchange, opts = {}) click to toggle source

DESCRIPTION:

Binds a queue to an exchange. Until a queue is bound it will not receive any messages. Queues are bound to the direct exchange " by default. If error occurs, a Bunny::ProtocolError is raised.

  • :key => 'routing key'* <tt>:key => 'routing_key' - Specifies the routing key for the binding. The routing key is used for routing messages depending on the exchange configuration.

  • :nowait => true or false (default) - Ignored by Bunny, always false.

RETURNS:

:bind_ok if successful.

# File lib/bunny/queue08.rb, line 118
def bind(exchange, opts = {})
  exchange = exchange.respond_to?(:name) ? exchange.name : exchange

  # ignore the :nowait option if passed, otherwise program will hang waiting for a
  # response that will not be sent by the server
  opts.delete(:nowait)

  opts = {
    :queue => name,
    :exchange => exchange,
    :routing_key => opts.delete(:key),
    :nowait => false
  }.merge(opts)

  client.send_frame(Qrack::Protocol::Queue::Bind.new(opts))

  method = client.next_method

  client.check_response(method, Qrack::Protocol::Queue::BindOk, "Error binding queue: #{name} to exchange: #{exchange}")

  # return message
  :bind_ok
end
default_consumer() click to toggle source

@return [Bunny::Consumer] Default consumer associated with this queue (if any), or nil @note Default consumer is the one registered with the convenience {#subscribe} method. It has no special properties of any kind. @see #subscribe @see Bunny::Consumer @api public

# File lib/bunny/queue08.rb, line 56
def default_consumer
  @default_consumer
end
delete(opts = {}) click to toggle source

DESCRIPTION:

Requests that a queue is deleted from broker/server. When a queue is deleted any pending messages are sent to a dead-letter queue if this is defined in the server configuration. Removes reference from queues if successful. If an error occurs raises Bunny::ProtocolError.

Options:

  • :if_unused => true or false (default) - If set to true, the server will only delete the queue if it has no consumers. If the queue has consumers the server does not delete it but raises a channel exception instead.

  • :if_empty => true or false (default) - If set to true, the server will only delete the queue if it has no messages. If the queue is not empty the server raises a channel exception.

  • :nowait => true or false (default) - Ignored by Bunny, always false.

Returns:

:delete_ok if successful

# File lib/bunny/queue08.rb, line 165
def delete(opts = {})
  # ignore the :nowait option if passed, otherwise program will hang waiting for a
  # response that will not be sent by the server
  opts.delete(:nowait)

  opts = { :queue => name, :nowait => false }.merge(opts)

  client.send_frame(Qrack::Protocol::Queue::Delete.new(opts))

  method = client.next_method

  client.check_response(method, Qrack::Protocol::Queue::DeleteOk, "Error deleting queue #{name}")

  client.queues.delete(name)

  # return confirmation
  :delete_ok
end
pop(opts = {}, &blk) click to toggle source

DESCRIPTION:

Gets a message from a queue in a synchronous way. If error occurs, raises Bunny::ProtocolError.

OPTIONS:

  • :ack => false (default) or true - If set to false, the server does not expect an acknowledgement message from the client. If set to true, the server expects an acknowledgement message from the client and will re-queue the message if it does not receive one within a time specified by the server.

RETURNS:

Hash {:header, :payload, :delivery_details}. :delivery_details is a hash {:consumer_tag, :delivery_tag, :redelivered, :exchange, :routing_key}.

If the queue is empty the returned hash will contain the values -

:header => nil
:payload => :queue_empty
:delivery_details => nil

N.B. If a block is provided then the hash will be passed into the block and the return value will be nil.

# File lib/bunny/queue08.rb, line 213
def pop(opts = {}, &blk)

  # do we want to have to provide an acknowledgement?
  ack = opts.delete(:ack)

  opts = {
    :queue => name,
    :consumer_tag => name,
    :no_ack => !ack,
    :nowait => true
  }.merge(opts)

  client.send_frame(Qrack::Protocol::Basic::Get.new(opts))

  method = client.next_method

  if method.is_a?(Qrack::Protocol::Basic::GetEmpty) then
    queue_empty = true
  elsif !method.is_a?(Qrack::Protocol::Basic::GetOk)
    raise Bunny::ProtocolError, "Error getting message from queue #{name}"
  end

  if !queue_empty
    # get delivery tag to use for acknowledge
    self.delivery_tag = method.delivery_tag if ack

    header = client.next_payload

    # If maximum frame size is smaller than message payload body then message
    # will have a message header and several message bodies
    msg = ''
    while msg.length < header.size
      msg << client.next_payload
    end

    msg_hash = {:header => header, :payload => msg, :delivery_details => method.arguments}

  else
    msg_hash = {:header => nil, :payload => :queue_empty, :delivery_details => nil}
  end

  # Pass message hash to block or return message hash
  blk ? blk.call(msg_hash) : msg_hash
end
purge(opts = {}) click to toggle source

DESCRIPTION:

Removes all messages from a queue. It does not cancel consumers. Purged messages are deleted without any formal "undo" mechanism. If an error occurs raises Bunny::ProtocolError.

Options:

  • :nowait => true or false (default) - Ignored by Bunny, always false.

Returns:

:purge_ok if successful

# File lib/bunny/queue08.rb, line 274
def purge(opts = {})
  # ignore the :nowait option if passed, otherwise program will hang waiting for a
  # response that will not be sent by the server
  opts.delete(:nowait)

  opts = { :queue => name, :nowait => false }.merge(opts)

  client.send_frame(Qrack::Protocol::Queue::Purge.new(opts))

  method = client.next_method

  client.check_response(method, Qrack::Protocol::Queue::PurgeOk, "Error purging queue #{name}")

  # return confirmation
  :purge_ok
end
status() click to toggle source

DESCRIPTION:

Returns hash {:message_count, :consumer_count}.

# File lib/bunny/queue08.rb, line 299
def status
  client.send_frame(Qrack::Protocol::Queue::Declare.new(:queue => name, :passive => true))
  method = client.next_method
  {:message_count => method.message_count, :consumer_count => method.consumer_count}
end
subscribe(opts = {}, &blk) click to toggle source
# File lib/bunny/queue08.rb, line 305
def subscribe(opts = {}, &blk)
  raise RuntimeError.new("This queue already has default consumer. Please instantiate Bunny::Consumer directly and call its #consume method to register additional consumers.") if @default_consumer && ! opts[:consumer_tag]

  # Create a subscription.
  @default_consumer = self.class.consumer_class.new(client, self, opts)
  @default_consumer.consume(&blk)
end
unbind(exchange, opts = {}) click to toggle source

DESCRIPTION:

Removes a queue binding from an exchange. If error occurs, a Bunny::ProtocolError is raised.

OPTIONS:

  • :key => 'routing key'* <tt>:key => 'routing_key' - Specifies the routing key for the binding.

  • :nowait => true or false (default) - Ignored by Bunny, always false.

RETURNS:

:unbind_ok if successful.

# File lib/bunny/queue08.rb, line 370
def unbind(exchange, opts = {})
  exchange = exchange.respond_to?(:name) ? exchange.name : exchange

  # ignore the :nowait option if passed, otherwise program will hang waiting for a
  # response that will not be sent by the server
  opts.delete(:nowait)

  opts = {
    :queue => name,
    :exchange => exchange,
    :routing_key => opts.delete(:key),
    :nowait => false
  }.merge(opts)

  client.send_frame(Qrack::Protocol::Queue::Unbind.new(opts))

  method = client.next_method

  client.check_response(method, Qrack::Protocol::Queue::UnbindOk, "Error unbinding queue #{name}")

  # return message
  :unbind_ok
end
unsubscribe(opts = {}) click to toggle source

DESCRIPTION:

Cancels a consumer. This does not affect already delivered messages, but it does mean the server will not send any more messages for that consumer.

OPTIONS:

  • :consumer_tag => 'tag' - Specifies the identifier for the consumer.

  • :nowait => true or false (default) - Ignored by Bunny, always false.

Returns:

:unsubscribe_ok if successful

# File lib/bunny/queue08.rb, line 331
def unsubscribe(opts = {})
  # Default consumer_tag from subscription if not passed in
  consumer_tag = @default_consumer ? @default_consumer.consumer_tag : opts[:consumer_tag]

  # Must have consumer tag to tell server what to unsubscribe
  raise Bunny::UnsubscribeError,
  "No consumer tag received" if !consumer_tag

  # Cancel consumer
  client.send_frame(Qrack::Protocol::Basic::Cancel.new(:consumer_tag => consumer_tag,:nowait => false))

  method = client.next_method

  client.check_response(method, Qrack::Protocol::Basic::CancelOk, "Error unsubscribing from queue #{name}")

  # Reset subscription
  @default_consumer = nil

  # Return confirmation
  :unsubscribe_ok
end