Karthik
Karthik

Reputation: 391

Launching Delayed HTTP Response with Play

I have a .net class which makes a HTTP Request to a controller in play but my controller has to make the request wait and after some time i have to make response to the request . I tried using Promise but can't figure out how to achieve this need help to solve this...

Thanks in advance

Upvotes: 0

Views: 543

Answers (2)

Buck Armani
Buck Armani

Reputation: 26

Awaiting is generally not the best idea. What you probably want to do in this instance is create an Akka actor that gets ping'd by the Scheduler every so often. When it receives that ping message it would check if the condition you're looking for exists and would notify all the actor-refs that registered interest in that event.

When you send a message to an akka actor via the ask pattern, the sender for that message is an actor backing the Future which is fulfilled once that backing actor is sent a message.

Play 2.0 has a simple way of turning an Akka Future into a Play Promise. And there you have it, asynchronous programming with Akka and Play!

So in summary, your Actor needs to react to two messages: RegisterListener Ping

RegisterListener is the message you send using the ask pattern, The actor will need to retain a reference to the sender of that listener so that it can be notified when the condition is fulfilled.

Upvotes: 1

Codemwnci
Codemwnci

Reputation: 54884

Assuming that you want to do this in Play 1.2.x, you would use the await function. For example, to receive a request, wait for 30 seconds, and then respond, you would do

public static void delay30() {
    await("30s");
    renderText("a-response");
}

You could use any of the render methods, renderText is just an example.

Upvotes: 1

Related Questions