public static void Run([CosmosDBTrigger( databaseName: "IoT", collectionName: "IoT", ConnectionStringSetting = "DBConnection", LeaseCollectionName = "leases")] IReadOnlyList <Document> documents, TraceWriter log) { Microsoft.WindowsAzure.Storage.Queue.CloudQueueClient queueClient; Microsoft.WindowsAzure.Storage.Queue.CloudQueue queue; Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=scmrstorage;AccountKey=FtW5Qlz/5rWiqX0MPlGO0X2anGs5t7ea/H/ZkdcIEHlTA9isEinpscnuuhw8GwKR+7+Eo2IDRG1jwdMoDsRTqg==;EndpointSuffix=core.windows.net"); queueClient = storageAccount.CreateCloudQueueClient(); queue = queueClient.GetQueueReference("trafficqueue"); foreach (var doc in documents) { if (doc.GetPropertyValue <string>("accident") == "true") { string m = String.Format("{{ \"lat\": {0}, \"long\": {1}, \"carId\": \"{2}\" }}", doc.GetPropertyValue <string>("lat"), doc.GetPropertyValue <string>("longitude"), doc.GetPropertyValue <string>("carid")); Microsoft.WindowsAzure.Storage.Queue.CloudQueueMessage message = new Microsoft.WindowsAzure.Storage.Queue.CloudQueueMessage (m); queue.AddMessage(message); } } }
static BsmController() { Trace.TraceInformation("[TRACE] Entering BsmController::BsmController() static initializer..."); // NOTE: Need to fully qualify System.Configuration to disambiguate from ApiController.Configuration //string strStorageAccountConnectionString = // System.Configuration.ConfigurationManager.AppSettings["StorageAccountConnectionString"]; string strStorageAccountConnectionString = Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("StorageAccountConnectionString"); if (strStorageAccountConnectionString == null) { Trace.TraceError("Unable to retrieve storage account connection string"); } else if (strStorageAccountConnectionString.Length <= 0) { Trace.TraceError("Storage account connection string empty"); } else //connect to the cloud storage account { try { srStorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(strStorageAccountConnectionString); } catch (Exception e) { Trace.TraceError("Exception occurred when parsing storage account connection string\n{0}\n{1}", strStorageAccountConnectionString, e.Message); } if (srStorageAccount != null) { srCloudQueueClient = srStorageAccount.CreateCloudQueueClient(); srBsmQueue = srCloudQueueClient.GetQueueReference(srBsmQueueName); try { if (srBsmQueue.CreateIfNotExists()) { Trace.TraceInformation("Created Azure BSM queue '{0}'", srBsmQueueName); } else { Trace.TraceInformation("Got reference to existing BSM queue '{0}'", srBsmQueueName); } } catch (Exception e) { Trace.TraceError("Exception occurred when creating queue for inbound BSM bundles\n{0}", e.Message); srBsmQueue = null; } } } Trace.TraceInformation("[TRACE] Exiting BsmController::BsmController() static initializer..."); return; }
} // DeleteQueueItem /// <summary> /// Creates the storage and gets a reference (once) /// </summary> public static Microsoft.WindowsAzure.Storage.Queue.CloudQueue ConnectToQueueStorage(string queueName, string cloudAccountName, string cloudKey) { try { Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials( cloudAccountName, cloudKey); Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true); Microsoft.WindowsAzure.Storage.Queue.CloudQueueClient queueStorage = storageAccount.CreateCloudQueueClient(); queueStorage.DefaultRequestOptions.RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(TimeSpan.FromSeconds(DEFAULT_SAVE_QUEUE_RETRY_WAIT_IN_MILLISECONDS), DEFAULT_SAVE_QUEUE_RETRY_ATTEMPTS); queueStorage.DefaultRequestOptions.ServerTimeout = new TimeSpan(0, DEFAULT_SAVE_AND_READ_QUEUE_TIMEOUT_IN_MINUTES, 0); Microsoft.WindowsAzure.Storage.Queue.CloudQueue cloudQueue = queueStorage.GetQueueReference(queueName); cloudQueue.CreateIfNotExistsAsync(); return(cloudQueue); } catch (Exception ex) { throw new Exception("Storage services initialization failure. " + "Check your storage account configuration settings. If running locally, " + "ensure that the Development Storage service is running. \n" + ex.Message); } } // ConnectToQueueStorage
public static CloudQueue GetQueue(string name) { Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = GetStorageAccount(); CloudQueueClient client = storageAccount.CreateCloudQueueClient(); CloudQueue queue = client.GetQueueReference(name); queue.CreateIfNotExists(); return(queue); }
/* * Member Methods */ public override bool OnStart() { Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.Changed += RoleEnvironment_Changed; Trace.TraceInformation("[TRACE] Entering BsmWorkerRole::OnStart()..."); ServicePointManager.DefaultConnectionLimit = 12; //maximum number of concurrent connections string strStorageAccountConnectionString = Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("StorageAccountConnectionString"); if (strStorageAccountConnectionString == null) { Trace.TraceError("Unable to retrieve storage account connection string"); } else if (strStorageAccountConnectionString.Length <= 0) { Trace.TraceError("Storage account connection string empty"); } else //connect to the cloud storage account { Trace.TraceInformation("[INFO] Retrieved StorageAccountConnectionString for BsmWorkerRole\n{0}", strStorageAccountConnectionString); try { srStorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(strStorageAccountConnectionString); } catch (Exception e) { Trace.TraceError("Exception occurred when parsing storage account connection string\n{0}\n{1}", strStorageAccountConnectionString, e.Message); } if (srStorageAccount != null) { srCloudQueueClient = srStorageAccount.CreateCloudQueueClient(); srBsmQueue = srCloudQueueClient.GetQueueReference(srBsmQueueName); try { if (srBsmQueue.CreateIfNotExists()) { Trace.TraceInformation("Created Azure BSM queue '{0}'", srBsmQueueName); } else { Trace.TraceInformation("Got reference to existing BSM queue '{0}'", srBsmQueueName); } } catch (Exception e) { Trace.TraceError("Exception occurred when creating queue for inbound BSM bundles\n{0}", e.Message); srBsmQueue = null; } BsmTimeTableLogger.Initialize(srStorageAccount, srBsmTimeTableName); } } string strDatabaseConnectionString = Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("InfloDatabaseConnectionString"); if (strDatabaseConnectionString == null) { Trace.TraceError("Unable to retrieve database connection string"); } else if (strDatabaseConnectionString.Length <= 0) { Trace.TraceError("Database connection string empty"); } else //connect to the database { Trace.TraceInformation("[INFO] Retrieved InfloDatabaseConnectionString for BsmWorkerRole\n{0}", strDatabaseConnectionString); srDbContext = new InfloDbContext(strDatabaseConnectionString); } BsmTimeTableLogger.Enabled = IsStatisticalLoggingEnabled(); BsmTimeTableLogger.MinimalLoggedElapsedTime = GetMinimalLoggedElapsedTime(); Trace.TraceInformation("[TRACE] Exiting BsmWorkerRole::OnStart()..."); return(base.OnStart()); }
} // PutQueueItem /// <summary> /// Creates the storage and gets a reference (once) /// </summary> private static void InitializeStorage(string queueContainer) { string containerName = queueContainer.ToString().ToLower(); // must be lower case! if (storageInitializedDictionary.ContainsKey(containerName) && storageInitializedDictionary[containerName] == true) { return; } lock (gate) { if (storageInitializedDictionary.ContainsKey(containerName) && storageInitializedDictionary[containerName] == true) { return; } try { Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials( Sample.Azure.Common.Setting.SettingService.CloudStorageAccountName, Sample.Azure.Common.Setting.SettingService.CloudStorageKey); Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageBlobEndPoint), new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageQueueEndPoint), new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageTableEndPoint), new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageFileEndPoint)); Microsoft.WindowsAzure.Storage.Queue.CloudQueueClient queueStorage = storageAccount.CreateCloudQueueClient(); queueStorage.DefaultRequestOptions.RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(TimeSpan.FromSeconds(DEFAULT_SAVE_QUEUE_RETRY_WAIT_IN_MILLISECONDS), DEFAULT_SAVE_QUEUE_RETRY_ATTEMPTS); int queueSaveTimeoutInMinutes = DEFAULT_SAVE_AND_READ_QUEUE_TIMEOUT_IN_MINUTES; string timeOutOverRide = Sample.Azure.Common.Setting.SettingService.SaveAndReadQueueTimeoutInMinutes; if (timeOutOverRide != null) { queueSaveTimeoutInMinutes = int.Parse(timeOutOverRide); } queueStorage.DefaultRequestOptions.ServerTimeout = TimeSpan.FromMinutes(queueSaveTimeoutInMinutes); queueStorage.DefaultRequestOptions.ServerTimeout = new TimeSpan(0, DEFAULT_SAVE_AND_READ_QUEUE_TIMEOUT_IN_MINUTES, 0); Microsoft.WindowsAzure.Storage.Queue.CloudQueue cloudQueue = queueStorage.GetQueueReference(containerName); cloudQueue.CreateIfNotExists(); queueStorageDictionary.Add(containerName, queueStorage); storageInitializedDictionary.Add(containerName, true); } catch (Exception ex) { throw new Exception("Storage services initialization failure. " + "Check your storage account configuration settings. If running locally, " + "ensure that the Development Storage service is running. \n" + ex.Message); } } // lock } // InitializeStorage