public override void Run() { CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); }); //Initialize var storageAccount = CloudStorageAccount.FromConfigurationSetting("ConnectionString"); var queueClient = storageAccount.CreateCloudQueueClient(); var EventsQueue = queueClient.GetQueueReference("earthquakes"); var StationsQueue = queueClient.GetQueueReference("stations"); var ReducerQueue = queueClient.GetQueueReference("reducer"); while (true) { Thread.Sleep(10000); CloudQueueMessage EventMessage = EventsQueue.GetMessage(TimeSpan.FromMinutes(2)); if (EventMessage != null) { QuakeMessage quake = DeserialeMessage(EventMessage); quake.CalculateGeometry(); quake.CalculateCenter(); var service = storageAccount.CreateCloudTableClient().GetDataServiceContext(); service.IgnoreResourceNotFoundException = true; service.IgnoreMissingProperties = true; IQueryable <StationsInfo> StationsList; if (!String.IsNullOrEmpty(quake.StationsGroup)) { StationsList = (from c in service.CreateQuery <StationsInfo>("Stations").AsTableServiceQuery() where c.PartitionKey == quake.StationsGroup select c); } else { StationsList = CalculateGrid(quake); } int Counter = 0; foreach (var item in StationsList) { quake.StationCode = item.Code; quake.Station = item; CloudQueueMessage StationMessage = new CloudQueueMessage(quake.ToXml()); StationsQueue.AddMessage(StationMessage); Counter++; } CloudQueueMessage ReducerMessage = new CloudQueueMessage(String.Format("{0};{1};{2}", quake.EventID.ToLower(), Counter, quake.DeleteMe.ToString())); ReducerQueue.AddMessage(ReducerMessage); EventsQueue.DeleteMessage(EventMessage); } } }
public bool AddMessage(QuakeMessage data) { var service = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=cloudquake;AccountKey=Mm+fR9oqs12+fF/96ThXUtl3eMCkMlYMRvTf+dEP36dQRNqS6oKQoRrtELTF4AWaPEISkWsMBzo65ZI+PU2LGA=="); try { var container = service.CreateCloudBlobClient().GetContainerReference(data.EventID.ToLower()); container.CreateIfNotExist(); container.SetPermissions(new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob }); var queue = service.CreateCloudQueueClient().GetQueueReference("earthquakes"); queue.AddMessage(new CloudQueueMessage(data.ToXml())); return(true); } catch (Exception) { return(false); } }
static void Main(string[] args) { CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { configSetter(ConfigurationManager.AppSettings[configName]); }); var storageAccount = CloudStorageAccount.FromConfigurationSetting("ConnectionString"); var queueClient = storageAccount.CreateCloudQueueClient(); var queue = queueClient.GetQueueReference("earthquakes"); queue.CreateIfNotExist(); var s = queueClient.GetQueueReference("stations"); //s.Delete(); s.CreateIfNotExist(); var d = queueClient.GetQueueReference("reducer"); // d.Delete(); d.CreateIfNotExist(); InitializeStationsTable(storageAccount); var service = storageAccount.CreateCloudTableClient().GetDataServiceContext(); var stations = from c in service.CreateQuery <StationsInfo>("Stations").AsTableServiceQuery() select c; var ConfigurationContainer = storageAccount.CreateCloudBlobClient().GetContainerReference("config"); StreamWriter sw = new StreamWriter("sta_grid.txt"); int i = 0; foreach (var item in stations) { switch (item.SoilCondition.ToLower()) { case "rock": i = 0; break; case "soil": i = 1; break; case "soft-soil": i = 2; break; } sw.WriteLine(String.Format("{0} {1} {2}", item.Latitude, item.Longitude, i)); } sw.Close(); ConfigurationContainer.GetBlobReference("sta_grid.txt").UploadFile("sta_grid.txt"); string message = String.Empty; QuakeMessage data = new QuakeMessage() { Depth = 0.7, Dip = 43, Stress = 55, Strike = 88, FaultType = "N", Flag = "1", StationsGroup = "benchmark", Lat = 40.45, Lon = 23, Magnitude = 5.1, EventID = "testmessage" }; queue.AddMessage(new CloudQueueMessage(data.ToXml())); System.Threading.Thread.Sleep(5000); using (Stream stream = new MemoryStream()) { StreamWriter writer = new StreamWriter(stream); writer.Write(queue.GetMessage().AsString); writer.Flush(); stream.Position = 0; object result = new XmlSerializer(typeof(QuakeMessage)).Deserialize(stream); QuakeMessage ddd = (QuakeMessage)result; Console.WriteLine(ddd.StationsGroup); } QuakeMessage ss = new QuakeMessage(); //queue.GetMessage().AsString() string title; while (true) { message = String.Empty; title = String.Empty; Console.WriteLine("Title : "); title = Console.ReadLine(); message += title + ";"; Console.WriteLine("Mangitude : "); message += Console.ReadLine() + ";"; Console.WriteLine("Latitude : "); message += Console.ReadLine() + ";"; Console.WriteLine("Longitude"); message += Console.ReadLine() + ";"; Console.WriteLine("Depth : "); message += Console.ReadLine(); // CloudQueueMessage m = new CloudQueueMessage(data.ToBinary()); // queue.AddMessage(new CloudQueueMessage(message)); Console.ReadKey(); try { var blobClient = storageAccount.CreateCloudBlobClient(); var blobContainer = blobClient.GetContainerReference(title); blobContainer.CreateIfNotExist(); blobContainer.SetPermissions(new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob }); } catch (Exception x) { Console.WriteLine(x.InnerException.Message); } } }