简体   繁体   English

如何将 changefeeds Monitored Collection 名称放入我的 ChangesHander - Containers.ChangesHander<t> (Azure 宇宙 v3)</t>

[英]How can I get the changefeeds Monitored Collection name into my ChangesHander - Containers.ChangesHander<T> (Azure Cosmos v3)

I have a service that spawns off a number of Changefeeds to monitor a number of different Cosmos DB collections.我有一项服务,它产生了许多 Changefeed 来监控许多不同的 Cosmos DB collections。 In v1 or 2,the ChangefeedObserver class included the ChangefeedObserverContext from which I could extract the collection name from.在 v1 或 2 中,ChangefeedObserver class 包含 ChangefeedObserverContext,我可以从中提取集合名称。

  public Task ProcessChangesAsync(IChangeFeedObserverContext context, IReadOnlyList<Document> deltas, CancellationToken cancellationToken)
    {
        string observerCollection = string.Empty;
        try
        {
            Regex rx = new Regex(@"\b(\w+$)");
            observerCollection = rx.Match(context.FeedResponse.ContentLocation).Value.ToString();

In v3, instead of passing a type as you processor, you pass it a delegate method whose signature no longer contains the context在 v3 中,不是在处理器中传递类型,而是传递一个委托方法,其签名不再包含上下文

MS Docs Container.ChangesHandlerDelegate MS Docs Container.ChangesHandlerDelegate

Change Feed Processor 更改 Feed 处理器

    private ChangeFeedProcessor ChangeFeedInitialize(Container leasingContainer, Container monitoringContainer, string hostName)
    {
        ChangeFeedProcessor changeFeedProcessor = monitoringContainer
            .GetChangeFeedProcessorBuilder<Document>(hostName, this.HandleChangesAsync)
                .WithInstanceName("isn")
                .WithLeaseContainer(leasingContainer)
                .Build();

        return changeFeedProcessor;
    }

    private async Task HandleChangesAsync(IReadOnlyCollection<Document> changes, CancellationToken cancellationToken)
    {
        ILogger logger = AnalyticsHelper.BuildMeMyLogger(this.loggerFactory);

        try
        {
            AnalyticsChangefeedProcessor changefeedProcessor = new AnalyticsChangefeedProcessor();
            await changefeedProcessor.HandleChangesAsync(changes, this.analyticsContext.DataLakeStorageProvider, "CollectionName", logger);
        }
        catch (Exception ex)
        {
            logger.LogFailure($"Failed to process changes: {ex.Message}", TagIds.ExceptionAnalytics, ex);
        }
    }

In the above coded, I have a basic method that creates the Changefeed (gets started via a timer), and the the delegate method that sends the processing off to a larger class to take actions, depending on the monitored collection.在上面的代码中,我有一个创建 Changefeed(通过计时器启动)的基本方法,以及将处理发送到更大的 class 以采取行动的委托方法,具体取决于监控的集合。

So, how can I get this changefeeds Monitored Collection value into the ChangesHander?那么,如何将这个changefeeds Monitored Collection 值放入 ChangesHander 中?

You already have the reference, you can inject it or reference it.你已经有了引用,你可以注入它或者引用它。

private ChangeFeedProcessor ChangeFeedInitialize(Container leasingContainer, Container monitoringContainer, string hostName)
{
    ChangeFeedProcessor changeFeedProcessor = monitoringContainer
        .GetChangeFeedProcessorBuilder<Document>(hostName, 
                (IReadOnlyCollection<Document> changes, CancellationToken cancellationToken) => 
                    this.HandleChangesAsync(monitoringContainer, changes, cancellationToken))
            .WithInstanceName("isn")
            .WithLeaseContainer(leasingContainer)
            .Build();

    return changeFeedProcessor;
}

private async Task HandleChangesAsync(Container monitoringContainer, IReadOnlyCollection<Document> changes, CancellationToken cancellationToken)
{
    ILogger logger = AnalyticsHelper.BuildMeMyLogger(this.loggerFactory);

    try
    {
        AnalyticsChangefeedProcessor changefeedProcessor = new AnalyticsChangefeedProcessor();
        await changefeedProcessor.HandleChangesAsync(changes, this.analyticsContext.DataLakeStorageProvider, "CollectionName", logger);
    }
    catch (Exception ex)
    {
        logger.LogFailure($"Failed to process changes: {ex.Message}", TagIds.ExceptionAnalytics, ex);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM