これを試してみてください。3 つのダウンローダを作成しますが、好きなだけ作成するように構成することもできます。これにより、3 つのダウンロード要求を同時に処理できます。
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])
}