module Bunny
=begin rdoc
=== 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.
=end
class Queue09
attr_reader :name, :client
attr_accessor :delivery_tag
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
# 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,
:reserved_1 => 0
}.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)
client.send_frame(
Qrack::Protocol09::Queue::Declare.new({ :queue => name || '', :nowait => false, :reserved_1 => 0 }.merge(opts))
)
method = client.next_method
raise Bunny::ProtocolError, "Error declaring queue #{name}" unless method.is_a?(Qrack::Protocol09::Queue::DeclareOk)
@name = method.queue
client.queues[@name] = self
end
=begin rdoc
=== 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.
=end
def ack(opts={})
# If delivery tag is nil then set it to 1 to prevent errors
self.delivery_tag = 1 if self.delivery_tag.nil?
client.send_frame(
Qrack::Protocol09::Basic::Ack.new({:delivery_tag => delivery_tag, :multiple => false}.merge(opts))
)
# reset delivery tag
self.delivery_tag = nil
end
=begin rdoc
=== DESCRIPTION:
Gets a message from a queue in a synchronous way. If error occurs, raises _Bunny_::_ProtocolError_.
==== OPTIONS:
* :header => true or false (_default_) - If set to _true_,
hash {:header, :delivery_details, :payload} is returned.
* :no_ack => true (_default_) or false - If set to _true_, the server does not expect an
acknowledgement message from the client. If set to _false_, 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:
If :header => true returns hash {:header, :delivery_details, :payload}. :delivery_details is
a hash {:delivery_tag, :redelivered, :exchange, :routing_key, :message_count}. If
:header => false only the message payload is returned.
=end
def pop(opts = {})
# do we want the message header?
hdr = opts.delete(:header)
# do we want to have to provide an acknowledgement?
ack = opts.delete(:ack)
client.send_frame(
Qrack::Protocol09::Basic::Get.new({ :queue => name,
:consumer_tag => name,
:no_ack => !ack,
:nowait => true,
:reserved_1 => 0 }.merge(opts))
)
method = client.next_method
if method.is_a?(Qrack::Protocol09::Basic::GetEmpty) then
return :queue_empty
elsif !method.is_a?(Qrack::Protocol09::Basic::GetOk)
raise Bunny::ProtocolError, "Error getting message from queue #{name}"
end
# get delivery tag to use for acknowledge
self.delivery_tag = method.delivery_tag if ack
header = client.next_payload
msg = client.next_payload
raise Bunny::MessageError, 'unexpected length' if msg.length < header.size
# Return message with additional info if requested
hdr ? {:header => header, :payload => msg, :delivery_details => method.arguments} : msg
end
=begin rdoc
=== DESCRIPTION:
Publishes a message to the queue via the default nameless '' direct exchange.
==== RETURNS:
nil
=end
def publish(data, opts = {})
exchange.publish(data, opts)
end
=begin rdoc
=== DESCRIPTION:
Returns message count from Queue#status.
=end
def message_count
s = status
s[:message_count]
end
=begin rdoc
=== DESCRIPTION:
Returns consumer count from Queue#status.
=end
def consumer_count
s = status
s[:consumer_count]
end
=begin rdoc
=== DESCRIPTION:
Returns hash {:message_count, :consumer_count}.
=end
def status
client.send_frame(
Qrack::Protocol09::Queue::Declare.new({ :queue => name, :passive => true, :reserved_1 => 0 })
)
method = client.next_method
{:message_count => method.message_count, :consumer_count => method.consumer_count}
end
=begin rdoc
=== DESCRIPTION:
Asks the server to start a "consumer", which is a transient request for messages from a specific
queue. Consumers last as long as the channel they were created on, or until the client cancels them
with an _unsubscribe_. Every time a message reaches the queue it is passed to the _blk_ for
processing. If error occurs, _Bunny_::_ProtocolError_ is raised.
==== OPTIONS:
* :header => true or false (_default_) - If set to _true_, hash is delivered for each message
{:header, :delivery_details, :payload}.
* :consumer_tag => '_tag_' - Specifies the identifier for the consumer. The consumer tag is
local to a connection, so two clients can use the same consumer tags. If this field is empty the
queue name is used.
* :no_ack=> true (_default_) or false - If set to _true_, the server does not expect an
acknowledgement message from the client. If set to _false_, 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.
* :exclusive => true or false (_default_) - Request exclusive consumer access, meaning
only this consumer can access the queue.
* :nowait => true or false (_default_) - Ignored by Bunny, always _false_.
* :timeout => number of seconds (default = 0 no timeout) - The subscribe loop will continue to wait for
messages until terminated (Ctrl-C or kill command) or this timeout interval is reached.
==== RETURNS:
If :header => true returns hash {:header, :delivery_details, :payload} for each message.
:delivery_details is a hash {:consumer_tag, :delivery_tag, :redelivered, :exchange, :routing_key}.
If :header => false only message payload is returned.
If :timeout => > 0 is reached returns :timed_out
=end
def subscribe(opts = {}, &blk)
consumer_tag = opts[:consumer_tag] || name
secs = opts[:timeout] || 0
# ignore the :nowait option if passed, otherwise program will hang waiting for a
# response from the server causing an error.
opts.delete(:nowait)
# do we want the message header?
hdr = opts.delete(:header)
# do we want to have to provide an acknowledgement?
ack = opts.delete(:ack)
client.send_frame(
Qrack::Protocol09::Basic::Consume.new({ :reserved_1 => 0,
:queue => name,
:consumer_tag => consumer_tag,
:no_ack => !ack,
:nowait => false }.merge(opts))
)
raise Bunny::ProtocolError,
"Error subscribing to queue #{name}" unless
client.next_method.is_a?(Qrack::Protocol09::Basic::ConsumeOk)
loop do
begin
Timeout::timeout(secs) do
@method = client.next_method
end
rescue Timeout::Error
return :timed_out
end
break if @method.is_a?(Qrack::Protocol09::Basic::CancelOk)
# get delivery tag to use for acknowledge
self.delivery_tag = @method.delivery_tag if ack
header = client.next_payload
msg = client.next_payload
raise Bunny::MessageError, 'unexpected length' if msg.length < header.size
# pass the message and related info, if requested, to the block for processing
blk.call(hdr ? {:header => header, :payload => msg, :delivery_details => method.arguments} : msg)
end
end
=begin rdoc
=== 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_.
=end
def unsubscribe(opts = {})
consumer_tag = opts[:consumer_tag] || name
# ignore the :nowait option if passed, otherwise program will hang waiting for a
# response from the server causing an error
opts.delete(:nowait)
client.send_frame( Qrack::Protocol09::Basic::Cancel.new({ :consumer_tag => consumer_tag }.merge(opts)))
raise Bunny::ProtocolError,
"Error unsubscribing from queue #{name}" unless
client.next_method.is_a?(Qrack::Protocol09::Basic::CancelOk)
end
=begin rdoc
=== 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'* :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.
=end
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)
bindings[exchange] = opts
client.send_frame(
Qrack::Protocol09::Queue::Bind.new({ :queue => name,
:exchange => exchange,
:routing_key => opts.delete(:key),
:nowait => false,
:reserved_1 => 0 }.merge(opts))
)
raise Bunny::ProtocolError,
"Error binding queue #{name}" unless
client.next_method.is_a?(Qrack::Protocol09::Queue::BindOk)
# return message
:bind_ok
end
=begin rdoc
=== DESCRIPTION:
Removes a queue binding from an exchange. If error occurs, a _Bunny_::_ProtocolError_ is raised.
==== OPTIONS:
* :key => 'routing key'* :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.
=end
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)
bindings.delete(exchange)
client.send_frame(
Qrack::Protocol09::Queue::Unbind.new({ :queue => name,
:exchange => exchange,
:routing_key => opts.delete(:key),
:nowait => false,
:reserved_1 => 0 }.merge(opts)
)
)
raise Bunny::ProtocolError,
"Error unbinding queue #{name}" unless
client.next_method.is_a?(Qrack::Protocol09::Queue::UnbindOk)
# return message
:unbind_ok
end
=begin rdoc
=== 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
=end
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)
client.send_frame(
Qrack::Protocol09::Queue::Delete.new({ :queue => name, :nowait => false, :reserved_1 => 0 }.merge(opts))
)
raise Bunny::ProtocolError,
"Error deleting queue #{name}" unless
client.next_method.is_a?(Qrack::Protocol09::Queue::DeleteOk)
client.queues.delete(name)
# return confirmation
:delete_ok
end
=begin rdoc
=== 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
=end
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)
client.send_frame(
Qrack::Protocol09::Queue::Purge.new({ :queue => name, :nowait => false, :reserved_1 => 0 }.merge(opts))
)
raise Bunny::ProtocolError, "Error purging queue #{name}" unless client.next_method.is_a?(Qrack::Protocol09::Queue::PurgeOk)
# return confirmation
:purge_ok
end
private
def exchange
@exchange ||= Bunny::Exchange09.new(client, '', { :type => :direct,
:key => name,
:reserved_1 => 0,
:reserved_2 => false,
:reserved_3 => false})
end
def bindings
@bindings ||= {}
end
end
end