public async Task <Boolean> ReadFromQueueAndIndexAsync(AzureSearchManager searchMan)
        {
            if (searchMan == null)
            {
                throw new ArgumentNullException("searchMan");
            }

            try
            {
                var msg = _qClient.Receive();

                if (msg == null)
                {
                    return(false);
                }

                await searchMan.IndexDocumentAsync(msg.GetBody <string>());

                _qClient.Complete(msg.LockToken);

                return(true);
            }
            catch (Exception ex)
            {
                _qClient.Abort();

                throw ex;
            }
        }
Beispiel #2
0
        public void Receive()
        {
            _client.OnMessage(m =>
            {
                Console.WriteLine("Message body: " + m.GetBody <String>());
                Console.WriteLine("Message id: " + m.MessageId);

                _client.Complete(m.LockToken);

                Thread.Sleep(1000);
            }, new OnMessageOptions()
            {
                AutoComplete = false
            });
        }
Beispiel #3
0
        // Methods
        public void Acknowledge(bool ignoreError = true)
        {
            try
            {
                #region Acknowledging Message
                if (IsAcknowledgmentConfigured)
                {
                    queueClient.Complete(lockToken);
                }
                else
                {
                    throw MessageQueueCommonItems.PrepareAndLogQueueException(
                              errorCode: QueueErrorCode.AcknowledgmentIsNotConfiguredForQueue,
                              message: ErrorMessages.AcknowledgmentIsNotConfiguredForQueue,
                              innerException: null,
                              queueContext: CommonItems.ServiceBusName,
                              queueName: queueName,
                              logger: logger);
                }

                #endregion
            }
            catch (QueueException)
            {
                if (ignoreError == false)
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                var queueException = MessageQueueCommonItems.PrepareAndLogQueueException(
                    errorCode: QueueErrorCode.FailedToAcknowledgeMessage,
                    message: ErrorMessages.FailedToAcknowledgeMessage,
                    innerException: ex,
                    queueContext: CommonItems.ServiceBusName,
                    queueName: queueName,
                    logger: logger);

                if (ignoreError == false)
                {
                    throw queueException;
                }
            }
        }
 private void ReceiveSettings()
 {
     do
     {
         QueueClient     client = QueueClient.Create(ClientSettingsQueue);
         BrokeredMessage message;
         while ((message = client.Receive(TimeSpan.FromSeconds(5))) != null)
         {
             if (_stopEvent.WaitOne(TimeSpan.Zero))
             {
                 return;
             }
             var data = message.GetBody <ClientSetting>();
             client.Complete(message.LockToken);
             UpdateSettingsXml(data);
         }
         client.Close();
     }while (WaitHandle.WaitAny(new WaitHandle[] { _stopEvent }, 1000) != 0);
 }
        private void PdfDocListen(string saveLocation)
        {
            QueueClient client = QueueClient.Create(PdfDocQueue);

            do
            {
                BrokeredMessage message;
                while ((message = client.Receive(TimeSpan.FromSeconds(5))) != null)
                {
                    if (_stopEvent.WaitOne(TimeSpan.Zero))
                    {
                        return;
                    }
                    var data    = message.GetBody <PdfDocument>();
                    var docData = Path.Combine(saveLocation, data.Name);
                    File.WriteAllBytes(docData, data.PdfDocumentBlob);
                    client.Complete(message.LockToken);
                }
            }while (WaitHandle.WaitAny(new WaitHandle[] { _stopEvent }, 1000) != 0);
            client.Close();
        }
 static void Main(string[] args)
 {
     CreateQueue();
     queueClient = QueueClient.Create(queueName, ReceiveMode.PeekLock);
     Console.Out.WriteLine("Queue client receive mode is " + queueClient.Mode.ToString());
     try
     {
         while (true)
         {
             long msgCount = nsMgr.GetQueue(queueName).MessageCountDetails.ActiveMessageCount;
             Console.Out.WriteLine("Ready for messages... ");
             if (msgCount > 0) { Console.Out.WriteLine("The queue has " + msgCount + " messages waiting"); }
             var msg = queueClient.Receive(new TimeSpan(1, 0, 0));
             Console.Out.WriteLine("Received message with id {0}", msg.MessageId);
             Messages.Greeting greeting = msg.GetBody<Messages.Greeting>();
             Console.Out.WriteLine("Message for: {0}", greeting.Name );
             Console.Out.WriteLine("{0}\n", greeting.Message);
             if (queueClient.Mode == ReceiveMode.PeekLock)
             {
                 try
                 {
                     Console.Out.WriteLine("Completing message with lock token {0}", msg.LockToken);
                     queueClient.Complete(msg.LockToken);
                 }
                 catch (MessageLockLostException)
                 {
                     Console.Out.WriteLine("Oh noes! We lost the lock on the message. ");
                 }
             }
             Console.Out.WriteLine();
         }
     }
     finally
     {
         queueClient.Close();
     }
 }
 public void Complete(string id)
 {
     Interlocked.Increment(ref _completedCount);
     _queueClient.Complete(new Guid(id));
 }
Beispiel #8
0
 public void completeMessage()
 {
     queueClient.Complete(message.LockToken);
 }
        protected override async Task OnProcessWorkItem(BrokeredMessage message)
        {
            Utils.CheckAndLogDeliveryCount(message, taskHubDescription.MaxTaskActivityDeliveryCount, this.orchestratorQueueName);

            Task renewTask = null;
            var  renewCancellationTokenSource = new CancellationTokenSource();

            try
            {
                TaskMessage taskMessage = await Utils.GetObjectFromBrokeredMessageAsync <TaskMessage>(message);

                OrchestrationInstance orchestrationInstance = taskMessage.OrchestrationInstance;
                if (orchestrationInstance == null || string.IsNullOrWhiteSpace(orchestrationInstance.InstanceId))
                {
                    throw TraceHelper.TraceException(TraceEventType.Error,
                                                     new InvalidOperationException("Message does not contain any OrchestrationInstance information"));
                }
                if (taskMessage.Event.EventType != EventType.TaskScheduled)
                {
                    throw TraceHelper.TraceException(TraceEventType.Critical,
                                                     new NotSupportedException("Activity worker does not support event of type: " +
                                                                               taskMessage.Event.EventType));
                }

                // call and get return message
                var          scheduledEvent = (TaskScheduledEvent)taskMessage.Event;
                TaskActivity taskActivity   = objectManager.GetObject(scheduledEvent.Name, scheduledEvent.Version);
                if (taskActivity == null)
                {
                    throw new TypeMissingException("TaskActivity " + scheduledEvent.Name + " version " +
                                                   scheduledEvent.Version + " was not found");
                }

                renewTask = Task.Factory.StartNew(() => RenewUntil(message, renewCancellationTokenSource.Token));

                // TODO : pass workflow instance data
                var          context        = new TaskContext(taskMessage.OrchestrationInstance);
                HistoryEvent eventToRespond = null;

                try
                {
                    string output = await taskActivity.RunAsync(context, scheduledEvent.Input);

                    eventToRespond = new TaskCompletedEvent(-1, scheduledEvent.EventId, output);
                }
                catch (TaskFailureException e)
                {
                    TraceHelper.TraceExceptionInstance(TraceEventType.Error, taskMessage.OrchestrationInstance, e);
                    string details = IncludeDetails ? e.Details : null;
                    eventToRespond = new TaskFailedEvent(-1, scheduledEvent.EventId, e.Message, details);
                }
                catch (Exception e)
                {
                    TraceHelper.TraceExceptionInstance(TraceEventType.Error, taskMessage.OrchestrationInstance, e);
                    string details = IncludeDetails
                        ? string.Format("Unhandled exception while executing task: {0}\n\t{1}", e, e.StackTrace)
                        : null;
                    eventToRespond = new TaskFailedEvent(-1, scheduledEvent.EventId, e.Message, details);
                }

                var responseTaskMessage = new TaskMessage();
                responseTaskMessage.Event = eventToRespond;
                responseTaskMessage.OrchestrationInstance = orchestrationInstance;

                BrokeredMessage responseMessage = Utils.GetBrokeredMessageFromObject(responseTaskMessage,
                                                                                     settings.MessageCompressionSettings,
                                                                                     orchestrationInstance, "Response for " + message.MessageId);
                responseMessage.SessionId = orchestrationInstance.InstanceId;

                using (var ts = new TransactionScope())
                {
                    workerQueueClient.Complete(message.LockToken);
                    deciderSender.Send(responseMessage);
                    ts.Complete();
                }
            }
            finally
            {
                if (renewTask != null)
                {
                    renewCancellationTokenSource.Cancel();
                    renewTask.Wait();
                }
            }
        }