Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Akka Cookbook

You're reading from   Akka Cookbook Recipes for concurrent, fast, and reactive applications

Arrow left icon
Product type Paperback
Published in May 2017
Publisher Packt
ISBN-13 9781785288180
Length 414 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Authors (3):
Arrow left icon
Vivek Mishra Vivek Mishra
Author Profile Icon Vivek Mishra
Vivek Mishra
Piyush Mishra Piyush Mishra
Author Profile Icon Piyush Mishra
Piyush Mishra
Héctor Veiga Ortiz Héctor Veiga Ortiz
Author Profile Icon Héctor Veiga Ortiz
Héctor Veiga Ortiz
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Diving into Akka FREE CHAPTER 2. Supervision and Monitoring 3. Routing Messages 4. Using Futures and Agents 5. Scheduling Actors and Other Utilities 6. Akka Persistence 7. Remoting and Akka Clustering 8. Akka Streams 9. Akka HTTP 10. Understanding Various Akka patterns 11. Microservices with Lagom

Defining the actor's behavior and state

In this recipe, we will define an actor, which will receive some messages, and apply its behavior to its state. After that, we will create that actor inside the actor system.

We will see what is meant by the terms behavior and the state of an actor.

Getting ready

To step through this recipe, we need to import the Hello-Akka project in any IDE, like intelliJ Idea, and ensure that SBT is installed on our machine to build and run the Scala project from the console.

How to do it...

  1. Open the project Hello-Akka in an IDE like intelliJ Idea, and create a Scala file, BehaviorAndState.scala, inside the package, com.packt.chapter1.
  2. Add an import to the top of the file: import akka.actor.Actor.

Inside the file, define the actor as follows:

        class SummingActor extends Actor { 
// state inside the actor
var sum = 0
// behaviour which is applied on the state
override def receive: Receive = {
// receives message an integer
case x: Int => sum = sum + x
println(s"my state as sum is $sum")
// receives default message
case _ => println("I don't know what
are you talking about")
}
}

Here, we are not creating an actor, we are only defining the state and behavior.

  1. From the previous recipe, we know that ActorSystem is the home for actors, and we know how to create an ActorSystem. Now, we will create the actor inside it.

Add the following imports to the top of the file:

        import akka.actor.Props 
import akka.actor.ActorSystem
object BehaviourAndState extends App {
val actorSystem = ActorSystem("HelloAkka")
// creating an actor inside the actor system
val actor = actorSystem.actorOf(Props[SummingActor])
// print actor path
println(actor.path)
}
  1. Run the preceding application from the IDE or from the console, and it will print the actor path.

You can run the application from the console using the following command:

      sbt "runMain com.packt.chapter1.BehaviorAndState"
akka://HelloAkka/user/$a

Here, HelloAkka is the ActorSystem name, user is the user guardian actor, that is, the parent actor for your actor, and $a is the name of your actor.

You can also give a name to your actor:

val actor = actorSystem.actorOf(Props[SummingActor],
"summingactor")

If you run the application again, it will print the actor name as summingactor.

The output will be as follows:

akka://HelloAkka/user/summingactor  

How do we create an actor if it takes an argument in the constructor as shown in the following code:

        class SummingActorWithConstructor(intitalSum: Int)
extends Actor {
// state inside the actor
var sum = 0
// behaviour which is applied on the state
override def receive: Receive = {
// receives message an integer
case x: Int => sum = intitalSum + sum + x
println(s"my state as sum is $sum")
// receives default message
case _ => println("I don't know what
are you talking about")
}
}

For this, we use the following code:

        actorSystem.actorOf(Props(classOf[
SummingActorWithConstructor], 10), "summingactor")

How it works...

As we know from the previous recipe, the ActorSystem is a place where the actor lives.

In the preceding application, we define the actor with its state and behavior, and then create it inside Akka using the API provided by Akka.

In case of the summingactor, the state is the variable sum and the behavior is adding of the integer to the sum as soon as the message arrives.

There's more...

You have been reading a chapter from
Akka Cookbook
Published in: May 2017
Publisher: Packt
ISBN-13: 9781785288180
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image