alvin's blog

How to send a message to an Akka Actor and wait for a reply

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 13.10, “How to send a message to an Akka Actor and wait for a reply.”

Problem

You have one actor that needs to ask another actor for some information, and needs an immediate reply. (The first actor can’t continue without the information from the second actor.)

Solution

Use the ? or ask methods to send a message to an Akka actor and wait for a reply, as demonstrated in the following example:

How to stop Akka Actors (Scala)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 13.6, “How to stop Akka Actors.”

Problem

You want to stop one or more running Akka actors.

Solution

There are several ways to stop Akka actors. The most common ways are to call system.stop(actorRef) at the ActorSystem level or context.stop(actorRef) from inside an actor.

There are other ways to stop an actor:

How to get started with a simple Scala/Akka Actor (Hello, world)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 13.1, “How to get started with a simple Scala/Akka Actor.”

Problem

You want to begin using actors to build concurrency into your applications.

Solution

Create an actor by extending the akka.actor.Actor class and writing a receive method in your class. The receive method should be implemented with a case statement that allows the actor to respond to the different messages it receives.

An introduction to Scala/Akka Actors (Actors and Concurrency)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is the introduction to Chapter 13, “Actors and Concurrency.”

In Scala you can still use Java threads, but the “Actor model” is the preferred approach for concurrency. The Actor model is at a much higher level of abstraction than threads, and once you understand the model, it lets you focus on solving the problem at hand, rather than worrying about the low-level problems of threads, locks, and shared data.

Syndicate content