.NET용 AWS 메시지 처리 프레임워크를 사용하여 메시지 사용 - AWS SDK for .NET

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

.NET용 AWS 메시지 처리 프레임워크를 사용하여 메시지 사용

이 시험판 설명서는 프리뷰 릴리즈의 기능에 관한 것입니다. 내용은 변경될 수 있습니다.

.NET용 AWS 메시지 처리 프레임워크를 사용하면 프레임워크 또는 메시징 서비스 중 하나를 사용하여 게시된 메시지를 사용할 수 있습니다. 메시지는 다양한 방법으로 소비될 수 있으며, 그 중 일부는 아래에 설명되어 있습니다.

메시지 핸들러

메시지를 사용하려면 처리하려는 각 메시지 유형의 IMessageHandler 인터페이스를 사용하여 메시지 처리기를 구현하십시오. 메시지 유형과 메시지 처리기 간의 매핑은 프로젝트 시작 시 구성됩니다.

await Host.CreateDefaultBuilder(args) .ConfigureServices(services => { // Register the AWS Message Processing Framework for .NET services.AddAWSMessageBus(builder => { // Register an SQS Queue that the framework will poll for messages. // NOTE: The URL given below is an example. Use the appropriate URL for your SQS Queue. builder.AddSQSPoller("https://sqs.us-west-2.amazonaws.com/012345678910/MyAppProd"); // Register all IMessageHandler implementations with the message type they should process. // Here messages that match our ChatMessage .NET type will be handled by our ChatMessageHandler builder.AddMessageHandler<ChatMessageHandler, ChatMessage>(); }); }) .Build() .RunAsync();

다음 코드는 메시지의 샘플 메시지 핸들러를 보여줍니다. ChatMessage

public class ChatMessageHandler : IMessageHandler<ChatMessage> { public Task<MessageProcessStatus> HandleAsync(MessageEnvelope<ChatMessage> messageEnvelope, CancellationToken token = default) { // Add business and validation logic here. if (messageEnvelope == null) { return Task.FromResult(MessageProcessStatus.Failed()); } if (messageEnvelope.Message == null) { return Task.FromResult(MessageProcessStatus.Failed()); } ChatMessage message = messageEnvelope.Message; Console.WriteLine($"Message Description: {message.MessageDescription}"); // Return success so the framework will delete the message from the queue. return Task.FromResult(MessageProcessStatus.Success()); } }

MessageEnvelope외부에는 프레임워크에서 사용하는 메타데이터가 들어 있습니다. message속성은 메시지 유형 (이 경우ChatMessage) 입니다.

MessageProcessStatus.Success()돌아가서 메시지가 성공적으로 처리되었음을 나타내면 프레임워크가 Amazon SQS 대기열에서 메시지를 삭제할 것입니다. 메시지가 MessageProcessStatus.Failed() 반환되면 대기열에 남아 다시 처리하거나 데드레터 대기열 (구성된 경우) 로 이동할 수 있습니다.

장기 실행 프로세스에서의 메시지 처리

SQS 대기열 AddSQSPoller URL로 호출하여 대기열을 지속적으로 폴링하고 메시지를 BackgroundService처리하는 장기 실행을 시작할 수 있습니다.

await Host.CreateDefaultBuilder(args) .ConfigureServices(services => { // Register the AWS Message Processing Framework for .NET services.AddAWSMessageBus(builder => { // Register an SQS Queue that the framework will poll for messages. // NOTE: The URL given below is an example. Use the appropriate URL for your SQS Queue. builder.AddSQSPoller("https://sqs.us-west-2.amazonaws.com/012345678910/MyAppProd", options => { // The maximum number of messages from this queue that the framework will process concurrently on this client. options.MaxNumberOfConcurrentMessages = 10; // The duration each call to SQS will wait for new messages. options.WaitTimeSeconds = 20; }); // Register all IMessageHandler implementations with the message type they should process. builder.AddMessageHandler<ChatMessageHandler, ChatMessage>(); }); }) .Build() .RunAsync();

SQS 메시지 폴러 구성

SQS 메시지 폴러는 호출 시 에서 구성할 수 있습니다. SQSMessagePollerOptions AddSQSPoller

  • MaxNumberOfConcurrentMessages- 대기열에서 동시에 처리할 수 있는 최대 메시지 수입니다. 기본값은 10입니다.

  • WaitTimeSeconds- ReceiveMessage SQS 호출이 메시지가 대기열에 도착할 때까지 대기한 후 반환하는 시간 (초). 메시지를 사용할 수 있는 경우 통화는 보다 빨리 반환됩니다. WaitTimeSeconds 기본값은 20입니다.

메시지 가시성 타임아웃 처리

SQS 메시지에는 가시성 타임아웃 기간이 있습니다. 한 소비자가 특정 메시지를 처리하기 시작하면 해당 메시지는 대기열에 남아 있지만 두 번 이상 처리하지 않도록 다른 소비자에게 숨겨집니다. 메시지가 처리되지 않고 삭제되어 다시 표시되면 다른 소비자가 같은 메시지를 처리하려고 할 수 있습니다.

프레임워크는 현재 처리 중인 메시지의 가시성 제한 시간을 추적하여 연장하려고 시도합니다. 호출 SQSMessagePollerOptions AddSQSPoller 시 에서 이 동작을 구성할 수 있습니다.

  • VisibilityTimeout- 메시지를 받은 기간 (초) 은 후속 검색 요청에서 숨겨집니다. 기본값은 30입니다.

  • VisibilityTimeoutExtensionThreshold- 메시지의 가시성 제한 시간이 만료 후 몇 초 이내에 도달하면 프레임워크는 가시성 제한 시간을 1초 더 VisibilityTimeout 연장합니다. 기본값은 5입니다.

  • VisibilityTimeoutExtensionHeartbeatInterval- 프레임워크가 만료 후 몇 초 이내에 있는 메시지를 확인한 다음 가시성 제한 시간을 연장하는 빈도 (VisibilityTimeoutExtensionThreshold초) 기본값은 1입니다.

다음 예시에서는 프레임워크가 1초마다 아직 처리 중인 메시지가 있는지 확인합니다. 다시 표시되고 5초 이내에 메시지가 표시되면 프레임워크는 자동으로 각 메시지의 가시성 제한 시간을 30초 더 연장합니다.

// NOTE: The URL given below is an example. Use the appropriate URL for your SQS Queue. builder.AddSQSPoller("https://sqs.us-west-2.amazonaws.com/012345678910/MyAppProd", options => { options.VisibilityTimeout = 30; options.VisibilityTimeoutExtensionThreshold = 5; VisibilityTimeoutExtensionHeartbeatInterval = 1; });

함수 내 AWS Lambda 메시지 처리

SQS와 Lambda의 통합으로.NET용 AWS 메시지 처리 프레임워크를 사용할 수 있습니다. 이는 패키지에서 제공됩니다. AWS.Messaging.Lambda 시작하려면 해당 README를 참조하십시오.