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的活动.您必须轮询新项目.

点赞