Introduction to Akka slide was presented to HyScala - Dec 2016 meet-up. Just prepared for the impatient and not goes too deep in to various aspects of Akka.
2. Excuse me !!!!!
I assume that you ,,,,
already stepped in to "Scala"land and its a JVM
language
written some Scala code and understand its
fundementals
familiar with SBT (like maven)
3. About this talk
is
for "Impatient"
help you to write Akka Code "right now" in
Scala
distinguish Actor model and Akka
not
teaching the partial functions and algebraic
data types in Scala
cover all aspects of Akka
4. Agenda
What makes us to consider Actor Model
Understanding Actor model of programming
Introducing to Akka
Akka Actor model approach
Setting up
Writing Actors using Akka (with code examples)
Moving forward (touchbase)
Q&A
5. What makes us to consider Actor
Model
Modern day programming need to process lot of
data in terms of
- volume
- variety
- velocity
our code started to use multiple CPU cores within
the machine or across a cluster
6. What makes us to consider Actor
Model (contd)
Obvious option is to consider aysnc programming
with "threads"
(atleast in Java) programming for threads is a
cumbersome
requires a lot of boiler-plate code
communicaiton between threads (with in and
outside JVM) is tedious
7. What makes us to consider Actor
Model (contd)
does this means the thread model is broken ??
No .. but it serves as a good foundation
just that it need better async abstraction
8. So how do we solve this ?
for writing concurrent and distributed programs,
we need threads to communicate easily
"Actor Model" to the rescue
9. What is an Actor
this model prescribes an "Actor" backed by a
thread model (by the underlying runtime or OS)
Async abstraction can be expressed as an actor in
the form of Class or function
an Actor framework
can create & manage actor
clients can communicate only by passing messages
(Async'ly) to the actor
actors can also communicate between themselves
10. What is an Actor
unlike Thread, an Actor
message driven
responds to certain messages from other Actors
(or non-actors)
can handle only one message at any instant of
time
12. Introducing Akka
Scala/Java API whcih implements the Actor model
provides a framework to create & manage actors ,
backed by JVM Thread model
Akka provides a mailbox for each actor : in-
memory queue to stage messages
clients acan send messages either by
! send or ? ask pattern (more on that later)
actor reveives messages and respond to clients
Akka actors use pattern matching to hande
messages
14. Setting up
sbt co-ordinates
in build.sbt
name := "hyscala-akka-intro-talk"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.4.14")
open the project in your IDE to get started
15. plain simple actor
class PlainActor extends Actor {
def receive: Receive = {
case _ =>
}
}
actor which can respond to some messages:
class PrimitiveHandlingActor extends Actor {
def receive: Receive = {
case "hello world" => println("""received hello world """)
case 11 => println("received ")
case _ => println("some other data type")
}
}
16. simple shopping cart
cart and order as two actors
show case their interaction with simple messages
covers actor to actor message passing
18. Cart Actor
class CartActor extends Actor {
val items = ListBuffer[Item]()
def receive: Receive = {
case item: Item => items.+=(item)
// done with adding items to cart
case Done => ???
}
}
19. Cart Actor - next version
class CartActor(billingActor: ActorRef) extends Actor {
...
case Done =>
billingActor ! Order(items.toList)
items.clear()
}
20. Billing Actor
class BilingActor extends Actor {
def receive: Receive = {
case order: Order =>
println(s"Here your bill ${Bill(order.items.foldLeft(0.0)((a
}
}
21. all together ....
ouput:
Here your bill Bill(175.0)
object AkkaIntroTalk extends App {
val actorSystem = ActorSystem("actorsystem")
val billingActor = actorSystem.actorOf(Props[BillingActor],
val cartActor = actorSystem.actorOf(Props(classOf[CartAct
cartActor ! Item("Apple", 3, 20.0)
cartActor ! Item("Orange", 3, 15.0)
cartActor ! Item("Butter", 1, 70.0)
cartActor ! Done
}
22. Learning Akka
Online
Akka Docs
Let it crash - blog
Books
Learning Akka
Reactive Programming with Scala and Akka -
(I've authored this book)