Priority Queues With Simple Queue Service
Priority Queues With Simple Queue Service
Q U E U E S E R V I CE
Amazon’s Simple Queue Service (SQS) is a distributed queuing system. SQS is similar to the
straightforward queues, but with some significant differences. Like most distributed systems, SQS
has to deal with consistency across datacenters.
A traditional queue is a collection of items that follows the First-In-First-Out (FIFO) structure.
Traditional queues are created in Java using class constructors:
Queue<String> queue = new Queue<String>();
In traditional queues, items are added to the queue with an Enqueue method and removed from the
queue with a Dequeue method.
queue.offer(“The First message”);
A Priority Queue is a specialization of the queue data structure designed for delivering messages
in a different order. Messages are delivered based on priority, not time.
Priority, as defined by priority queues, is not standard. Priority can be defined per queue. With
Java’s PriorityQueue interface, priority is based on comparisons between the messages, using a
Comparator that is passed in when the queue is constructed. If no Comparator is passed, then the
natural comparison operator is used.
PriorityQueue<String> priorityQueue = new PriorityQueue<String>();
Notice that the message received first is the second one added. This is because “A Second message”
is first alphabetically. The String datatype’s natural comparison is (essentially) alphabetical
comparison.
To create an explicit comparison, a Comparator can be created. The best way to do this is to wrap
the String inside a custom class.
Page 1 of 6
[PRIORITY QUEUES WITH SIMPLE QUEUE SERVICE ] Amazon AWS
public StringMessage(){
this.message = message;
this.priority = priority;
return message;
this.message = message;
return priority;
this.priority = priority;
Page 2 of 6
[PRIORITY QUEUES WITH SIMPLE QUEUE SERVICE ] Amazon AWS
return 1;
return -1;
else
return 0;
});
Finally, we create StringMessage objects and send them to the queue. Notice that explicit priorities
are used.
priorityQueue.offer(msg1);
priorityQueue.offer(msg2);
A queue with SQS is created using a CreateQueue message. The queue can be accessed from any
platform using any language capable of REST or SOAP. After a queue is created, messages are
queued by sending a message to the queue.
AmazonSQS service = new AmazonSQSClient(accessKeyId, secretAccessKey);
request.setQueueName(queueName);
sendRequest.setQueueName( queueName);
Page 3 of 6
[PRIORITY QUEUES WITH SIMPLE QUEUE SERVICE ] Amazon AWS
sendRequest.setMessageBody(“Hello, world”);
To dequeue a message, you receive a message from the queue and then delete it.
request.setMaxNumberOfMessages(1);
request.setQueueName(queueName);
if (response.isSetReceiveMessageResult()) {
delRequest.setQueueName(queueName);
delRequest.setReceiptHandle(message.getReceiptHandle() );
service.deleteMessage(delRequest);
Many distributed queuing implementations are transactional. SQS queues have a visibility timeout.
If the received message is not deleted by the end of the timeout, the message is visible to other
clients. During this timeout, no other client will be able to receive the message.
Page 4 of 6
[PRIORITY QUEUES WITH SIMPLE QUEUE SERVICE ] Amazon AWS
Because SQS queues are distributed, not all systems within Amazon hold the same messages.
Further, using the principal of eventual consistency, it is possible that messages will not be
received in the same order as they are sent in all cases.
Keep in mind that SQS queues are neither FIFO nor based on any a priori priority data. Messages
are assumed to be unordered.
PROBLEMS
The following problems each use a sample data set generated by the attached Java program
“InstallSQS.java”. Follow the instructions in the document “Getting Started with Amazon’s Web
Services for Lessons Plans” for getting an Amazon Web Services account and setting up your
system.
Page 5 of 6
[PRIORITY QUEUES WITH SIMPLE QUEUE SERVICE ] Amazon AWS
Page 6 of 6