01: Simple Akka Tutorial in Scala step by step for the Java developers

Simple Akka Tutorial in Scala step by step with Maven. This is reactive programming in action, which is used for message driven interactions. This extends setting up Scala and Simple Akka tutorial in Java step by step

Step 1: Create a scala maven project named “simple-akka-scala” as shown below.

Step 2: Import the maven scala project “simple-akka-scala” into “Scala IDE” (i.e Eclipse Scala Plugin).

File –> Import –> “Existing Maven Projects” –> “Browse” –> simple-akka-scala.

Step 3: Delete the test scala files *.scala under “src/test/scala” as they will not compile.

Step 4: Modify the pom.xml file as shown below with 2.11 scala libraries.

Step 5: Under “src/main/scala” folder in the package “com.mytutorial” create the actor “Consumer.scala” as shown below.

Step 6: Under “src/main/scala” folder in the package “com.mytutorial” create the actor “Producer.scala” as shown below.

Run Producer.scala

Output:

Points to remember

1) In the “Producer.scala”, you can think of the object keyword as creating a singleton object of a class.

2) In the “Producer.scala”, the ! is similar to invoking the “tell” method on the ActoreRef “printNumbersConsumer”.

3) In the “Producer.scala”, the extends App, which is an executable trait. A trait is like an interface in Java. The “Producer.java” could have been written as shown below without extending the “App” trait.

In Java, a class can only inherit from one abstract class, but can implement many interfaces. Similarly, Scala classes can extend many traits. It is better to think of traits as mixins.

4) Scala’s “Unit” type roughly corresponds to void in Java.

5) In the pom.xml the plugin “scala-maven-plugin” from “net.alchim31.maven” is used for compiling/testing/running/documenting scala code in maven.

6) The “Producer.scala” uses the “for loop”, which is not functional programming. The above code can be rewritten functionally as shown below.

7) “Scala IDE for eclipse” is used to add the Scala natures. You can check the hidden file “.project

8) The hidden file “.classpath” will have classpath entries like “src/main/scala” and “src/test/scala”.


Java Interview FAQs

800+ Java Interview Q&As

Top