c# – 在ServiceFabric中使用ReliableQueue而不进行轮询?
发布时间:2020-12-15 22:30:52 所属栏目:百科 来源:网络整理
导读:我一直在关注Service Fabric中的有状态服务.我一直在挖掘这些例子,特别是WordCount.他们有一个RunAsync方法,在WordCountService中看起来像这样: protected override async Task RunAsync(CancellationToken cancellationToken) { IReliableQueuestring inpu
我一直在关注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的活动.您必须轮询新项目.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |