Search code examples
c#azureazure-service-fabric

Use ReliableQueue in ServiceFabric without polling?


I've been looking at stateful services within Service Fabric. I've been digging through the examples, specifically the WordCount. They have a RunAsync method that looks like this inside of the 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());
            }
        }
    }

Essentially, in this example, the service is polling the ReliableQueue every 100ms for messages. Is there a way to do this without the poll? Can we subscribe to an event or something that gets triggered when a message is successfully added to the ReliableQueue?


Solution

  • No, currently there are no events you can use for ReliableQueue. You have to poll for new items.