Untitled
unknown
scala
a year ago
2.3 kB
3
Indexable
import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest import software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse import software.amazon.awssdk.services.sqs.model.Message import software.amazon.awssdk.services.sqs.SqsAsyncClient // Create SqsAsyncClient instance val sqsClient: SqsAsyncClient = SqsAsyncClient.builder().build() // Create ReceiveMessageRequest val request = ReceiveMessageRequest.builder() .queueUrl(queueUrl) .maxNumberOfMessages(settings.maxBatchSize) .waitTimeSeconds(settings.waitTimeSeconds) .build() // Asynchronously receive messages sqsClient.receiveMessage(request).whenComplete { (result: ReceiveMessageResponse, exception: Throwable) => if (exception != null) { // Handle error failureCallback.invoke(exception) } else { // Process received messages val messages: java.util.List[Message] = result.messages() // Invoke success callback for each message or batch processing as needed messages.forEach { message => successCallback.invoke(message) } } } // second import scala.concurrent.{Future, ExecutionContext} import software.amazon.awssdk.services.sqs.model.{ReceiveMessageRequest, ReceiveMessageResponse, Message} import software.amazon.awssdk.services.sqs.SqsAsyncClient // Create SqsAsyncClient instance val sqsClient: SqsAsyncClient = SqsAsyncClient.builder().build() // Create ReceiveMessageRequest val request = ReceiveMessageRequest.builder() .queueUrl(queueUrl) .maxNumberOfMessages(settings.maxBatchSize) .waitTimeSeconds(settings.waitTimeSeconds) .build() // Implicit ExecutionContext for running Futures implicit val ec: ExecutionContext = ExecutionContext.global // Asynchronously receive messages and handle success and failure val receiveFuture: Future[Unit] = Future { sqsClient.receiveMessage(request).handle[Unit] { case Right(result) => // Process received messages val messages: java.util.List[Message] = result.messages() // Invoke success callback for each message or batch processing as needed messages.forEach { message => successCallback.invoke(message) } case Left(exception) => // Handle error failureCallback.invoke(exception) } } // If you need to wait for the future to complete, you can use Await.result