alvin's blog

Getting started with the Scala REPL (command-line shell)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.1, “How to get started with the Scala REPL.”

Problem

You want to get started using the Scala REPL, including understanding some of its basic features, such as tab completion, starting the REPL with different options, and dealing with errors.

Solution

To start the Scala REPL, type scala at your operating system command line:

Akka/Scala: How to monitor the death of an Actor with 'watch'

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 13.8, “Monitoring the death of an Akka Actor with ‘watch’”.

Problem

You want an actor to be notified when another actor dies.

Solution

Use the watch method of an actor’s context object to declare that the actor should be notified when an actor it’s monitoring is stopped.

Examples of how to use parallel collections in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 13.12, “Examples of how to use parallel collections in Scala.”

Problem

You want to improve the performance of algorithms by using parallel collections.

Solution

When creating a collection, use one of the Scala’s parallel collection classes, or convert an existing collection to a parallel collection. In either case, test your algorithm to make sure you see the benefit you’re expecting.

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:

Concurrency with Scala Futures (Futures tutorial)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 13.9, “Simple concurrency with Scala Futures.”

Problem

You want a simple way to run one or more tasks concurrently, including a way to handle their results when the tasks finish. For instance, you may want to make several web service calls in parallel, and then work with their results after they all return.

How to shut down the Akka Actor system

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

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:

Syndicate content