1

I'm using Play2 (with Java) to write a webservice which runs a fairly computationally expensive operation. As per the documentation, I've broken up the process into a chain of promises resembling:

// Do some IO stuff on a background thread
Promise<ExtractionResult> extractDataPromise = Akka.future(new Callable<ExtractionResult>(){
    @Override
    public ExtractionResult call() throws Exception {

        return null;
    }
});

// Perform a couple computations based on the result
Promise<MyResult> longRunningComputation1 = extractDataPromise.map(
    new Function<ExtractionResult, MyResult>(){
        public MyResult apply(ExtractionResult er){
            return longRunningComputation();
        }
    }
)

Promise<MyResult> longRunningComputation2 = extractDataPromise.map(
    new Function<ExtractionResult, Result>(){
        public Result apply(ExtractionResult er){
            return longRunningComputation();
        }
    }
)

// Synchronize back up to make the result
Promise.sequence(longRunningComputation1, longRunningComputation2).flatMap(
    // Do something with the results
)

From what I've read in the documentation, you cannot inturrupt an actor in the middle of its execution, that is, I could not cancel the chain above in the middle of performning the IO or in the middle of the longRunningComputation. Would it be possible to cancel the entire operation between the chained calls? That is, if the user made a cancel request, could I set a flag that the operations could check to essentially become no-ops and return a null result? Is there another recommended way to cancel long-running service requests?

I'm fairly new to both Play and the underlying Akka framework, so I'm open to suggestions. Thanks

Syllepsis
  • 449
  • 4
  • 13
  • 1
    You can try to implement some sort of cancellable Future and return it from the actor. Something like http://stackoverflow.com/questions/16020964/cancellation-with-future-and-promise-in-scala (scala). – Sergiy Prydatchenko Jun 03 '13 at 11:21
  • That's basically exactly what I ended up doing. Thanks – Syllepsis Jun 05 '13 at 18:16

0 Answers0