27 Aralık 2022 Salı

SpringCloud AWS SQS @SqsListener Anotasyonu

Giriş
Şu satırı dahil ederiz
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;
Açıklaması şöyle
As we will use Spring to build the microservice we could use Spring Cloud AWS. It is very easy to build an SQS listener using this library. With little to no configurations, one can create the listener method just by annotation it with @SqsListener annotation.

But this library is limited. The maximum amount of messages that can be fetched at once cant be more than 10. Although it allows parallel processing, receiving up to 10 messages doesn't allow saving messages at once to the database.

Because it receives messages, creates multiple parallel threads, and each thread calls the listener method passing one message as an argument.

deletionPolicy Alanı
Şöyle yaparız
import com.aws.sqs.model.Message; import org.springframework.cloud.aws.messaging.listener.SqsMessageDeletionPolicy;
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;

@RestController
@RequestMapping(value = "/sqs")
public class SqsController {
  //queue name
  private static final String QUEUE = "my-queue";
...
  // @SqsListener listens to the message from the queue.
  @SqsListener(value = QUEUE, deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
  public void getMessageFromSqs(Message message, @Header("MessageId") String messageId) {
    logger.info("Received message= {} with messageId= {}", message.toString(), messageId);
  }
}

Hiç yorum yok:

Yorum Gönder