Created
June 29, 2012 05:40
-
-
Save jami-i/3016054 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import java.util.Date; | |
import java.util.concurrent.Callable; | |
import play.data.Form; | |
import play.data.validation.Constraints.Required; | |
import play.libs.Akka; | |
import play.libs.F.Function; | |
import play.libs.F.Promise; | |
import play.mvc.Controller; | |
import play.mvc.Result; | |
public class TimeoutController extends Controller { | |
static Form<Timeout> timeoutForm = form(Timeout.class); | |
public static Result index() { | |
System.out.println("timeout sequence start" + new Date()); | |
Timeout timeout = timeoutForm.bindFromRequest().get(); | |
Promise<Integer> promise = Akka.future(new CallableTimeout(timeout.t)); | |
return async( | |
promise.map( | |
new Function<Integer,Result>(){ | |
@Override | |
public Result apply(Integer arg0) throws Throwable { | |
System.out.println("timeout sequence end" + new Date()); | |
return ok("in"+ arg0 + "sec"); | |
} | |
} | |
) | |
); | |
} | |
public static class CallableTimeout implements Callable<Integer>{ | |
private int timeout = 0; | |
public CallableTimeout(int timeout) { | |
this.timeout = timeout; | |
} | |
@Override | |
public Integer call() throws Exception { | |
Thread.sleep(timeout); | |
return Integer.valueOf(timeout); | |
} | |
} | |
public static class Timeout { | |
@Required | |
public Integer t; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment