02: Simple Akka tutorial in Scala with 2 Actors

This tutorial extends the previous tutorial Simple Akka Tutorial in Scala step by step for the Java developers, and Simple Akka tutorial in Java with 2 Actors.

Step 1: The “Consumer.scala” file that contains two actor classes “ConsumerOfOddNumbers” and “ConsumerOfEvenNumbers“. In Scala, you can have multiple classes in a single file as shown below.

Step 2: The “Producer.scala” that sends messages (i.e. numbers) to two actors “ConsumerOfOddNumbers” and “ConsumerOfEvenNumbers”.

Running Producer.scala

Output:

Points to ponder for Java developers

1) In Scala, you can have multiple classes in a single file as shown in “Consumer.scala”.

2) Matching multiple conditions in one statement with

It checks for “instanceof Integer” && being an odd number with the “%”.

3)Unit” in the “Producer.scala” is the return type. You can have implicit or explicit return types.

Implicit Unit return type:

Explicit Unit return type:

4) The “Producer.scala” written the functional programming way.


Java developer & architect Q&As

Java developers Q&As

Top