/// <summary> /// Registers a change feed observer to update changes read on /// change feed to destination collection. Deregisters change feed /// observer and closes process when enter key is pressed /// </summary> /// <returns>A Task to allow asynchronous execution</returns> private async Task RunChangeFeedHostAsync() { string monitoredUri = ConfigurationManager.AppSettings["CosmosDBEndpointUri"]; string monitoredSecretKey = ConfigurationManager.AppSettings["CosmosDBAuthKey"]; string monitoredDbName = ConfigurationManager.AppSettings["DatabaseName"]; string monitoredContainerName = ConfigurationManager.AppSettings["ContainerName"]; // Source collection to be monitored for changes DocumentCollectionInfo documentCollectionInfo = new DocumentCollectionInfo { Uri = new Uri(monitoredUri), MasterKey = monitoredSecretKey, DatabaseName = monitoredDbName, CollectionName = monitoredContainerName }; string leaseUri = ConfigurationManager.AppSettings["LeaseUri"]; string leaseSecretKey = ConfigurationManager.AppSettings["LeaseSecretKey"]; string leaseDbName = ConfigurationManager.AppSettings["LeaseDbName"]; string leaseContainerName = ConfigurationManager.AppSettings["LeaseContainerName"]; // Lease Collection managing leases on each of the underlying shards of the source collection being monitored DocumentCollectionInfo leaseContainerInfo = new DocumentCollectionInfo { Uri = new Uri(leaseUri), MasterKey = leaseSecretKey, DatabaseName = leaseDbName, CollectionName = leaseContainerName }; DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory(this.DocumentClient); ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions(); int feedPollDelayInSeconds = int.Parse(ConfigurationManager.AppSettings["FeedPollDelayInSeconds"]); feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(240); feedProcessorOptions.LeaseExpirationInterval = TimeSpan.FromSeconds(240); feedProcessorOptions.FeedPollDelay = TimeSpan.FromMilliseconds(feedPollDelayInSeconds * 1000); feedProcessorOptions.StartFromBeginning = true; feedProcessorOptions.MaxItemCount = 2000; ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder(); builder .WithHostName(this.HostName) .WithFeedCollection(documentCollectionInfo) .WithLeaseCollection(leaseContainerInfo) .WithProcessorOptions(feedProcessorOptions) .WithObserverFactory(new DocumentFeedObserverFactory(this.DocumentClient)); var result = await builder.BuildAsync(); await result.StartAsync().ConfigureAwait(false); }
// Boilerplate from the ChangeFeedProcessor library. public async Task RunChangeFeedHostAsync() { string hostName = "ChatHost" + DateTime.Now.Ticks.ToString(); // The collection to be observed is registered here. DocumentCollectionInfo documentCollectionInfo = new DocumentCollectionInfo { Uri = new Uri(Keys.CosmosDBUri), MasterKey = Keys.CosmosDBKey, DatabaseName = GroupSelectionPageViewModel.SelectedDBName, CollectionName = "Messages" }; // The lease is a collection where the changes are temporary stored. DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo { Uri = new Uri(Keys.CosmosDBUri), MasterKey = Keys.CosmosDBKey, DatabaseName = GroupSelectionPageViewModel.SelectedDBName, CollectionName = "Lease" }; DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory(); ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions(); feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15); feedProcessorOptions.StartFromBeginning = true; ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder(); builder .WithHostName(hostName) .WithFeedCollection(documentCollectionInfo) .WithLeaseCollection(leaseCollectionInfo) .WithProcessorOptions(feedProcessorOptions) .WithObserverFactory(new DocumentFeedObserverFactory()); // A new ChangeFeedProcessor is built and then run asynchronously. var result = await builder.BuildAsync(); await result.StartAsync(); }
// Boilerplate from the ChangeFeedProcessor library. public async Task RunChangeFeedHostAsync() { string hostName = "FlashHost" + DateTime.Now.Ticks.ToString(); DocumentCollectionInfo documentCollectionInfo = new DocumentCollectionInfo { Uri = new Uri(Keys.CosmosDBUri), MasterKey = Keys.CosmosDBKey, DatabaseName = GroupSelectionPageViewModel.SelectedDBName, CollectionName = "Flashcards" }; DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo { Uri = new Uri(Keys.CosmosDBUri), MasterKey = Keys.CosmosDBKey, DatabaseName = GroupSelectionPageViewModel.SelectedDBName, CollectionName = "FlashLease" }; DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory(); ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions(); feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15); feedProcessorOptions.StartFromBeginning = true; ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder(); builder .WithHostName(hostName) .WithFeedCollection(documentCollectionInfo) .WithLeaseCollection(leaseCollectionInfo) .WithProcessorOptions(feedProcessorOptions) .WithObserverFactory(new DocumentFeedObserverFactory()); var result = await builder.BuildAsync(); await result.StartAsync(); }
/// <summary> /// Registers change feed observer to update changes read on change feed to destination /// collection. Deregisters change feed observer and closes process when enter key is pressed /// </summary> /// <returns>A Task to allow asynchronous execution</returns> public async Task RunChangeFeedHostAsync() { // newRecord(8888); string hostName = Guid.NewGuid().ToString(); // monitored collection info DocumentCollectionInfo documentCollectionLocation = new DocumentCollectionInfo { Uri = new Uri(this.monitoredUri), MasterKey = this.monitoredSecretKey, DatabaseName = this.monitoredDbName, CollectionName = this.monitoredCollectionName }; // lease collection info DocumentCollectionInfo leaseCollectionLocation = new DocumentCollectionInfo { Uri = new Uri(this.leaseUri), MasterKey = this.leaseSecretKey, DatabaseName = this.leaseDbName, CollectionName = this.leaseCollectionName }; // destination collection info DocumentCollectionInfo destCollInfo = new DocumentCollectionInfo { Uri = new Uri(this.destUri), MasterKey = this.destSecretKey, DatabaseName = this.destDbName, CollectionName = this.destCollectionName }; // Customizable change feed option and host options ChangeFeedOptions feedOptions = new ChangeFeedOptions(); // ie customize StartFromBeginning so change feed reads from beginning // can customize MaxItemCount, PartitonKeyRangeId, RequestContinuation, SessionToken and StartFromBeginning feedOptions.StartFromBeginning = true; ChangeFeedHostOptions feedHostOptions = new ChangeFeedHostOptions(); // ie. customizing lease renewal interval to 15 seconds // can customize LeaseRenewInterval, LeaseAcquireInterval, LeaseExpirationInterval, FeedPollDelay feedHostOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15); feedHostOptions.LeaseRenewInterval = TimeSpan.FromMilliseconds(100); feedHostOptions.LeaseAcquireInterval = TimeSpan.FromMilliseconds(100); feedHostOptions.FeedPollDelay = TimeSpan.FromMilliseconds(100); using (DocumentClient destClient = new DocumentClient(destCollInfo.Uri, destCollInfo.MasterKey)) { DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory(destClient, destCollInfo); ChangeFeedEventHost host = new ChangeFeedEventHost(hostName, documentCollectionLocation, leaseCollectionLocation, feedOptions, feedHostOptions); await host.RegisterObserverFactoryAsync(docObserverFactory); Console.WriteLine("Running... Press enter to stop."); Console.ReadLine(); // do not unreg for now // await host.UnregisterObserversAsync(); } }