Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Hyperscale Messaging in .NET with Amazon's Simple Queuing Service (SQS) : Page 4

Amazon SQS makes queuing messages across organizations over HTTP a snap with its Web service interface and Amazon-backed infrastructure. This article walks you through the process to get started using Amazon SQS to create and control queues and messages.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Add an Amazon SQS Web Reference
Add a Web reference to Amazon SQS WSDL by pointing to http://queue.amazonaws.com/doc/2006-04-01/QueueService.wsdl as shown in Figure 9. This will create com.amazonws.queue.* proxies to access the Amazon SQS service.

Figure 9. Web Reference to Amazon SQS WSDL: The figure shows how to find the Amazon SQS WSDL file to add the Web reference to your project.
Figure 10. The Completed Dashboard: The figure shows the completed Amazon SQS dashboard form, which lets you perform common queue and message operations.
The Amazon SQS Dashboard:
You are now ready to start using Amazon SQS. Among other files the downloadable code contains the Windows Form frmHyper (the file Hyper.cs—see Figure 10) and a wrapper class for Amazon SQS specific calls named AmazonSQS.cs.

The dashboard lets you list, create, and delete queues (delete is available only when a Queue is empty), and create and delete messages.

Most of the code is self-explanatory, but I'll go over couple of functions in the AmazonSQS.cs file. The getQueueNames() method shown below retrieves a list of queues. As shown, you first create an instance of QueueServiceWse (note the "Wse" suffix). All calls require you to construct parameters, which is this case is ListQueue. You pass this parameter to the ListQueues() function which returns the response, packaged as a ListQueueResponse instance. You then check for success and return the Queue list to the caller.

You will be prompted for a certificate password. This is the same password you supplied when you installed the certificate on Windows.

The getQueueService() method is a convenience function that creates an instance of QueueServiceWse and sets the AmazonSQS URL and the name of the WSE application policy you want to use.

public static string[] getQueueNames() { com.amazonaws.queue.QueueServiceWse queueService = getQueueService(); com.amazonaws.queue.ListQueues listQueues = new com.amazonaws.queue.ListQueues(); com.amazonaws.queue.ListQueuesResponse listQueueResponse = queueService.ListQueues(listQueues); if (listQueueResponse == null || listQueueResponse.ResponseStatus.StatusCode != SUCCESS) return null; string[] queueNames = listQueueResponse.QueueUrl; return queueNames; } private static com.amazonaws.queue.QueueServiceWse getQueueService() { com.amazonaws.queue.QueueServiceWse queueService = new com.amazonaws.queue.QueueServiceWse(); queueService.Url = "http://queue.amazonaws.com/"; queueService.SetPolicy("HyperscalePolicy"); return queueService; }

To use the sendMessage() function, you first create an instance of MessageQueueWse (note the Wse suffix again) that points to a specific queue. Then you create an instance of Message, load the message with string content, and send it using the MessageQueueWse.sendMessage() method.

The getMessageQueueWse method is another convenience function that sets the WSE policy and Queue name. Here's the code for both methods:

public static string sendMessage(string queueURL, string message) { com.amazonaws.queue.MessageQueueWse messageQueue = getMessageQueue(queueURL); ; com.amazonaws.queue.SendMessage sendMessage = new com.amazonaws.queue.SendMessage(); sendMessage.MessageBody = message; com.amazonaws.queue.SendMessageResponse response = messageQueue.SendMessage(sendMessage); if (response == null || response.ResponseStatus.StatusCode != SUCCESS) { return null; } return response.MessageId; } private static com.amazonaws.queue.MessageQueueWse getMessageQueue(string queueURL) { com.amazonaws.queue.MessageQueueWse messageQueue = new com.amazonaws.queue.MessageQueueWse(); messageQueue.SetPolicy("HyperscalePolicy"); messageQueue.Url = queueURL; return messageQueue; }

The attached code contains a number of other useful functions that you can experiment with.

While Amazon SQS provides good basic queuing capabilities, it still does not provide capabilities such as such as journaling, acknowledgements, publish-subscribe, routing, etc that organizations have come to expect from messaging infrastructure. However, given the clout and weight Amazon carries, don't be surprised if such features begin showing up in the future as part of Amazon SQS.

Gautam Shah has been developing and integrating applications for over a decade. He has designed patented solutions that enable information sharing between government agencies. His interests center around J2EE, .NET, Open Source, BizTalk, Indian cooking, and anything that's vaguely interesting on the New York Times non-fiction bestseller list.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date