微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 在ServiceFabric中使用ReliableQueue而不进行轮询?

我一直在关注Service Fabric中的有状态服务.我一直在挖掘这些例子,特别是WordCount.他们有一个RunAsync方法,在WordCountService中看起来像这样:

protected override async Task RunAsync(CancellationToken cancellationToken)
    {
        IReliableQueue<string> inputQueue = await this.StateManager.GetorAddAsync<IReliableQueue<string>>("inputQueue");

        while (true)
        {
            cancellationToken.ThrowIfCancellationRequested();

            try
            {
                using (ITransaction tx = this.StateManager.CreateTransaction())
                {
                    ConditionalValue<string> dequeuReply = await inputQueue.TryDequeueAsync(tx);

                    if (dequeuReply.HasValue)
                    {
                       //... {more example code here }
                    }
                await Task.Delay(TimeSpan.FromMilliseconds(100),cancellationToken);
            }
            catch (TimeoutException)
            {
                //Service Fabric uses timeouts on collection operations to prevent deadlocks.
                //If this exception is thrown,it means that this transaction was waiting the default
                //amount of time (4 seconds) but was unable to acquire the lock. In this case we simply
                //retry after a random backoff interval. You can also control the timeout via a parameter
                //on the collection operation.
                Thread.Sleep(TimeSpan.FromSeconds(new Random().Next(100,300)));

                continue;
            }
            catch (Exception exception)
            {
                //For sample code only: simply trace the exception.
                ServiceEventSource.Current.MessageEvent(exception.ToString());
            }
        }
    }

本质上,在此示例中,服务每隔100毫秒轮询一次ReliableQueue消息.没有民意调查,有没有办法做到这一点?当消息成功添加到ReliableQueue时,我们可以订阅事件或触发的事件吗?

解决方法

不,目前没有可用于ReliableQueue的活动.您必须轮询新项目.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐