Right now you're doing this:
- Client makes a request and waits for the response
- You start a long-running computation
- You wait until the computation is finished
- You return the result of your computation to the client with
ok
result (only when it's finished)
Maybe you should just create a new Promise
with your computation and without waiting for it to finish, immediately return ok
to a client?
This way you can do whatever you want with the result of the computation (write it to db etc.) and then your client can either check by himself the result of the operation or you can propagate the result to him using something like websocket
or server-sent event
.
Taking your example, you simply doesn't wait on the first promise and just return :
public static Result index(String name, String surname) {
// will be processed asynchronously
Promise<Integer> promiseOfInt = Promise.promise(new F.Function0<Integer>() {
public Integer apply() throws InterruptedException {
return intensiveComputation();
}
});
promiseOfInt.map(new Function<Integer, SomeReturnType>() {
public SomeReturnType apply(Integer i) {
// do smth like writing to db...
return new SomeReturnType("done!");
}
});
// returns immediately
return ok("Got result: " + i);
}