[2.0 - Scala] Multiple async calls

207 views
Skip to first unread message

Meeraj Kunnumpurath

unread,
Apr 25, 2012, 3:08:33 PM4/25/12
to play-fr...@googlegroups.com
Hi,

What is the best way to use async HTTP in a controller that makes multiple outbound WS calls such that the response is returned after all the calls complete?

Kind regards
Meeraj

Pascal Voitot Dev

unread,
Apr 25, 2012, 4:06:17 PM4/25/12
to play-fr...@googlegroups.com
You can use a wonderful feature from Promise:
Promise.sequence(List[Promise[A]]): Promise[List[A]]

You can provide it with a List (or any sequence) of promises and it returns a Promise of List meaning it redeems all Promises before redeemining the Promise[List].
So you can launch several WS calls in parallel and it will await for responses in an asynchronous and non-blocking way...

A Promise can embed other promises etc... So you can build a tree of promises etc...

interesting isn't it?

br
Pascal

Meeraj

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Nourhan El Malahy

unread,
Apr 25, 2012, 4:08:52 PM4/25/12
to play-fr...@googlegroups.com
Guys, please how can I pop-up a message when the user clicks on a certain button?? 

Pascal Voitot Dev

unread,
Apr 25, 2012, 4:37:44 PM4/25/12
to play-fr...@googlegroups.com
please don't pollute another thread...
open a new thread if you have a specific question

thanks
Pascal

Nourhan El Malahy

unread,
Apr 25, 2012, 4:46:11 PM4/25/12
to play-fr...@googlegroups.com
I'm Sorry, I didn't notice! I wrote it here by mistake!

Meeraj Kunnumpurath

unread,
Apr 25, 2012, 5:41:38 PM4/25/12
to play-fr...@googlegroups.com
Thanks Pascal, I will have a look at that. Yes, I did see a passing reference to this in the Play docco.

Regards
Meeraj

Meeraj Kunnumpurath

unread,
Apr 26, 2012, 5:42:11 PM4/26/12
to play-fr...@googlegroups.com
Hi Pascal,

Thanks for your suggestion, it works like a dream. I have the code below, if it is of any help to anyone else.

def makeCall() = Action { implicit request =>

    def scheduleCalls(callIds : Seq[String], request : Request[AnyRef]) : List[Promise[Response]] = {
        if (callIds.isEmpty) {
            return List()
        } else {
            val promise = WS.url("https://callUrl" + callIds.head).post("")
            promise :: scheduleCalls(callIds.tail, request)
        }
    }

    Async {
        val callIds = request.body.asFormUrlEncoded.get("callId")
        val promises = Promise.sequence(scheduleCalls(callIds, request))
        promises.map { response =>
            Ok("Ok")
        }
    }

}

Regards
Meeraj

On Wed, Apr 25, 2012 at 9:06 PM, Pascal Voitot Dev <pascal.v...@gmail.com> wrote:

Pascal Voitot Dev

unread,
Apr 26, 2012, 5:51:18 PM4/26/12
to play-fr...@googlegroups.com
One of the great features of Play2 ;)

You're welcome ;)
Reply all
Reply to author
Forward
0 new messages