示例#1
0
        private async Task Process()
        {
            try
            {
                while (!TokenSource.Token.IsCancellationRequested)
                {
                    var messages = await SqsClientHandler.GetSqsMessages(SqsQueue.Url, SqsQueue.RedrivePolicy.MaxReceiveCount, SqsQueue.ReceiveMessageWaitTimeSeconds, TokenSource.Token).ConfigureAwait(false);

                    foreach (var message in messages)
                    {
                        var messageType = message.MessageAttributes.GetMessageTypeAttributeValue();

                        if (messageType == null)
                        {
                            throw new ArgumentNullException($"No MessageType attribute present in message {JsonSerializer.Serialize(message)}");
                        }

                        if (messageType.Equals(typeof(T).Name))
                        {
                            var processed = await ProcessMessage(message).ConfigureAwait(false);

                            if (processed)
                            {
                                _ = await SqsClientHandler.DeleteMessage(SqsQueue.Url, message.ReceiptHandle, TokenSource.Token).ConfigureAwait(false);
                            }
                        }
                    }
                }
            }
            catch (OperationCanceledException ex)
            {
                Devon4NetLogger.Information("The message operation process has been cancelled");
                Devon4NetLogger.Information(ex);
            }
            catch (Exception ex)
            {
                Devon4NetLogger.Error(ex);
                throw;
            }
        }
示例#2
0
 protected SqsMessageConsumerHandler(IServiceCollection services, SqsQueue sqsQueue, AWSCredentials awsCredentials = null, RegionEndpoint regionEndpoint = null)
 {
     SqsClientHandler = new SqsClientHandler(awsCredentials, regionEndpoint);
     Services         = services;
     SqsQueue         = sqsQueue;
 }