Akka Actors: Need an example to understand some basics

↘锁芯ラ 提交于 2019-12-20 09:02:36

问题


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.

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!


回答1:


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])
}



回答2:


Create a DownloadActor class that manages the downloads, Have all DownloadActors share the same Dispatcher, Configure the Dispatcher according to your needs (max num threads, queue size etc), Have all DownloadActors linked to the same Supervisor, Configure the Supervisor according to your needs (probably OneForOneStrategy), Create a new DownloadActor for each new Download or use a LoadBalancer with an appropriate InfiniteIterator to distribute the downloads to the DownloadActors.

If you use AsycHttpClient to download the files, it supports download-resume.



来源:https://stackoverflow.com/questions/4594456/akka-actors-need-an-example-to-understand-some-basics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!