Actor basic operations
In this section, we will discuss some of the useful and frequently used Akka Actor operations. In the Akka Toolkit, an Actor can perform the following operations:
- Create an Actor
- Send messages
- Receive messages
- Stop an Actor
- Become/unbecoming an Actor
- Supervise an Actor
Let's define a simple Actor to explore all these operations, one by one, in the following sections.
Defining an Actor
Before creating an Actor and performing its operations, we need to define it.
In the Akka Toolkit, we can define an Actor class by extending the Actor
Trait and providing implementation for the receive
method, as follows:
class SampleActor extends Actor { def receive: Receive = { //Implmentation } }
Here's the description:
- Here,
Actor
is a trait available in theakka.actor
package - The
Actor
trait has an abstract method—receive
- The
receive ()
function is a Scala Partial Function - It takes a type of
Any
and returnsUnit
- Its complete signature is this:
def receive: PartialFunction[Any,Unit...