chapter two

2 Up and running

 

This chapter covers

  • Defining an actor and its behavior
  • Instantiating and sending messages to an actor
  • Keep state in the actor with a variable
  • Keep 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 their behavior. It also explains how to handle state and how to send messages with a scheduled delay.

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

In the course of this chapter, you will get to know different variants of such a wallet, which will guide you through some Akka primitives. At first, the wallet only function is to print the deposited money on the console without keeping track of the total amount. Later, you'll learn how to store the money in a variable and finally deactivate/activate the wallet.

The examples in this book are located in the repository https://github.com/franciscolopezsancho/akka-topics

To run them, you need to follow the instructions in the appendix to install all the tools.

2.1 Printing money

2.2 Starting to code

2.2.1 The protocol of the actor

2.2.2 Creating the app and instantiation of the actor

2.2.3 Sending messages

2.2.4 Implementation of the actor. Receiving messages.

2.2.5 Terminating the system

2.2.6 The whole application

2.2.7 The solution in git

2.2.8 running the app

2.3 Keep state with a variable

2.4 Keep state with behaviors

2.5 Scheduling a message

2.6 Summary