To execute Futures in Scala we use default global ExecutionContext. Here is a way to define customized ExecutionContext to use in your application. You can create ExecutionContext from Java Executor Service, which allows you to tune the thread pool for executing calls asynchronously independently from the rest of your application. Also, scenarios where computation would take long time to finish, it is good idea to use separate ExecutionContext.
import java.util.concurrent.Executors import concurrent.ExecutionContext val executorService = Executors.newFixedThreadPool(10) val executionContext = ExecutionContext.fromExecutorService(executorService)
0 Comments
Here is Akka based approach to crawl through web pages and identify links embedded. Below code accepts domain name, main page link and depth as input parameters to fetch the links. Depth signifies the level of nesting to crawl.
Sample Output with depth:2, domain: https://en.wikipedia.org/, main link: /wiki/Main_Page: Following are the links fetched from https://en.wikipedia.org//wiki/Main_Page for depth of 2 (Depth: ,1) /wiki/Moment_magnitude_scale /wiki/Environmental_degradation /wiki/Edward_Witten /wiki/Climate_change /wiki/Listed_building /wiki/S-duality /wiki/Wikipedia /wiki/DNA_nanotechnology ........ Messages.scalapackage SimpleCrawler sealed trait Messages case object GetLinks extends Messages case class Crawl(url: String) extends Messages case class Result(links: Set[String]) extends Messages Crawler.scalapackage SimpleCrawler import akka.actor.Actor import scala.io.Source class Crawler(domain: String) extends Actor { val regexForLinks = """\"/wiki/[a-zA-Z\-_]+\"""".r def receive = { case Crawl(url: String) => sender() ! Result(getLinks(url)) } def getLinks(url: String): Set[String] = { val source = Source.fromURL(domain + url, "utf8") val content = source.getLines.mkString source.close() regexForLinks.findAllMatchIn(content).map (_.toString().replace("\"", "")).toSet } } Master.scalapackage SimpleCrawler import akka.actor.{Props, Actor} class Master(domain: String, mainLink: String, depth: Int) extends Actor { var numOfWorkers: Int = 1 var depthCount: Int = 0 var numResults: Int = 0 var singleWorkerMode: Boolean = false var netResult: Map[Int, Set[String]] = Map.empty[Int, Set[String]] var results: Set[String] = Set.empty[String] val singleCrawler = context.actorOf(Props(new Crawler(domain)), name ="singleCrawler") def receive = { case GetLinks => singleWorkerMode = true singleCrawler ! Crawl(mainLink) case Result(result: Set[String]) => numResults += 1 results = results ++ result if(numResults == numOfWorkers) { depthCount += 1 netResult += (depthCount -> results) numResults = 0 numOfWorkers = results.size results = Set.empty[String] } if(depthCount == depth) { println("Following are the links fetched from %s/%s for depth of %d". format(domain, mainLink, depth)) netResult.map{ case (d, respose) => println("Depth: " + d); respose.map(println) } context.system.terminate() } else if (singleWorkerMode) { singleWorkerMode = false for { links <- netResult.get(depthCount) } yield { links map { link => singleCrawler ! Crawl(link) } } } } } Main.scalapackage SimpleCrawler import akka.actor.{Props, ActorSystem} object Main extends App { val domain = "https://en.wikipedia.org" val mainLink = "/wiki/Main_Page" val depth = 2 val system = ActorSystem("SimpleCrawler") val master = system.actorOf(Props(new Master(domain, mainLink, depth)), name ="master") master ! GetLinks }
The general expansion of e^x = 1 + x + x^2/2 + .... = ∑x ^n / n!. 'numOfIteration' signifies value of 'n' in the expansion
Below code creates an actor for every iteration and sums the result received from each actor to get final net result. Actor RoundRobinPool mechanism is used to schedule jobs to each actor. Result on running the below code to calculate e ^ 3, after first 10 iterations is : 20.06339285714286 Messages.Scalapackage MasterSlaveExample sealed trait Messages case object Calculate extends Messages case class Work(x: Double, iterationValue: Int) extends Messages case class Result (result: Double) extends Messages case object Close extends Messages Worker.Scalapackage MasterSlaveExample import akka.actor.Actor class Worker extends Actor { def receive = { case Work(x: Double, iterValue: Int) => sender() ! Result(Math.pow(x,iterValue) / factorial(iterValue)) case Close => context.stop(self) } def factorial (n: Int) = { def aux (product: Int, value: Int): Int = { if(value == 0) return product aux(product * value , value - 1) } aux(1, n) } } Master.Scalapackage MasterSlaveExample import akka.actor.{Props, Actor} import akka.routing.{RoundRobinPool} class Master (x: Double, numOfIterations: Int) extends Actor { var netResult = 0.0 var numOfResults = 0 val router = context.actorOf(Props[Worker].withRouter(RoundRobinPool(numOfIterations)), name = "router") def receive = { case Calculate => for (i <- 0 until numOfIterations) router ! Work(x, i) case Result(result: Double) => netResult += result numOfResults += 1 sender() ! Close if ( numOfResults == numOfIterations) { println("Value of e^%s is %s \n".format(x,netResult)) context.system.terminate() } } } Main.scalapackage MasterSlaveExample import akka.actor.{Props, ActorSystem} object Main extends App { val system = ActorSystem("masterSlaveExample") val master = system.actorOf(Props(new Master(3.0,10))) master ! Calculate } PingPongSystem.scalaimport akka.actor.{Props, ActorSystem, ActorRef, Actor} sealed trait PingPongMessage case object Begin extends PingPongMessage case object Ping extends PingPongMessage case object Pong extends PingPongMessage case object End extends PingPongMessage class PlayerA(opponent: ActorRef) extends Actor { var rallyCount = 0 def receive = { case Begin => opponent ! Ping case Pong => if(rallyCount < 10) { println(self + ":" + rallyCount + "-> Pong") rallyCount += 1 sender ! Ping } else { println("PlayerA - Bye") sender ! End context.stop(self) } } } class PlayerB extends Actor { def receive = { case Ping => println(self + ":" + "-> Ping") sender ! Pong case end => println("PlayerB - Bye") context.system.terminate() } } object PingPongSystem extends App { val system = ActorSystem("pingPongSystem") val playerB = system.actorOf(Props[PlayerB], "playerB") val playerA = system.actorOf(Props(new PlayerA(playerB)), "playerA") playerA ! Begin }
A simple example to demonstrate the flow of messages in a simple Akka based system. Lets create an actor "Tom" who greets 'Hello World' when he receives Greet message from Master Actor.
Tom.Scalaimport akka.actor.Actor sealed trait Greetings case object Greet extends Greetings case object Done extends Greetings class Tom extends Actor { def receive = { case Greet => println("Hello World") sender() ! Done } } Master.scalapackage akka.tutorial.first.scala import akka.actor.{Actor, Props} class Master extends Actor { override def preStart () = { val greeter = context.actorOf(Props[Tom],"greeter") greeter ! Greet } def receive = { case Done => context.stop(self) } } Main.scalapackage akka.tutorial.first.scala object Main { def main (args: Array[String]) { akka.Main.main(Array(classOf[Master].getName)) } }
Basic operations on Optional Lists with optional values.
Option[Seq[Option[Int]] Example
Basic operations on Optional Maps
Option[[Map[String,Int]] Example
Basic Operations on Maps with Optional Values.
Map[String,Option[Int]] Example
Some basic operations on Option List of type Int. Same can be extended to other types
Option[Int] Example
|
Archives
October 2016
Categories
All
|