actor - How to cancel (or just interrupt) a scala.concurrent.Future? -


i have java library performs long running blocking operations. library designed respond user cancellation requests. library integration point callable interface.

i need integrate library application within actor. initial thought this:

import scala.concurrent.future import scala.concurrent.executioncontext.implicits.global val callable: java.util.concurrent.callable[void] = ??? val fut = future {   callable.call() } fut.onsuccess {   case _ => // continue on success path } fut.onfailure {   case throwable => // handle exceptions } 

i think code work in as not block actor. don't know how provide way cancel operation. assume while callable processing, actor receives message indicates should cancel operation being worked on in callable, , library responsive cancellation requests via interrupting processing thread.

what best practice submit callable within actor , sometime later cancel operation?

update

to clear, library exposes instance of java.util.concurrent.callable interface. callable in , of not provide cancel method. callable object implemented in such way responsive cancellation due interrupting thread. in java, done submitting callable executor. return java.util.concurrent.future. future object provides cancel method.

in java following:

executorservice executor = ... callable c = ... future f = executor.submit(c); ... // more stuff ... // if need cancel callable task this: f.cancel(true); 

it seems there disconnect between java.util.concurrent.future , scala.concurrent.future. java version provides cancel method while scala 1 not.

in scala this:

// when akka actor receives message process  // long running/blocking task schedule run // on different thread this:  import scala.concurrent.future import scala.concurrent.executioncontext.implicits.global val callable: java.util.concurrent.callable[void] = ??? val fut = future {   callable.call() } fut.onsuccess {   case _ => // continue on success path } fut.onfailure {   case throwable => // handle exceptions }  // now, if/when actor receives message cancel // task because taking long finish (even // though running in background) there no way // know of cancel or interrupt  // scala.concurrent.future. 

is there idiomatic scala approach cancelling scala.concurrent.future?

from understood library exposing interface has call , cancel method, right? i'm assuming can call cancel whenever want to. example 1 below should started.

class interruptablethingy extends actor {   implicit val ctx = context.system.dispatchers.lookup("dedicated-dispatcher")          var counter = 0   var tasks = map.empty[int, hascancelmethod]    def receive = {     case "dothing" =>       counter += 1       val id = counter        val thing = ???       future { thing.call() } onsuccess {} // ...        tasks(id) = thing       sender() ! id      case interrupt(id) =>        tasks(id).cancel()       tasks -= id   } } case class interrupt(taskid: int) 

please notice we're using dedicated dispatcher blocking futures. pattern can configure dedicated dispatcher fittingly blocking workloads (and won't eat resourced in default dispatcher). dispatchers explained in more detail in docs here: http://doc.akka.io/docs/akka/2.3.3/scala/dispatchers.html


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -