我有一个akka演员(工人)接收请求并回复它。请求处理可能需要3-60分钟。来电者(也是演员)目前正在使用!等待future.get,但是如果需要,可以更改Caller actor的设计。另外,我目前正在使用EventDriven调度程序。
如何取消(用户启动)请求处理,以便释放工作者并返回就绪状态以接收新请求?我希望有一个类似于java.util.concurrent.Future的取消方法但在Akka 1.1.3中找不到的方法
修改
我们试图通过completeWithException
获取我们正在寻找的行为:
object Cancel {
def main(args: Array[String]) {
val actor = Actor.actorOf[CancelActor].start
EventHandler.info(this, "Getting future")
val future = (actor ? "request").onComplete(x => EventHandler.info(this, "Completed!! " + x.get))
Thread.sleep(500L)
EventHandler.info(this, "Cancelling")
future.completeWithException(new Exception("cancel"))
EventHandler.info(this, "Future is " + future.get)
}
}
class CancelActor extends Actor {
def receive = {
case "request" =>
EventHandler.info(this, "start")
(1 to 5).foreach(x => {
EventHandler.info(this, "I am a long running process")
Thread.sleep(200L)
})
self reply "response"
EventHandler.info(this, "stop")
}
}
但这并没有阻止这个漫长的过程。
[INFO] [9/16/11 1:46 PM] [main] [Cancel$] Getting future
[INFO] [9/16/11 1:46 PM] [akka:event-driven:dispatcher:global-2] [CancelActor] start
[INFO] [9/16/11 1:46 PM] [akka:event-driven:dispatcher:global-2] [CancelActor] I am a long running process
[INFO] [9/16/11 1:46 PM] [akka:event-driven:dispatcher:global-2] [CancelActor] I am a long running process
[INFO] [9/16/11 1:46 PM] [akka:event-driven:dispatcher:global-2] [CancelActor] I am a long running process
[INFO] [9/16/11 1:46 PM] [main] [Cancel$] Cancelling
[ERROR] [9/16/11 1:46 PM] [akka:event-driven:dispatcher:global-7] [ActorCompletableFuture]
java.lang.Exception: cancel
at kozo.experimental.Cancel$.main(Cancel.scala:15)
...
[INFO] [9/16/11 1:46 PM] [akka:event-driven:dispatcher:global-2] [CancelActor] I am a long running process
[INFO] [9/16/11 1:46 PM] [akka:event-driven:dispatcher:global-2] [CancelActor] I am a long running process
[INFO] [9/16/11 1:46 PM] [akka:event-driven:dispatcher:global-2] [CancelActor] stop
相反,请考虑java.util.concurrent.Future:
的行为object Cancel2 {
def main(args: Array[String]) {
val executor: ExecutorService = Executors.newSingleThreadExecutor()
EventHandler.info(this, "Getting future")
val future = executor.submit(new Runnable {
def run() {
EventHandler.info(this, "start")
(1 to 5).foreach(x => {
EventHandler.info(this, "I am a long running process")
Thread.sleep(200L)
})
}
})
Thread.sleep(500L)
EventHandler.info(this, "Cancelling")
future.cancel(true)
EventHandler.info(this, "Future is " + future.get)
}
}
这会阻止长时间运行的过程
[INFO] [9/16/11 1:48 PM] [main] [Cancel2$] Getting future
[INFO] [9/16/11 1:48 PM] [pool-1-thread-1] [anon$1] start
[INFO] [9/16/11 1:48 PM] [pool-1-thread-1] [anon$1] I am a long running process
[INFO] [9/16/11 1:48 PM] [pool-1-thread-1] [anon$1] I am a long running process
[INFO] [9/16/11 1:48 PM] [pool-1-thread-1] [anon$1] I am a long running process
Exception in thread "main" java.util.concurrent.CancellationException
...
[INFO] [9/16/11 1:48 PM] [main] [Cancel2$] Cancelling
答案 0 :(得分:2)
您还可以检查演员中未来的状态。
class MyActor extends Actor {
def receive = {
case msg =>
while(!self.senderFuture.get.isCompleted) {
performWork(msg)
}
self reply result
}
...
}
这要求邮件与'?'一起发送或者'问'但是。 希望能帮助到你。
答案 1 :(得分:1)
如果您只是在VM中,您可以使用Job消息传递AtomicBoolean,并间歇性地检查您的演员是否应该中止。
actor ! Job(..., someAtomicBoolean)
class MyActor extends Actor {
def receive = {
case Job(..., cancelPlease) =>
while(cancelPlease.get == false) {
performWork
}
self reply result
}
}