Skip to content

Instantly share code, notes, and snippets.

@oluies
Created October 9, 2011 16:16
Show Gist options
  • Save oluies/1273859 to your computer and use it in GitHub Desktop.
Save oluies/1273859 to your computer and use it in GitHub Desktop.
package cusin.filesplitter2
import scalax.io._
import Resource._
import java.util.concurrent.TimeUnit
import com.programmera.timer._
import akka.actor.Actor.actorOf
import akka.actor.Actor
import akka.actor.ActorRef
import akka.dispatch.Dispatchers
import akka.routing.CyclicIterator
import akka.routing.Routing
import akka.dispatch.Future
import akka.dispatch.Futures
import akka.dispatch.Futures.future
import cusin.filesplitter2.FileWriterBackend.fileWriterService
import cusin.filesplitter2.FileWriterBackend.CreateFileRequest
import scala.collection.mutable.ArrayBuffer
import akka.actor.PoisonPill
import akka.actor.Actor.Timeout
object FileWriterBackend {
case class CreateFileRequest(id:Int, filetype:String, contents:String )
val backendDispatcher = Dispatchers.newExecutorBasedEventDrivenDispatcher("backend-dispatcher")
.setCorePoolSize(2)
.build
val fileWriterService = actorOf[FileWriterService].start()
private def loadBalanced(poolSize: Int, actor: => ActorRef): ActorRef = {
val workers = Vector.fill(poolSize)(actor.start())
Routing.loadBalancerActor(CyclicIterator(workers)).start()
}
class FileWriterService extends Actor{
self.dispatcher = backendDispatcher
def receive = {
case CreateFileRequest(id, filetype, contents) => write(id, filetype, contents)
}
}
def write(id:Int, filetype:String, contents:String ) = {
implicit val codec = Codec.UTF8
val name = "\\temp\\out\\" + id + "_" + "type" + ".xml"
val someFile: Seekable = fromFile(name)
someFile.write(contents)
}
}
object App extends UsingTimer {
def main(args:Array[String]) {
Timer.addTimer("all")
implicit val timeout = Timeout(100000)
implicit val codec = Codec.UTF8
val in: Input = Resource.fromURL("file://\\temp\\sampledata\\sampledata_largerst.xml")
withTimer("all") {
//.filter(_.startsWith("<"))
val listOfFutures = in.lines().zipWithIndex.map { e =>
val f = Future{
fileWriterService ! CreateFileRequest(e._2,"delete",e._1)
}
f
}
val allDone = Futures.fold(())(listOfFutures.toIterable)( (_,_) => () ) flatMap { _ => fileWriterService ? PoisonPill }
println(allDone.await.get)
}
//Consumed time both = 496507801608
System.out.println("Consumed time all = " + Timer.consumedTime("all"))
// System.out.println("Consumed time par = " + Timer.consumedTime("par"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment