Created
June 12, 2013 00:45
-
-
Save RhysC/5762111 to your computer and use it in GitHub Desktop.
Azure storage queue (not service bus) demo using mvc and worker role
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller | |
{ | |
public ActionResult SendMessage() | |
{ | |
return View(); | |
} | |
[HttpPost] | |
public ActionResult SendMessage(string content) | |
{ | |
new Messaging.MessagingGateway().SendMessage(content); | |
ViewBag.SentMessage = content; | |
return View(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using (Html.BeginForm()) | |
{ | |
@Html.TextArea("content") | |
<input type="submit" value="Send it!" /> | |
} | |
@if (ViewBag.SentMessage != null) | |
{ | |
<div> | |
Sent Message : | |
<p> | |
@ViewBag.SentMessage | |
</p> | |
</div> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.WindowsAzure; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Queue; | |
namespace MvcWebRole1.Messaging | |
{ | |
public class MessagingGateway | |
{ | |
public void SendMessage(string content) | |
{ | |
// Retrieve storage account from connection string. | |
var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); | |
// Create the queue client. | |
var queueClient = storageAccount.CreateCloudQueueClient(); | |
// Retrieve a reference to a queue. | |
var queue = queueClient.GetQueueReference("myqueue"); | |
// Create the queue if it doesn't already exist. | |
if(!queue.Exists()) | |
throw new Exception("No queue bro!"); | |
// Create a message and add it to the queue. | |
var message = new CloudQueueMessage(content); | |
queue.AddMessage(message); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Net; | |
using System.Threading; | |
using Microsoft.WindowsAzure; | |
using Microsoft.WindowsAzure.Diagnostics; | |
using Microsoft.WindowsAzure.ServiceRuntime; | |
using Microsoft.WindowsAzure.StorageClient; | |
namespace WorkerRole1 | |
{ | |
public class WorkerRole : RoleEntryPoint | |
{ | |
public override void Run() | |
{ | |
// This is a sample worker implementation. Replace with your logic. | |
Trace.WriteLine("WorkerRole1 entry point called", "Information"); | |
// Retrieve storage account from connection string | |
var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); | |
// Create the queue client | |
var queueClient = storageAccount.CreateCloudQueueClient(); | |
// Retrieve a reference to a queue | |
var queue = queueClient.GetQueueReference("myqueue"); | |
queue.CreateIfNotExist(); | |
while (true) | |
{ | |
try | |
{ | |
// Get the next message | |
var retrievedMessage = queue.GetMessage(); | |
if (retrievedMessage == null) | |
{ | |
var current = Debug.IndentSize; | |
Debug.IndentSize = 10; | |
Debug.WriteLine("No message to process"); | |
Debug.IndentSize = current; | |
Thread.Sleep(10000); | |
continue; | |
} | |
var currentIndent = Debug.IndentSize; | |
Debug.IndentSize = 10; | |
var so = Newtonsoft.Json.JsonConvert.SerializeObject(retrievedMessage); | |
Debug.WriteLine(so); | |
Debug.WriteLine(" ------ " + retrievedMessage.AsString); | |
Debug.IndentSize = currentIndent; | |
//Process the message in less than 30 seconds, and then delete the message | |
queue.DeleteMessage(retrievedMessage); | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine(ex); | |
throw; | |
} | |
} | |
} | |
public override bool OnStart() | |
{ | |
// Set the maximum number of concurrent connections | |
ServicePointManager.DefaultConnectionLimit = 12; | |
// For information on handling configuration changes | |
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357. | |
return base.OnStart(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment