Set Up Akka

Before we go any further, let’s set up so that you’re all ready to start coding in Akka. Akka.io can be written using Java and Scala. To reduce the learning time and easy to learn I will choose Scala to explain Akka based concurrency. At the time of this post Akka stable version is – Akka 2.3.6 and supported Scala stable version is - Scala 2.11.

Scala Setup with SBT

The Simple Build Tool (SBT) has become most popular and easier to work. SBT is basically a building tool like Maven and Ant. Though SBT is a new for building tool mostly it downloads all the libraries from Maven repository ☺. In my scala blog series I have discussed about installing Scala with SBT very clearly. After setting up scala with SBT to download akka we need only to add dependencies from the maven repository

name := "akka-tutorial"

version := "1.0"

scalaVersion := "2.11.1"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.3.6"
)

Pull Akka Actors into SBT

If your using an Intellij IDEA once you have added the dependencies it will automatically download all the needed components. To update it through command line we’ll run sbt update, so we can let SBT do its things.

image
Figure 1 - Downloading Akka.io
image
Figure 2 - Akka.io libraries

We’re now ready to go! :D

import java.util.concurrent.Executors
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future, ExecutionContext}

/**
 * Created by renienj on 10/17/14.
 */
object MainAkka {
  val pool = Executors.newCachedThreadPool()
  implicit val ec = ExecutionContext.fromExecutorService(pool)
  def main(args: Array[String]) {
    val duration = Duration(10000, "millis")
    val future = Future { "<3 Akka Awesome <3 " }
    val result = Await.result(future, duration)
    println(result)
    pool.shutdown()
  }
}

To make sure everything can be complied and run properly create MainAkka.scala file, add the above code compile and run. Its juts a test application with Akka Future and if it prints the following output :

<3 Akka Awesome <3

That’s it ☺. Now the tools are ready to build a large-scale concurrent application. In my next blog post lets discuss about Actors in Akka.

Blog Series