Akka演员:需要一个例子来了解一些基础知识 [英] Akka Actors: Need an example to understand some basics
问题描述
我正在修补Akka,并需要一些建议如何实现我想到的具体。我想有一个演员,我可以发送 DownloadFile(URI,文件)
消息,并下载它。由于这可以并行,我不想下载文件后文件,但有并发下载的限制。
I'm tinkering with Akka and need some advice how to implement something specific i have in mind. I want to have an actor which i can send a DownloadFile(URI, File)
message and downloads it. Since this can be paralleled, I don't want to download file after file but have a limit of concurrent downloads.
使用Akka建模的方法是什么?其他的事情,想到的是:如果一个工人的演员因某种原因死亡会发生什么?如何重试下载?等等。
Whats the intended way to model something like this with Akka? Other things that come to mind are: What happens if one of the "worker" actor dies for some reason? How to retry the download? Etc. etc.
我知道这是一个非常巨大的问题,但我希望有人花时间回答它!谢谢!
I know this is a very huge question but i hope someone takes the time to answer it! Thank you!
推荐答案
它会创建三个 - 但你可以配置它来创建尽可能多的你喜欢 - 下载器,以便三个下载请求可以并发处理。
Give this a shot; it creates three - but you could configure it to create as many as you like - downloaders, so that three download requests could be processed concurrenty.
sealed trait DownloaderMessage
case class DownloadFile(uri: URI, file: File) extends DownloaderMessage
object Downloader {
val dispatcher = Dispatchers.newExecutorBasedEventDrivenWorkStealingDispatcher("pool").build
}
class Downloader extends Actor {
self.lifeCycle = Permanent
self.dispatcher = Downloader.dispatcher
def receive = {
case DownloadFile(uri, file) =>
// do the download
}
}
trait CyclicLoadBalancing extends LoadBalancer { this: Actor =>
val downloaders: List[ActorRef]
val seq = new CyclicIterator[ActorRef](downloaders)
}
trait DownloadManager extends Actor {
self.lifeCycle = Permanent
self.faultHandler = OneForOneStrategy(List(classOf[Exception]), 5, 5000)
val downloaders: List[ActorRef]
override def preStart = downloaders foreach { self.startLink(_) }
override def postStop = self.shutdownLinkedActors()
}
class DownloadService extends DownloadManager with CyclicLoadBalancing {
val downloaders = List.fill(3)(Actor.actorOf[Downloader])
}
这篇关于Akka演员:需要一个例子来了解一些基础知识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!