Skip to content
laforge49 edited this page Jul 22, 2011 · 10 revisions

A file loader service can be helpful when you need to read short files. The problem is that I/O blocks, so if too many actors are doing I/O a lot of threads can be blocked. And threads are expensive, as they have a large memory footprint. Here's the message for requesting a file load:

case class LoadFile(file: File)

When a file is loaded, the response is an Array[Byte]. The logic for requesting a file load is straight forward.

systemServices(LoadFile(file)) { ... }

To test this we will create an actor, ShortFileLoader, which loads a file when it receives a DoIt message.

case class DoIt()
class ShortFileLoader extends Actor(new Mailbox, null) {
  bind(classOf[DoIt], doit)
  def doit(msg: AnyRef, rf: Any => Unit) {
    val cwd = new File(".")
    println("cwd = " + cwd.getCanonicalPath)
    val file = new File("aShortTestFile.txt")
    println("test file exists = " + file.exists)
    systemServices(LoadFile(file))(rf)
  }
}

Clone this wiki locally