Created
November 23, 2011 08:59
-
-
Save freekh/1388237 to your computer and use it in GitHub Desktop.
Akka Presentation: GOTO Prague 2011
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor._ //Actor | |
import akka.actor.Actor._ //actorOf | |
import akka.dispatch._ //Futures | |
import java.util.concurrent.CountDownLatch //Need for demo | |
//Create an Actor here... | |
class AnActor(var state : String) extends Actor { | |
def receive = { | |
case m => { | |
state = m.toString | |
println("replying with: " + state) | |
self.reply(m) | |
} | |
} | |
} | |
object helpers { | |
class Waiter(latch : CountDownLatch) extends Actor { | |
def receive = { | |
case m => latch.countDown | |
} | |
} | |
def sleep(ms : Long) = Thread.sleep(ms) | |
def timeItTook(f : => Unit) : Unit = { | |
val time = System.currentTimeMillis | |
f //running what ever is blocking | |
println("Took: %s millis" format (System.currentTimeMillis - time)) | |
} | |
} | |
object AkkaPres extends App{ | |
import helpers._ | |
//Create and start your actor | |
val actor = actorOf(new AnActor("init")).start | |
//Messages | |
val res = actor ? "hello prague" | |
//Other ways of initializing... | |
//Observe non-blocking behavior... | |
println("non-blocking") | |
//Till I actually get the result... | |
println("waited for the result: " + res.get) | |
/**FUTURES**/ | |
//Asking for results == create a Future | |
//A simple future | |
val future = Future { sleep(100); "hello from Future"} | |
timeItTook{ | |
println("got: " + future.get) | |
} | |
//Am I non-blocking? | |
//Chain up several future tasks and make sure that you can wait for it | |
//waiter counts down the latch | |
// when it gets messages | |
val latch = new CountDownLatch(1) | |
val waiter = actorOf(new Waiter(latch)).start(); | |
val f = Future{ sleep(100); "a" }.onComplete { | |
value => actor ? "checking" | |
}.onComplete { | |
value => waiter ! "count" | |
} | |
latch.await | |
println("completed") | |
//Utils for Futures | |
val firstOf = Futures.firstCompletedOf( List( | |
Future { sleep(200); "200" }, | |
Future { sleep(100); "100" } | |
)) | |
println("first was : " + firstOf.get) | |
//Composing futures | |
val c = for { | |
a <- Future { 2 } | |
b <- Future { sleep(100); 3 } | |
} yield (a + b) | |
println("2 + 3 = " + c.get) | |
//Shutting down all actors to clean up | |
Actor.registry.actors foreach (_ ! PoisonPill) | |
} | |
//Remoting in 1.2: | |
object Server extends App{ | |
remote.start("localhost", 1337) | |
remote.register("aservice", actorOf(new AnActor("init state")).start) | |
} | |
object Client extends App{ | |
remote.start("localhost", 6164) | |
val actor = remote.actorFor("aservice", "localhost", 1337) | |
println("remotly got: " + (actor ? "hello").get) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment