Akka Futures and Akka Remoting

16
AKKA FUTURES AND REMOTE ACTORS Piyush Mishra Software Consultant Knoldus Software LLP

Transcript of Akka Futures and Akka Remoting

Page 1: Akka Futures  and Akka Remoting

AKKA FUTURES AND REMOTE ACTORS

Piyush Mishra

Software Consultant

Knoldus Software LLP

Page 2: Akka Futures  and Akka Remoting

TOPICS COVERED TOPICS COVERED

Future

Composing Futures

For Comprehentions

Remoting

Enabling Remote capabilities for your Akka Project

Remote Actors

Look up Remote Actors

Page 3: Akka Futures  and Akka Remoting

FUTURE

Future is a data structure used to retrieve the result of some concurrent operation. This operation is usually performed by an Actor or by the Dispatcher directly. This result can be accessed synchronously (blocking) or asynchronously (non-blocking).

Page 4: Akka Futures  and Akka Remoting

Execution Context

import akka.dispatch.{ ExecutionContext, Promise }

implicit val ec = ExecutionContext.fromExecutorService(yourExecutorServiceGoesHere)

// Do stuff with your brand new shiny ExecutionContext

val f = Promise.successful("foo")

// Then shut your ExecutionContext down at some

// appropriate place in your program/application

ec.shutdown()

Page 5: Akka Futures  and Akka Remoting

Use With Actors

import akka.dispatch.Await

import akka.pattern.ask

import akka.util.Timeout

import akka.util.duration._

implicit val timeout = Timeout(5 seconds)

val future = actor ? msg // enabled by the “ask” import

val result = Await.result(future, timeout.duration).asInstanceOf[String]

Page 6: Akka Futures  and Akka Remoting

Use Directly

A common use case within Akka is to have some computation performed concurrently without needing the extra utility of an Actor. If you find yourself creating a pool of Actors for the sole reason of performing a calculation in parallel, there is an easier (and faster) way:

import akka.dispatch.Await

import akka.dispatch.Future

import akka.util.duration._

val future = Future {

"Hello" + "World"

}

val result = Await.result(future, 1 second)

Page 7: Akka Futures  and Akka Remoting

Functional FuturesAkka’s Future has several monadic methods that are very similar to the ones used 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")))

Page 8: Akka Futures  and Akka Remoting

For Comprehension

val 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)

Page 9: Akka Futures  and Akka Remoting

Composing Futures

val 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 c

val result = Await.result(f3, 1 second).asInstanceOf[Int]

Page 10: Akka Futures  and Akka Remoting

Callback methods

future 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)⇒

}

Page 11: Akka Futures  and Akka Remoting

Remoting

Remoting 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.

Page 12: Akka Futures  and Akka Remoting

Remoting

To 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

}}}

Page 13: Akka Futures  and Akka Remoting

Creating Remote Actor

val 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")

Page 14: Akka Futures  and Akka Remoting

Look up Remote Actor

val 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")

Page 15: Akka Futures  and Akka Remoting

References

http://blog.knoldus.com/2013/01/12/akka-futures-in-scala-with-a-simple-example/

https://github.com/meetumaltiar/Akka2Bench

http://doc.akka.io/docs/akka/1.3.1/scala/tutorial-chat-server.html

Page 16: Akka Futures  and Akka Remoting

Thank you