Akka Futures and Akka Remoting

2,601 views

Published on

Published in: Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,601
On SlideShare
0
From Embeds
0
Number of Embeds
797
Actions
Shares
0
Downloads
57
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Akka Futures and Akka Remoting

  1. 1. AKKA FUTURES AND REMOTE ACTORS Piyush Mishra Software Consultant Knoldus Software LLP
  2. 2. TOPICS COVEREDFutureComposing FuturesFor ComprehentionsRemotingEnabling Remote capabilities for your Akka ProjectRemote ActorsLook up Remote Actors
  3. 3. FUTURE Future is a data structure used to retrieve the result of some concurrentoperation. This operation is usually performed by an Actor or by theDispatcher directly. This result can be accessed synchronously (blocking) orasynchronously (non-blocking).
  4. 4. Execution Contextimport akka.dispatch.{ ExecutionContext, Promise }implicit val ec =ExecutionContext.fromExecutorService(yourExecutorServiceGoesHere)// Do stuff with your brand new shiny ExecutionContextval f = Promise.successful("foo")// Then shut your ExecutionContext down at some// appropriate place in your program/applicationec.shutdown()
  5. 5. Use With Actorsimport akka.dispatch.Awaitimport akka.pattern.askimport akka.util.Timeoutimport akka.util.duration._implicit val timeout = Timeout(5 seconds)val future = actor ? msg // enabled by the “ask” importval result = Await.result(future, timeout.duration).asInstanceOf[String]
  6. 6. Use DirectlyA common use case within Akka is to have some computation performedconcurrently without needing the extra utility of an Actor. If you find yourselfcreating a pool of Actors for the sole reason of performing a calculation inparallel, there is an easier (and faster) way:import akka.dispatch.Awaitimport akka.dispatch.Futureimport akka.util.duration._val future = Future { "Hello" + "World"}val result = Await.result(future, 1 second)
  7. 7. Functional FuturesAkka’s Future has several monadic methods that are very similar to the onesused by Scala’s collections. These allow you to create ‘pipelines’ or ‘streams’that the result will travel through.val f1 = Future { "Hello" + "World"}val f2 = f1 map { x ⇒ x.length}val result = Await.result(f2, 1 second)result must be(10)f1.value must be(Some(Right("HelloWorld")))
  8. 8. For Comprehensionval f = for { a ← Future(10 / 2) // 10 / 2 = 5 b ← Future(a + 1) // 5 + 1 = 6 c ← Future(a - 1) // 5 - 1 = 4 if c > 3 // Future.filter} yield b * c // 6 * 4 = 24// Note that the execution of futures a, b, and c// are not done in parallel.val result = Await.result(f, 1 second)result must be(24)
  9. 9. Composing Futuresval f1 = ask(actor1, msg1)val f2 = ask(actor2, msg2)val f3 = for { a ← f1.mapTo[Int] b ← f2.mapTo[Int] c ← ask(actor3, (a + b)).mapTo[Int]} yield cval result = Await.result(f3, 1 second).asInstanceOf[Int]
  10. 10. Callback methodsfuture onSuccess { case "bar" ⇒ println("Got my bar alright!") case x: String ⇒ println("Got some random string: " + x) }future onFailure { case ise: IllegalStateException if ise.getMessage == "OHNOES" ⇒ //OHNOES! We are in deep trouble, do something! case e: Exception ⇒ //Do something else }future onComplete { case Right(result) ⇒ doSomethingOnSuccess(result) case Left(failure) ⇒ doSomethingOnFailure(failure) }
  11. 11. RemotingRemoting refers to the machenism by which you can send messages another actor running on other machines or in another JVM. This is a process to process communication.
  12. 12. RemotingTo enable remote capabilities in your Akka project you should, at a minimum,add the following changes to your application.conf file:akka {actor {provider = "akka.remote.RemoteActorRefProvider"}remote {transport = "akka.remote.netty.NettyRemoteTransport"netty {hostname = "127.0.0.1"port = 2552}}}
  13. 13. Creating Remote Actorval configString = """akka {actor { provider = "akka.remote.RemoteActorRefProvider" } remote {netty { hostname = "HOST"}}} akka { remote.netty.port = PORT}""" val customConf = ConfigFactory.parseString(configString)val remoteSystem = ActorSystem("RemoteApplication", ConfigFactory.load(customConf)) val remoteActor = remoteSystem..actorOf(Props("your actor" , "remote")
  14. 14. Look up Remote Actorval configString = """ akka {actor { provider = "akka.remote.RemoteActorRefProvider" } remote {netty {hostname = "HOST"}}} akka {remote.netty.port = PORT } """ val customConf = ConfigFactory.parseString(config) val system = ActorSystem("test", ConfigFactory.load(customConf)) val remoteActor = system.actorFor("akka://RemoteApplication@" +remoteHost + ":" + remotePort + "/user/remote")
  15. 15. Referenceshttp://blog.knoldus.com/2013/01/12/akka-futures-in-scala-with-a-simple-example/https://github.com/meetumaltiar/Akka2Benchhttp://doc.akka.io/docs/akka/1.3.1/scala/tutorial-chat-server.html
  16. 16. Thank you

×