actorsystem

An Akka actors ‘remote’ example

While doing some crazy things with SARAH, I realized that the best way to solve a particular problem was to use remote Akka actors. I haven’t had the opportunity to work with Akka much since finishing the Scala Cookbook, so I dug around trying to find a simple Akka remote “Hello, world” example. Unable to find a good one, I read some stuff, and created it myself.

How to shut down the Akka Actor system (ActorSystem)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 13.7, “How to shut down the Akka Actor system.”

Problem

You want to shut down the Akka actor system, typically because your application is finished, and you want to shut it down gracefully.

Solution

Call the shutdown method on your ActorSystem instance:

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.”

Back to top

Problem

You want to stop one or more running Akka actors.

Back to top

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:

Table of Contents

  1. Problem
  2. Solution
Back to top

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.

Scala: A simple Akka “Futures” example

Akka Futures FAQ: Can you share a simple example that shows how to use an Akka Future?

Sure. To fix a problem in my Sarah application I needed to be able to run some operations concurrently. After digging through the Akka Actors documentation I found that you can run simple operations as a Future, almost as easily as this: