/// <summary> /// this is the callback from the queue /// </summary> /// <param name="queueData">the data item returned from the queue</param> /// <param name="recieptHandle">the receipt handle of the data item</param> /// <param name="sqsQueue">the queue that captured this message</param> /// <returns>true to indicate we processed it, false to indicate it should remain in the queue</returns> private bool QueueCallBack(SQSDemoQueueData queueData, string recieptHandle, SqsQueueDispatcher <SQSDemoQueueData> sqsQueue) { bool returnValue = false; try { //to keep this real, we will randomize the delay periods, the effect is visible //when MaxConcurrency is more than 1 int delayMod = _RandomDelay.Next(_QueueDelayInSeconds, _QueueDelayInSeconds * 5); _SysLogger.LogInformation($"Working on message for {delayMod} seconds data: {queueData.RandomData}:{queueData.MoreRandomData}\r\n{recieptHandle}"); //Normally you would do work here, for our demo we just sleep a bit Task waitForIt = Task.Delay(TimeSpan.FromSeconds(delayMod), _CancellationTokenSource.Token); waitForIt.Wait(); if (!_CancellationTokenSource.IsCancellationRequested) { returnValue = true; } } catch (OperationCanceledException) { _SysLogger.LogInformation($"Was cancelled for message {recieptHandle}"); returnValue = false; } catch (Exception badThing) { _SysLogger.LogError(badThing, $"A critical error occurred during the queue callback for message {recieptHandle}"); //the SqsQueueDispatcher handles this so throw it again throw; } return(returnValue); }
/// <summary> /// simple method to enqueue items to the demo queue /// </summary> /// <param name="numItems"></param> static void EnqueueMessages(int numItems) { var config = GetConfiguration(); string queueName = config.GetValue <string>("appSettings:QueueName"); Console.WriteLine($"Queueing {numItems} messages to {queueName}"); SqsQueueDispatcher <SQSDemoQueueData> sqsQueue = new SqsQueueDispatcher <SQSDemoQueueData>(queueName); for (int count = 0; count < numItems; count++) { SQSDemoQueueData queueData = SQSDemoQueueData.RandomQueueData(); sqsQueue.EnqueueMessage(queueData); } Console.WriteLine($"Completed queueing {numItems} messages to {queueName}"); }