A queue is a temporary data structure used to store messages waiting to be processed. Amazon Simple queue Services (Amazon SQS) is a highly available scalable message queue that supports WEB services. The main benefits of Amazon SQS include:
Cloud-based solutions. Managed by Amazon without the need for a private infrastructure or professional support knowledge.
Internet based. Any client connected to the Internet can access the service through a WEB service, thus supporting business to business (business-to-business) integration.
Redundancy. The service stores all messages on multiple servers to provide high availability and fault tolerance.
Multiple concurrent reads/writes. Amazon SQS supports multiple processes reading and writing a queue at the same time, and locks messages when processing windows to prevent two clients from processing a single message at the same time.
Configurable. By using the Amazon SQS service, you can design and lock a window based on the processing requirements of the messages stored in the queue. A lock window prevents two queue readers from processing the same queue item at the same time. For longer-processing queue items, you need to lock the window for a longer period of time. The lock window is controlled by the visibility timeout parameter, which you can configure for each queue.
Easy to use API. It provides API wrappers for common languages, including Java and Microsoft. NET platforms, to support rapid development and seamless integration into existing applications.
Low-cost solutions. Companies only need to pay for the bandwidth they use for HTTP requests; Amazon SQS does not charge additional fees.
Before starting Amazon SQS development, it is helpful to understand some of its features. If you're not aware of these features, you may be frustrated or confused when you first start using Amazon SQS.
First, Amazon cannot guarantee the order in which the queue items are processed. This means that advanced first out (FIRST-IN-FIRST-OUT,FIFO) processing is not guaranteed, which is common in many Message Queuing implementations. Amazon only guarantees that all messages are distributed.
The second major feature of the Amazon SQS is final consistency. The main features of large database systems are consistency, high availability, and scalability. Instead of focusing on all 3 features, Amazon focuses on high availability and scalability, and then provides final consistency on this basis. This means that Amazon achieves high availability and scalability by sending all of the messages to multiple servers. Amazon guarantees that all the messages will eventually be distributed, but there is no guarantee when they will be distributed. From a practical standpoint, this means that if you send 3 messages to a queue, you may not receive all 3 messages the next time you try to receive the messages. You may receive all 3 messages in a read, or you receive the first two messages in a read, and you receive a third message in the second read. If you continue to poll the queue, you will eventually receive all 3 messages.
To start using Amazon SQS, you must obtain the Amazon SQS API Library based on the language you use. Amazon provides a library for all common languages, such as Perl, Microsoft Visual basic®.net, C #, Java, and PHP. These libraries are open source and easy to use.
Using the Amazon SQS through the Java language
Now you first learn how to use the Java language to create queues, send messages, and receive messages. The first step is to create an Amazon SQS queue. The code in Listing 1 shows how to create HTTP clients, instantiate Createqueuerequest objects, and call queue creation requests for Amazon SQS. The Access key ID (consisting of 20 letters and numbers) is the key required to request authentication or read the queue item. To create or manipulate a queue item, you need to use Secret access key (consisting of 40 letters and numbers). You will receive these keys when you register Amazon.
Listing 1. Create Amazon SQS queues
String queueName = "TestQueue";
// create http client
AmazonSQS service = new AmazonSQSClient(accessKeyId, secretAccessKey);
// instantiate create queue request
CreateQueueRequest request = new CreateQueueRequest();
request.setQueueName(queueName);
request.setDefaultVisibilityTimeout(30);
// execute create queue operation and get the server response
System.out.print("Creating Queue: " + queueName);
CreateQueueResponse response = service.createQueue(request);
if (response.isSetCreateQueueResult()) {
System.out.print("Create Queue Result:");
CreateQueueResult createQueueResult = response.getCreateQueueResult();
if (createQueueResult.isSetQueueUrl()) {
System.out.print("Queue Url: " + createQueueResult.getQueueUrl());
}
}