Closed dthauvin closed 8 years ago
map function takes a future and returns another Future. You will need to wait on the Future. A better way to write your code is and I have tested this is in a worksheet
import com.amazonaws.services.sqs.model.{ListQueuesRequest, ListQueuesResult, SendMessageRequest}
import com.amazonaws.services.sqs.scala.AmazonSQSClient
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
val sqsClient = new AmazonSQSClient("us-east-1")
val queues = sqsClient.listQueues(
new ListQueuesRequest()).map(
(r: ListQueuesResult) => Some(r.getQueueUrls))
Await.result(queues, scala.concurrent.duration.Duration.Inf);
Specifically re "does not close the program": the underlying AmazonSQSAsyncClient
from the Java SDK creates a thread pool using non-daemon threads to process callbacks.
On the plus side, this means you do not need to explicitly wait for all of the futures to complete before returning from your main method -- if they were daemon threads the program would exit as soon as the main method returned, quite possibly before you heard back from the service.
On the minus side, this means you need to explicitly tell the client when you're done with it via its shutdown
method, or the threads in this thread pool will continue to run forever, waiting for another callback to process. :(
Thanks for the explanation @fernomac . I did not see that question from the customer.
Hello Guys thank for your help both.
I tried to find a solution based on your advise but nothing works for me.
The first solution does not close the thread, even after 5 minute wait.
For the second advice I can not find a method to shutdown the objects I create .do I implement a method sys.addShutdownHook
?
Here is a snippets could you help me please.
package SQS
import java.{lang, util}
import com.amazonaws.regions.{Region, Regions}
import com.amazonaws.services.sqs.model._
import com.amazonaws.services.sqs.scala._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
object getSQS {
def main(args: Array[String]): Unit = {
val sqsClient = new AmazonSQSClient("eu-west-1")
val queues1 = sqsClient.listQueues(new ListQueuesRequest()).map((r: ListQueuesResult) => println(r.getQueueUrls))
Await.result(queues1, scala.concurrent.duration.Duration.Inf)
}
}
Hello Guys,
I have the same problem... I don't find any "shutdown" method, except "addshutdownhook" which is not the feature I'm looking for.
Actually, both "sendmessage" and "receivemessage" methods don't return anything, even with await.result(...)
Is there any way to exit the map function after sending/receiving messages from SQS?
You want this shutdown method (sqsClient.shutdown()
in @damdr's example). You can call it in the body of the map
function, in an andThen
chained onto it, or after Await.result(...)
finishes blocking the main thread; all three should be equivalent.
Great!!! thank you so much!
Thank you @fernomac
Hi Is it normal that the simple instructions below does not close the program? Can you tell me the correct way to use this methods. Thank you a lot .
val sqsClient = new AmazonSQSClient("eu-west-1") sqsClient.sendMessage(new SendMessageRequest("https://sqs.eu-west-1.amazonaws.com/xxxxxx/queue","TEST Message"))
val queues1 = sqsClient.listQueues(new ListQueuesRequest()).map((r: ListQueuesResult) => println(r.getQueueUrls))
val test = sqsClient.receiveMessage(new ReceiveMessageRequest("https://sqs.eu-west-1.amazonaws.com/xxxxxxx/queue")).map((r: ReceiveMessageResult) => println(r.getMessages))