2 Up and running

 

This chapter covers

  • Defining an actor and its behavior
  • Instantiating and sending messages to an actor
  • Keeping state in the actor with a variable
  • Keeping state in the actor with its behavior
  • Scheduling sending a message

This chapter covers in practice the basic parts of Akka from the previous chapter: creating an actor, sending messages to actors, and setting actors’ behavior. It also explains how to handle state and send messages with a scheduled delay.

Without further ado, let’s start with a minimal Akka application: a wallet into which you can deposit money. This can be part of a larger application that handles betting on sports events with money from the wallet. You learn how to create such a betting application later in this book, but first you need to learn some basics.

Note

To run the examples in this book, follow the instructions in appendix A to install all the tools. When you’ve finished the installation, return here and get started.

In this chapter, you get to know different variants of such a wallet, which will guide you through some Akka primitives:

  • At first, the wallet’s only function is to print the deposited money to the console without keeping track of the total amount.
  • Later, you learn how to store the money in a variable.
  • Finally, you deactivate/activate the wallet.

2.1 Printing money

2.2 Starting to code

2.2.1 The protocol of the actor

2.2.2 Creating an application and instantiating the actor

2.2.3 Sending messages

2.2.4 Implementing the actor: Receiving messages

2.2.5 Terminating the system

2.2.6 The application

2.2.7 The solution in Git