Aws sqs Doc and Ruby demo

Source: Internet
Author: User
Tags md5 digest unique id

# Amazon SQS collects and organizes aws sqs documents and uses Ruby demos

# Amazon simple Queue Service (SQS) is a scalable and reliable message delivery framework that allows you to easily create, store, and retrieve text messages. You can use it to build Amazon Web Services-based applications. Using SQS is a good way to build loosely coupled web applications. You only need to pay for messages based on your usage. The entire queue framework runs in the secure environment of Amazon data centers.


# SQS provides the following features: * Reliability
SQS can store messages in redundancy across multiple data centers to ensure their availability at any time.
* Simplicity
Accessing and using the SQS programming model is very simple, and SQS can be used in multiple programming languages.
* Security
SQS provides a high level of security. Only authorized users are allowed to access messages.
* Scalability
You can use SQS to create queue-based applications that can read and write unlimited messages.
* Low Cost
SQS meets your message delivery needs at a very low rate.


# A message contains no more than 8 KB of text data. Each message is stored until it is received by the application. When the application receiving a message reads a message from the queue, specify the visibility timeout value (in seconds ). Its function is like a lock:


* Ensure that other consumers in the queue do not see received messages within the specified period of time.
* Only when the timeout period expires and the application that reads the message does not delete the message will the message appear in the queue again.


* The message is retained for four days in the queue.
 
* SQS automatically deletes messages that have been in the queue for more than four days. SQS uses the "eventual consistency" model, which means that you can send messages to the queue, but the consumers of the queue may not be able to see it within a specific period of time. The message will be delivered eventually, but you must consider whether your application cares about the message order.


* Table 1. Message Components
Description
Messageid refers to the unique ID of the message.
Receipthandle is the only handle returned when obtaining a message from the queue. Each time a message is obtained from a queue, the returned handle is different. You need to use it when deleting a message.
The MD5 Digest of the message body string that is not URL-encoded by md5ofbody.
The actual message data of the body.


* Queue
A queue is a message container. Each message must be specified as the queue that will hold it. Messages sent to the queue are retained in the queue until they are explicitly deleted. The queue adopts the "first-in-first-out" order, but the order is not guaranteed. The default visibility timeout value for each queue is 30 seconds. You can modify this value for the entire queue or set each message separately when obtaining the message. The maximum timeout value for a queue or message visibility is two hours (7,200 seconds ). If the queue is not active for 30 consecutive days, SQS reserves the right to automatically delete them.


# Design considerations SQS are a bit different from common queue frameworks. Before designing SQS-based applications, you must consider three issues:


* SQS does not guarantee the order of messages in the queue.
Messages are in loose order in the queue; they are not stored in order of adding messages to the queue. SQS tries to maintain the order of messages, but it does not guarantee that the order of received messages is exactly the same as that of sent messages. If the order of messages is important to your application, you need to add sequence data to each message.
* SQS does not guarantee that messages in the queue are deleted.
When designing an application, you must ensure that processing the same message multiple times does not affect the program. SQS stores each message on multiple servers to provide redundancy and high availability. If one server is unavailable when a message is deleted, it is possible to obtain a copy of the message again after receiving the message (although this rarely happens ).
* SQS does not guarantee that all messages in the queue are returned during query.
SQS uses weighted random distribution-based message sampling. When querying a message, it only returns a message from a portion of the servers that have sampled it. Although a query request may not return all messages in the queue, if you keep getting messages from the queue, all the servers will be sampled and you will get all the messages.


# Migrate applications using the old API version as soon as possible.
To minimize the trouble, use the latest version of the API when using SQS to create a new application.




# Before getting started with Amazon Web Services and SQS, you must register an Amazon Web Services Account (see references ). Part 1 of this series details how to register an Amazon Web Services account.


After creating an account, you must enable the Amazon SQS service for the Account:


* Register an Amazon Web Services account.
* Navigate to the SQS homepage.
* Click sign up for Amazon SQS on the right side of the page.
* Provide required information and complete the registration process.




# Interacting with sqs aws sqs Ruby example (http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/SQS.html#queues-instance_method)


* Ruby AWS-SDK gem
Https://github.com/aws/aws-sdk-ruby


* Set some environment variables and reference the Amazon Web services access key.
You can obtain the access key from the Web Services Account Information Page. (Http://aws-portal.amazon.com/gp/aws/developer/account/index.html? Ie = utf8 & Action = Account-links)


Config. yml
access_key_id: xxx
secret_access_key: yyy




* Get a message


Sqs_poll.rb
#!/usr/bin/env ruby
 
require 'yaml'
require 'aws-sdk'
 
config_file = File.join(File.dirname(__FILE__),"config.yml")
config = YAML.load(File.read(config_file))
AWS.config(config)
 
sqs = AWS::SQS.new
queue = sqs.queues.create("my_queue")
queue.poll do |msg|
  puts msg.body
end



* Send a message


Sqs_send.rb

#!/usr/bin/env ruby
 
require 'yaml'
require 'aws-sdk'
 
config_file = File.join(File.dirname(__FILE__),"config.yml")
config = YAML.load(File.read(config_file))
AWS.config(config)
 
# http://rubydoc.info/github/amazonwebservices/aws-sdk-for-ruby/master/AWS/SQS
 
sqs = AWS::SQS.new
queue = sqs.queues.create("my_queue")
 
# http://rubydoc.info/github/amazonwebservices/aws-sdk-for-ruby/master/AWS/SQS/Queue
 
send = lambda { |name, queue|
  while true do
    queue.send_message("#{name}:#{Time.now.to_s}")
    sleep 1
  end
}
 
Thread.new { send.call("t1", queue) }
Thread.new { send.call("t2", queue) }
Thread.new { send.call("t3", queue) }
 
sleep 1000

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.