18 Aralık 2022 Pazar

AWS SQS API SqsClient Sınıfı

Giriş
Şu satırı dahil ederiz
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.CreateQueueRequest;
import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest;
import software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse;
builder metodu
Örnek
Elimizde şöyle bir kod olsun
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;

protected AwsCredentialsProvider getAwsCredentialsProvider() {
  return AwsCredentialsProviderChain.builder()
    .addCredentialsProvider(DefaultCredentialsProvider.create())
    .build();
}
Şöyle yaparız
protected SqsClient sqsClient() throws URISyntaxException {
  return SqsClient.builder()
    .region(Region.US_EAST_1)
    .endpointOverride(new URI("http://localhost:4566"))
    .credentialsProvider(getAwsCredentialsProvider())
    .build();
}
createQueue metodu
Örnek
Şöyle yaparız
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.CreateQueueRequest;

protected String createQueue(String queueName) throws URISyntaxException {
  var createQueueRequest = CreateQueueRequest.builder()
    .queueName(queueName)
    .build();
  return sqsClient().createQueue(createQueueRequest).queueUrl();
}
getQueueUrl metodu
Örnek
Şöyle yaparız
GetQueueUrlResponse getQueueUrlResponse = sqsClient.
  getQueueUrl(GetQueueUrlRequest.builder().queueName(QUEUE_NAME).build());
String originalUrl = getQueueUrlResponse.queueUrl();
receiveMessage metodu
Örnek
Şöyle yaparız
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest;
import software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse;

	
protected ReceiveMessageResponse receiveMessage(String queueUrl)
  throws URISyntaxException {
    var sqsClient = sqsClient();
    var receiveMessageRequest = ReceiveMessageRequest.builder()
      .queueUrl(queueUrl)
      .build();
  return sqsClient.receiveMessage(receiveMessageRequest);
}
sendMessageBatch metodu
Örnek
Şöyle yaparız. Burada mesajlar 10'luk gruplar halinde gönderiliyor
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest;
import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequestEntry;

List<SendMessageBatchRequestEntry> messageList = 

int sublistSize = 10;
String queueUrl = ...;

// Iterate the list with sublists of size 10
for (int i = 0; i < messageList.size(); i += sublistSize) {
  int endIndex = Math.min(i + sublistSize, messageList.size());
  List<SendMessageBatchRequestEntry> sublist = messageList.subList(i, endIndex);

  // Process the current sublist
  SendMessageBatchRequest sendMessageBatchRequest = SendMessageBatchRequest.builder()
    .queueUrl(queueUrl)
    .entries(sublist)
    .build();
    sqsClient.sendMessageBatch(sendMessageBatchRequest);
}



Hiç yorum yok:

Yorum Gönder