Class: Aws::Sqs

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

Overview

Aws::Sqs – RightScale's Amazon SQS interface, API version 2008-01-01 and later. The Aws::Sqs class provides a complete interface to the second generation of Amazon's Simple Queue Service. For explanations of the semantics of each call, please refer to Amazon's documentation at developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=31

Aws::Sqs is built atop Aws::SqsInterface, a lower-level procedural API that may be appropriate for certain programs.

Error handling: all operations raise an Aws::AwsError in case of problems. Note that transient errors are automatically retried.

sqs    = Aws::Sqs.new(aws_access_key_id, aws_secret_access_key)
queue1 = sqs.queue('my_awesome_queue')
 ...
queue2 = Aws::Sqs::Queue.create(sqs, 'my_cool_queue', true)
puts queue2.size
 ...
message1 = queue2.receive
message1.visibility = 0
puts message1
 ...
queue2.clear(true)
queue2.send_message('Ola-la!')
message2 = queue2.pop
 ...

NB: Second-generation SQS has eliminated the entire access grant mechanism present in Gen 1.

Params is a hash:

{:server       => 'queue.amazonaws.com' # Amazon service host: 'queue.amazonaws.com' (default)
 :port         => 443                   # Amazon service port: 80 or 443 (default)
 :multi_thread => true|false            # Multi-threaded (connection per each thread): true or false (default)
 :signature_version => '0'              # The signature version : '0' or '1'(default)
 :logger       => Logger Object}        # Logger instance: logs to STDOUT if omitted }

Defined Under Namespace

Classes: Message, Queue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aws_access_key_id = nil, aws_secret_access_key = nil, params = {}) ⇒ Sqs

Returns a new instance of Sqs


69
70
71
# File 'lib/sqs/sqs.rb', line 69

def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
  @interface = SqsInterface.new(aws_access_key_id, aws_secret_access_key, params)
end

Instance Attribute Details

#interfaceObject (readonly)

Returns the value of attribute interface


67
68
69
# File 'lib/sqs/sqs.rb', line 67

def interface
  @interface
end

Instance Method Details

#close_connectionObject


73
74
75
# File 'lib/sqs/sqs.rb', line 73

def close_connection
  @interface.close_connection
end

#queue(queue_name, create = true, visibility = nil) ⇒ Object

Returns Queue instance by queue name. If the queue does not exist at Amazon SQS and create is true, the method creates it.

Aws::Sqs.queue('my_awesome_queue') #=> #<Aws::Sqs::Queue:0xb7b626e4 ... >

94
95
96
97
98
99
100
101
102
# File 'lib/sqs/sqs.rb', line 94

def queue(queue_name, create=true, visibility=nil)
  if create
    url = @interface.create_queue(queue_name, visibility) # this returns the url even if it exists
  else
    url = @interface.queue_url_by_name(queue_name)
  end

  url ? Queue.new(self, url) : nil
end

#queues(prefix = nil) ⇒ Object

Retrieves a list of queues. Returns an array of Queue instances.

Aws::Sqs.queues #=> array of queues

83
84
85
86
87
# File 'lib/sqs/sqs.rb', line 83

def queues(prefix=nil)
  @interface.list_queues(prefix).map do |url|
    Queue.new(self, url)
  end
end