Class RightAws::SqsGen2Interface
In: lib/sqs/right_sqs_gen2_interface.rb
Parent: RightAwsBase
RuntimeError AwsError AwsNoChange RightAWSParser RightErrorResponseParser RightHttp2xxParser AcfInterface SqsInterface SqsGen2Interface S3Interface Ec2 SdbInterface RightAwsBase ActiveSdbConnect ActiveSdb SqsGen2 S3 S3Generator Sqs RightDummyParser AWSErrorHandler AwsBenchmarkingBlock AwsUtils RightSaxParserCallback lib/sqs/right_sqs_interface.rb lib/sqs/right_sqs_gen2.rb lib/s3/right_s3.rb lib/acf/right_acf_interface.rb lib/sqs/right_sqs_gen2_interface.rb lib/sqs/right_sqs.rb lib/sdb/right_sdb_interface.rb lib/sdb/active_sdb.rb lib/ec2/right_ec2.rb lib/s3/right_s3_interface.rb lib/awsbase/right_awsbase.rb RightAwsBaseInterface VERSION RightAws dot/m_13_0.png

Right::Aws::SqsGen2Interface - RightScale‘s low-level Amazon SQS interface for API version 2008-01-01 and later. For explanations of the semantics of each call, please refer to Amazon‘s documentation at developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=31

This class provides a procedural interface to SQS. Conceptually it is mostly a pass-through interface to SQS and its API is very similar to the bare SQS API. For a somewhat higher-level and object-oriented interface, see RightAws::SqsGen2.

Methods

Included Modules

RightAwsBaseInterface

Constants

API_VERSION = "2008-01-01"
DEFAULT_HOST = "queue.amazonaws.com"
DEFAULT_PORT = 443
DEFAULT_PROTOCOL = 'https'
REQUEST_TTL = 30
DEFAULT_VISIBILITY_TIMEOUT = 30

Public Class methods

[Source]

    # File lib/sqs/right_sqs_gen2_interface.rb, line 58
58:     def self.api 
59:       @@api
60:     end

[Source]

    # File lib/sqs/right_sqs_gen2_interface.rb, line 53
53:     def self.bench_sqs
54:       @@bench.service
55:     end

[Source]

    # File lib/sqs/right_sqs_gen2_interface.rb, line 50
50:     def self.bench_xml
51:       @@bench.xml
52:     end

Creates a new SqsInterface instance. This instance is limited to operations on SQS objects created with Amazon‘s 2008-01-01 API version. This interface will not work on objects created with prior API versions. See Amazon‘s article "Migrating to Amazon SQS API version 2008-01-01" at: developer.amazonwebservices.com/connect/entry.jspa?externalID=1148

 sqs = RightAws::SqsGen2Interface.new('1E3GDYEOGFJPIT75KDT40','hgTHt68JY07JKUY08ftHYtERkjgtfERn57DFE379', {:multi_thread => true, :logger => Logger.new('/tmp/x.log')})

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 }

[Source]

    # File lib/sqs/right_sqs_gen2_interface.rb, line 78
78:     def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
79:       init({ :name             => 'SQS', 
80:              :default_host     => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).host   : DEFAULT_HOST, 
81:              :default_port     => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).port   : DEFAULT_PORT, 
82:              :default_protocol => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).scheme : DEFAULT_PROTOCOL }, 
83:            aws_access_key_id     || ENV['AWS_ACCESS_KEY_ID'], 
84:            aws_secret_access_key || ENV['AWS_SECRET_ACCESS_KEY'], 
85:            params)
86:     end

Returns short queue name by url.

 RightSqs.queue_name_by_url('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 'my_awesome_queue'

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 301
301:     def self.queue_name_by_url(queue_url)
302:       queue_url[/[^\/]*$/]
303:     rescue
304:       on_exception
305:     end

Public Instance methods

Removes all visible messages from queue. Return true or an exception.

 sqs.clear_queue('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> true

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 331
331:     def clear_queue(queue_url)
332:       while (pop_messages(queue_url, 10).length > 0) ; end   # delete all messages in queue
333:       true
334:     rescue
335:       on_exception
336:     end

Creates a new queue, returning its URI.

 sqs.create_queue('my_awesome_queue') #=> 'http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue'

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 153
153:     def create_queue(queue_name, default_visibility_timeout=nil)
154:       req_hash = generate_request('CreateQueue', 'QueueName' => queue_name,
155:                                   'DefaultVisibilityTimeout' => default_visibility_timeout || DEFAULT_VISIBILITY_TIMEOUT )
156:       request_info(req_hash, SqsCreateQueueParser.new(:logger => @logger))
157:     rescue
158:       on_exception
159:     end

Deletes message from queue. Returns true or an exception. Amazon returns true on deletion of non-existent messages. You must use the receipt handle for a message to delete it, not the message ID.

From the SQS Developer Guide: "It is possible you will receive a message even after you have deleted it. This might happen on rare occasions if one of the servers storing a copy of the message is unavailable when you request to delete the message. The copy remains on the server and might be returned to you again on a subsequent receive request. You should create your system to be idempotent so that receiving a particular message more than once is not a problem. "

 sqs.delete_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 'Euvo62/1nlIet...ao03hd9Sa0w==') #=> true

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 276
276:     def delete_message(queue_url, receipt_handle)
277:       req_hash = generate_request('DeleteMessage', 'ReceiptHandle' => receipt_handle, :queue_url  => queue_url)
278:       request_info(req_hash, SqsStatusParser.new(:logger => @logger))
279:     rescue
280:       on_exception
281:     end

Deletes queue. Any messages in the queue are permanently lost. Returns true or an exception. Queue deletion can take up to 60 s to propagate through SQS. Thus, after a deletion, subsequent list_queues calls may still show the deleted queue. It is not unusual within the 60 s window to see the deleted queue absent from one list_queues call but present in the subsequent one. Deletion is eventual.

 sqs.delete_queue('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue_2') #=> true

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 185
185:     def delete_queue(queue_url)
186:       req_hash = generate_request('DeleteQueue', :queue_url      => queue_url)
187:       request_info(req_hash, SqsStatusParser.new(:logger => @logger))
188:     rescue
189:       on_exception
190:     end

Retrieves the queue attribute(s). Returns a hash of attribute(s) or an exception.

 sqs.get_queue_attributes('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue')
 #=> {"ApproximateNumberOfMessages"=>"0", "VisibilityTimeout"=>"30"}

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 197
197:     def get_queue_attributes(queue_url, attribute='All')
198:       req_hash = generate_request('GetQueueAttributes', 'AttributeName' => attribute, :queue_url  => queue_url)
199:       request_info(req_hash, SqsGetQueueAttributesParser.new(:logger => @logger))
200:     rescue
201:       on_exception
202:     end

Returns approximate number of messages in queue.

 sqs.get_queue_length('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 3

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 321
321:     def get_queue_length(queue_url)
322:       get_queue_attributes(queue_url)['ApproximateNumberOfMessages'].to_i
323:     rescue
324:       on_exception
325:     end

Lists all queues owned by this user that have names beginning with queue_name_prefix. If queue_name_prefix is omitted then retrieves a list of all queues. Queue creation is an eventual operation and created queues may not show up in immediately subsequent list_queues calls.

 sqs.create_queue('my_awesome_queue')
 sqs.create_queue('my_awesome_queue_2')
 sqs.list_queues('my_awesome') #=> ['http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue','http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue_2']

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 169
169:     def list_queues(queue_name_prefix=nil)
170:       req_hash = generate_request('ListQueues', 'QueueNamePrefix' => queue_name_prefix)
171:       request_info(req_hash, SqsListQueuesParser.new(:logger => @logger))
172:     rescue
173:       on_exception
174:     end

Pops (retrieves and deletes) first accessible message from queue. Returns the message in format {:id=>’message_id’, :body=>’message_body’} or nil.

 sqs.pop_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=>
   {:id=>"12345678904GEZX9746N|0N9ED344VK5Z3SV1DTM0|1RVYH4X3TJ0987654321", :body=>"message_1"}

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 359
359:     def pop_message(queue_url)
360:       messages = pop_messages(queue_url)
361:       messages.blank? ? nil : messages[0]
362:     rescue
363:       on_exception
364:     end

Pops (retrieves and deletes) up to ‘number_of_messages’ from queue. Returns an array of retrieved messages in format: [{:id=>’message_id’, :body=>’message_body’}].

  sqs.pop_messages('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 3) #=>
  [{"ReceiptHandle"=>"Euvo62/...+Zw==", "MD5OfBody"=>"16af2...81e3", "Body"=>"Goodbyte World!",
  "MessageId"=>"MEZI...JSWDE="}, {...}, ... , {...} ]

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 344
344:     def pop_messages(queue_url, number_of_messages=1)
345:       messages = receive_message(queue_url, number_of_messages)
346:       messages.each do |message|
347:         delete_message(queue_url, message['ReceiptHandle'])
348:       end
349:       messages
350:     rescue
351:       on_exception
352:     end
push_message(queue_url, message)

Alias for send_message

Returns short queue name by url.

 sqs.queue_name_by_url('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 'my_awesome_queue'

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 311
311:     def queue_name_by_url(queue_url)
312:       self.class.queue_name_by_url(queue_url)
313:     rescue
314:       on_exception
315:     end

Given the queue‘s short name, this call returns the queue URL or nil if queue is not found

 sqs.queue_url_by_name('my_awesome_queue') #=> 'http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue'

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 286
286:     def queue_url_by_name(queue_name)
287:       return queue_name if queue_name.include?('/')
288:       queue_urls = list_queues(queue_name)
289:       queue_urls.each do |queue_url|
290:         return queue_url if queue_name_by_url(queue_url) == queue_name
291:       end
292:       nil
293:     rescue
294:       on_exception
295:     end

Retrieves a list of messages from queue. Returns an array of hashes in format: {:id=>’message_id’, body=>’message_body’}

  sqs.receive_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue',10, 5) #=>
   [{"ReceiptHandle"=>"Euvo62...kw==", "MD5OfBody"=>"16af2171b5b83cfa35ce254966ba81e3",
     "Body"=>"Goodbyte World!", "MessageId"=>"MUM4WlAyR...pYOTA="}, ..., {}]

Normally this call returns fewer messages than the maximum specified, even if they are available.

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 233
233:     def receive_message(queue_url, max_number_of_messages=1, visibility_timeout=nil)
234:       return [] if max_number_of_messages == 0
235:       req_hash = generate_post_request('ReceiveMessage', 'MaxNumberOfMessages'  => max_number_of_messages, 'VisibilityTimeout' => visibility_timeout,
236:                                        :queue_url          => queue_url )
237:       request_info(req_hash, SqsReceiveMessageParser.new(:logger => @logger))
238:     rescue
239:       on_exception
240:     end

Sends a new message to a queue. Message size is limited to 8 KB. If successful, this call returns a hash containing key/value pairs for "MessageId" and "MD5OfMessageBody":

 sqs.send_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 'message_1') #=> "1234567890...0987654321"
 => {"MessageId"=>"MEs4M0JKNlRCRTBBSENaMjROTk58QVFRNzNEREhDVFlFOVJDQ1JKNjF8UTdBRllCUlJUMjhKMUI1WDJSWDE=", "MD5OfMessageBody"=>"16af2171b5b83cfa35ce254966ba81e3"}

On failure, send_message raises an exception.

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 252
252:     def send_message(queue_url, message)
253:       req_hash = generate_post_request('SendMessage', :message  => message, :queue_url => queue_url)
254:       request_info(req_hash, SqsSendMessagesParser.new(:logger => @logger))
255:     rescue
256:       on_exception
257:     end

Sets queue attribute. Returns true or an exception.

 sqs.set_queue_attributes('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', "VisibilityTimeout", 10) #=> true

From the SQS Dev Guide: "Currently, you can set only the VisibilityTimeout attribute for a queue… When you change a queue‘s attributes, the change can take up to 60 seconds to propagate throughout the SQS system."

NB: Attribute values may not be immediately available to other queries for some time after an update. See the SQS documentation for semantics, but in general propagation can take up to 60 s.

[Source]

     # File lib/sqs/right_sqs_gen2_interface.rb, line 217
217:     def set_queue_attributes(queue_url, attribute, value)
218:       req_hash = generate_request('SetQueueAttributes', 'Attribute.Name'  => attribute, 'Attribute.Value' => value, :queue_url  => queue_url)
219:       request_info(req_hash, SqsStatusParser.new(:logger => @logger))
220:     rescue
221:       on_exception
222:     end

[Validate]