private async Task <IEnumerable <QueueMessage> > ReceiveMessagesAsync(int count)
        {
            IReliableQueue <byte[]> collection = await _stateManager.GetOrAddAsync <IReliableQueue <byte[]> >(_queueName);

            var result = new List <QueueMessage>();

            using (var tx = new ServiceFabricTransaction(_stateManager, null))
            {
                while (result.Count < count)
                {
                    ConditionalValue <byte[]> message = await collection.TryDequeueAsync(tx.Tx);

                    if (message.HasValue)
                    {
                        QueueMessage qm = QueueMessage.FromByteArray(message.Value);

                        result.Add(qm);
                    }
                    else
                    {
                        break;
                    }
                }

                await tx.CommitAsync();
            }

            return(result.Count == 0 ? null : result);
        }
예제 #2
0
        public void Binary_NullId_Handled()
        {
            QueueMessage qm1 = QueueMessage.FromText("content2");
            QueueMessage qm2 = QueueMessage.FromByteArray(qm1.ToByteArray());

            Assert.Null(qm2.Id);
            Assert.Equal("content2", qm2.StringContent);
        }
예제 #3
0
        private static QueueMessage ToQueueMessage(FileInfo fi)
        {
            byte[] content = File.ReadAllBytes(fi.FullName);

            var result = QueueMessage.FromByteArray(content);

            result.Id = Path.GetFileNameWithoutExtension(fi.Name);
            return(result);
        }
예제 #4
0
      public void Binary_Serialize_Deserializes()
      {
         var qm = new QueueMessage("id", "content");
         qm.DequeueCount = 4;
         qm.Properties.Add("key", "value");

         byte[] data = qm.ToByteArray();

         QueueMessage qm2 = QueueMessage.FromByteArray(data);

         Assert.Equal("id", qm2.Id);
         Assert.Equal("content", qm2.StringContent);
         Assert.Equal(4, qm2.DequeueCount);
         Assert.Single(qm2.Properties);
         Assert.Equal("value", qm2.Properties["key"]);
      }
예제 #5
0
        private async Task ReceiveMessagesAsync(Func <IEnumerable <QueueMessage>, Task> onMessage, int maxBatchSize, CancellationToken cancellationToken)
        {
            var messages = new List <QueueMessage>();

            while (!cancellationToken.IsCancellationRequested && !_disposed)
            {
                try
                {
                    using (var tx = new ServiceFabricTransaction(_stateManager, null))
                    {
                        IReliableQueue <byte[]> collection = await GetCollectionAsync();

                        while (messages.Count < maxBatchSize)
                        {
                            ConditionalValue <byte[]> message = await collection.TryDequeueAsync(tx.Tx, TimeSpan.FromSeconds(4), cancellationToken);

                            if (message.HasValue)
                            {
                                QueueMessage qm = QueueMessage.FromByteArray(message.Value);

                                messages.Add(qm);
                            }
                            else
                            {
                                break;
                            }
                        }

                        //make the call before committing the transaction
                        if (messages.Count > 0)
                        {
                            await onMessage(messages);

                            messages.Clear();
                        }

                        await tx.CommitAsync();
                    }
                }
                catch (Exception ex)
                {
                    Trace.Fail($"failed to listen to messages on queue '{_queueName}'", ex.ToString());
                }

                await Task.Delay(_scanInterval);
            }
        }
        private async Task ReceiveMessagesAsync(Func <IReadOnlyCollection <QueueMessage>, CancellationToken, Task> onMessage, int maxBatchSize, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested && !_disposed)
            {
                try
                {
                    using (var tx = new ServiceFabricTransaction(_stateManager, null))
                    {
                        IReliableState collection = await GetCollectionAsync().ConfigureAwait(false);

                        var messages = new List <QueueMessage>();

                        while (messages.Count < maxBatchSize)
                        {
                            ConditionalValue <byte[]> message = await TryDequeueAsync(tx, collection, cancellationToken).ConfigureAwait(false);

                            if (message.HasValue)
                            {
                                QueueMessage qm = QueueMessage.FromByteArray(message.Value);

                                messages.Add(qm);
                            }
                            else
                            {
                                break;
                            }
                        }

                        //make the call before committing the transaction
                        if (messages.Count > 0)
                        {
                            await onMessage(messages, cancellationToken).ConfigureAwait(false);
                        }

                        await tx.CommitAsync().ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    Trace.Fail($"failed to listen to messages on queue '{_queueName}'", ex.ToString());
                }

                await Task.Delay(_scanInterval).ConfigureAwait(false);
            }

            Trace.TraceInformation("queue '{0}' scanner exited", _queueName);
        }