Module 4: Configuring the System to Use Simple Queue Service

Amazon Simple Queue Service (SQS) is a reliable, scalable, and fully-managed message queuing services for making reliable communication among distributed software components and microservices at any scale. It is best practice design for modern application system because of each individual components perform a discrete function while building the application which helps to improve reliability and scalability. SQS makes the application system components simple and cost effective to decouple and coordinate the components of a cloud application.

You can use Amazon SQS to send, store, and receive message between applications components at any volume, at any level of throughput, without losing messages or requiring other services to be available.

Through Two types of queue, SQS provides services. One is SQS Standard and another is SQS FIFO Queue. SQS Standard queue provides maximum throughput, best-effort ordering, and at-least-once delivery. SQS FIFO Queues are designed to guarantee that messages are processed exactly once, in the exact order that they are sent, with limited throughput.

The Benefit of SQS:  With SQS, there is no upfront cost, no need to acquire, install, and configure messaging software, and no time-consuming build-out and maintenance of supporting infrastructure.

SQS lets you decouple application components so that they run and fail independently, increasing the overall fault tolerance of the system.

Task list for the module 4: 

  • To create queue in the Amazon SQS
  • To Test the queue by sending a message in Queue Actions
  • Editing and changing the application source code
  • To add order and check out, and buy order to cart and testing queue from SQS services.
  •  to add a new application to the project that can pull the orders from the queue

The architecture of the project:

diagram6

Steps for the task: Creating queue in the Amazon SQS

In this task, I will demonstrate how to create a queue using SQS in the Amazon cloud Platform. The queue will help the application system to coordinate with the database to hold and send the message.

  1. You need to go to Amazon Simple Queue Service (SQS) from Services menu in the Amazon Management console. Click “Get Stated Now” for creating the queue. Put the name in the new queue: “dinoorders“. Make sure you’ve chosen the right region.1.jpg
  2. Choose Standard queue and click “Configure Queue“. Leave the Queue attributes with default parameters. Then Click “Create Queue“.2.jpg3.jpg

Steps for the task: Testing the queue by sending a message in Queue Actions

A queue has been created. Now I will show you how to test the newly created queue is working or not by sending a message.

  1. From the Queue Actions tab, you need to select “Send a Message” and click on it.4.jpg5.jpg
  2. You will get the following wizard after sending your message.6.jpg
  3. Now you need to refresh the queue screen and check that your message is available or not! I can view that one(1) message available for my dinoorders queue.7.jpg
  4. Now you will view the receiving message by right click the mouse on the queue and then select View/Delete Message option.8
  5. After clicking on View/Delete Message option, You will get a wizard called “View/Delete Messages in dinoorders”. Now I will pull the message by clicking on “StartPolling for the message” from this screen.9.jpg
  6. Now you can view the message exact message that you had sent previously. Pulling message means It will fetch all the message that has been sent to the queue or those messages are still in the queue.10.jpg
  7. After complete, your test successfully,  Delete the test message and stop polling.11.jpg
  8. After deleting the message from the poll queue, now you can see now messages are available in the dinoorders queue.12.jpg

Steps for the task: Editing and changing the application source code

In this task, I will demonstrate to apply some changes in application source code of NET702.DinoStore-> secure-> Checkout.aspx.cs file. I will insert a new code with order information into the queue as well as I will also remove the previously existing code because the previous code doesn’t have the scale and it directly inserts the order information data into the database. The new replacing code will help my system to handle bursts.

  1. Open NET702.DinoStore project on the Microsoft Visual studio, Go to the checkout. aspx.cs file. 13.jpg
  2. Now remove the code from ‘protected void btnPurchase_Click(object sender, EventArgs e)’ down to and including Response.Redirect(“PaymentConfirmation.aspx”);’. (line 66 to 126 inclusive)  in Checkout.aspx.cs. 14.jpg
  3. Replace the delete code with the following code: 

protected void btnPurchase_Click(object sender, EventArgs e)
{
//get shopping cart
ShoppingCart cart = Session.Contents[“cart”] as ShoppingCart;
//get user id
MembershipUser user = Membership.GetUser();
string userId = user.ProviderUserKey.ToString();
//add payment info
cart.CcType = ddlCcType.SelectedItem.Text;
cart.CcNumber = txtCcNumber.Text;
cart.CcExpiration = txtExpire.Text;
//create message for queue
using (AmazonSQSClient client = new AmazonSQSClient())
{
var jsoncart = Newtonsoft.Json.JsonConvert.SerializeObject(cart);
SendMessageRequest request = new SendMessageRequest();
request.QueueUrl = “YOUR QUEUE URL HERE”;
request.MessageBody = jsoncart;
SendMessageResponse response = client.SendMessage(request);
}
//clear out cart
cart.Items.Clear();
Session[“cart”] = cart;
//send user to confirmation page
Response.Redirect(“PaymentConfirmation.aspx”);15.jpg

4. In this step, I will add the following new ‘using’ statements under the existing set: using Amazon.SQS;
using Amazon.SQS.Model;
using Newtonsoft.Json; Those “using” codes are called function of the code. It will call directories of the function classes objects that are registered in those directories. 16.jpg

5. Now I will install NuGet packages in the references object on the DinoStore. This package is for the Json.NET package that will allow the shopping cart to be serialised as a JSON object and added to the queue. The” json.net” using the function I have called from the check.aspx.cs file.17.jpg

6.  Search for and install the Json.NET package on the Browser, and select Newtonsoft_Json and install it.18.jpg19.jpg20.jpg

7.Now you need to go “the web.config” file to add your credentials such as regions, access keys, security keys. Those credentials elements you will get AWS accounts.  Insert the following code after ‘ValidationSettings: UnobtrusiveValidationMode’

21.jpg

Note: Remember that we will remove this credentials from our source code when we will move to the cloud server. We will AWS IAM services because it is not good practice to use credentials into the code.

Steps for the task: To add order and check out, and buy order to cart and testing queue from SQS services.

In this task, I will process the order by login my user ID. I will add items to cart, check-out and buy some dinosaurs.

  1. Login your website by your user id. 22
  2. Now add some items to the cart. And check out them for buy. Then click “buy” for the items.23.jpg24.jpg25.jpg

If you check your cloud system fromSQS dinoorders queue, You will see some message in queue order.26.jpg27.jpg

This queue orders available with request file, It helps the application to balancing the load from web application side. SQS is acting as the buffer memory and reduce database overloading processing in the real time.

Adding and buying dinosaurs from the website shows that all configuration and changes that I’ve made till are working properly.

Steps for the task: to add a new application to the project that can pull the orders from the queue

In this task, I will add an order processor on the Microsoft Visual studio application. That order process will create an application for the queue services. The purpose of the queue application is that if request message comes from the web application or websites to the queue, the application polling the requested automatically in the background process.  The benefit is that you do not go to the SQS services every time to handle the queue manually. As a result, your application system processing performance will be increased.

I will do some changes in my application source code to demonstrate this process now.

  1. To the add the  NET702.DinoStore.OrderProcessor under your solution manager, Go to your solution explorer and right click your mouse, Click add on the Existing project and give the location of the file.  The location of my file is: (E:\NET702orginal\NET702.DinoStore.OrderProcessor)21.jpg
  2.  Now you need to insert your dinoorders queue URL into the NET702.DinoStore.OrderProcessor ‘Program.cs’ code. Therefore, your application source code able to establish the connection with your SQS services in the AWS cloud. You have to put this URL in the two places of your code: “request.QueueUrl” and “batchRequest.QueueUrl”. 34.jpg
  3. Now ‘App.config file in the “orderprocessor” from your solution explorer.5.jpg
  4. You need to add AWSRegion, and Access keys and Secret key to the point of “Add key“. and Change your StoreSqlDb server connection string to your “dinostoreinstance” URL, and add your DB username and password to this line.6.jpg
  5. After complete all those changes on the NET702.DinoStore.OrderProcessor, Right click on NET702.DinoStore.OrderProcessor project and select ‘Set as Startup Project’7.jpg
  6. Right-click the NET702.DinoStore.OrderProcessor from the solution explorer, choose Properties-Signing and untick the ‘Sign the Click Once manifestsInked9_LI
  7. To run the “NET702.DinoStore.OrderProcessor” project in the visual studio. First, you need to build the solutions, otherwise, You will get an error that program doesn’t get the exe file. After complete “Build Solution”, click “Start” at the top of the visual studio.11.jpg
  8. When you will click on “Start” program in visual studio. An application will be running like the following Screenshot.  Note: I already bought some dinosaurs from my websites with my user credentials that made a buying request message in the queue.  This application will show that a request has been received with a total number of count, And it will automatically be deleted from the queue. After deleting the queue message, It will again start for polling to fetch the message from the queue. quue app
  9. We can also view order details from the database through MySQL Workbench. That means SQS services polling data from the queue through the queue application. It has done automatic execution and delete the request message from the queue and store it in the database. And this running application helps the queue table to maintain SQS polling service automatic.12.jpg

Problem and solution:

When you will implement module 4, you will come across different kind of problems. Here I am going to mention what kind of problems I faced and with solutions.

Problem 1:  When you will debug your code after editing checkout.aspx.cs file, you may see the following error: CS0103: The name Newtonsoft doesn’t exist in the current context.error-1.jpgerror-2.jpg

Solution: To solve this error, You need to install “Newtonsoft.json” from “Manage Nuget package“. After installing Newtonsoft.json the problem must be solved. If you still get the above error, go to “Reference“, right click on your mouse.  and add references, you will get reference manager wizard and add Json.NET.NET 4.0. Your problem will be solved.Untitled.jpgerror2-solve.jpg

Problem 2: If you get the following configuration error. Follow the solution I’ve given below.error.jpg

Solution: Install the “AWS Session Provider” from the “Manager Nuget package“. The session regarding all problem will be solved.error-solve.jpg

Problem 3: If you get the following error after “build solution“, or “debugging your code”. Follow the following solution I’ve been given below.8.jpg

Solutions: Click the property of the order Processor file, from application wizard signing and untick the “Sign Click Once manifests. The above problem will be solved.

Inked9_LI.jpg

Problem 4: If get a message like that during polling message through your running application ” dinostoremembershipdb .orders” table not found or ” dinostoremembershipdb.items table doesn’t exist. 

Solution: Add the correct schema name before table name into the insert query of the code of program.cs. The problem will be solved. Your running application will able to fetch message without any exceptional handle errors message.polling error.jpg

The other errors you can get during configuring module 4:

Error 1: The type or namespace name ‘Amazon’ could not be found (are you missing a using directive or an assembly reference?)

Analysis: The compiler is unable to find the definition of “Amazon” from the available resources to be able to build the project (NET702.DinoStore.Order.Processor). So the hint here is the name “Amazon”. In the previous lab (Lab 3), we were instructed to make sure that a package (AWS SDK for .NET) should be installed in our environment. Now, we know that AWS stands for Amazon Web Services. Thus, we can infer that the package AWS SDK must contain the definition of the namespace called “Amazon”. Now let us verify if NET702.DinoStore.Order.Processor Project has the AWS SDK package by right-clicking on it and choosing “Manage NuGet Packages”. In the newly opened tab, type AWSSDK to search for the package that we are looking. In my case, an older version of the package was installed but a newer version is already available. What we can conclude from here is that the namespace “Amazon” is not yet defined in our current AWSSDK package. Thus we need to upgrade to the newer version. After installing the latest version of AWSSDK, build the project again and the error related to the amazon should be resolved. 

Solution: Install the latest AWSSDK package. Note that this will solve similar errors related to this package.

Error 2: ‘ReceiveMessageRequest’ does not contain a definition for ‘AttributeName’ and no extension method ‘AttributeName’ accepting a first argument of type ‘ReceiveMessageRequest’ could be found (are you missing a using directive or an assembly reference?)

Analysis: As mentioned in the error description, ‘ReceiveMessageRequest’ does not contain a definition for ‘AttributeName’. So we need to inspect the definition of ‘ReceiveMessageRequest’ to verify if it has a member named ‘AttributeName’. To get to the definition of ‘ReceiveMessageRequest’: 1. First, double click the error at the bottom of the visual studio. This will redirect you to the line where the error was detected. 2. Now right click ‘ReceiveMessageRequest’ and select Go To Definition. In the definition, we can observe that ‘ReceiveMessageRequest’ class has a member named ‘AttributeNames’ and not ‘AttributeName’. This means that code in Program.cs is incorrect.

Solution: Modify request.AttributeNametorequest.AttributeNames. It should have letter “S”.

Error 3: ‘ReceiveMessageResult’ does not contain a definition for ‘Message’ and no extension method ‘Message’ accepting a first argument of type ‘ReceiveMessageResult’ could be found (are you missing a using directive or an assembly reference?)

Solution:  Modify response.ReceiveMessageResult.Message. Count to response.ReceiveMessageResult.Messages.  Put the letter “s” after the message. There is ‘S’ missing.

The next Module I will discuss “Adding EC2 Virtual Machines and Deploying the WebApp”

Thank you 🙂 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s