Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Akka Tutorials: Messages Between Actors v.2

DZone's Guide to

Akka Tutorials: Messages Between Actors v.2

Learn more about Akka, specifically message between actors. Messaging between actors is a neat way to create a close interaction between them.

· Java Zone ·
Free Resource

Distribute microservices between the private and public clusters, yet maintain bi-directional connectivity. Content provided by IBM Developer.

How can we build a close interaction between Actors? Just with the help of messaging. In the previous post, I’ve already demonstrated how we can send messages between Actors. In this post, I want to continue on the topic of message exchange, but this time, let’s develop cooperation of two Actors which interact with each other.

How can we force an ActorA to send a message to ActorB? We can use the tell(message, sender) method. But this approach implies an explicit invocation of the method. In order to send a message from A to B as a reaction to something, we need to path ActorRef of B to A. This can be done with a help of a constructor or message.

The code of theTesterActor:

import akka.actor.{Props, Actor}
import akka.event.Logging
import com.actor.DeveloperActor.{Fix, Bug, NewFeature}

import scala.util.Random

class TesterActor extends Actor {

  val log = Logging.getLogger(context.system, this)

  def receive = {
    case feature: NewFeature => {
      log.info(s"Testing '${feature.name}'...")
      val bugExists = Random.nextInt(10)
      if (bugExists > 7) {
        log.info("A bug was found")
        sender() ! Bug
      }
    }
    case Fix => log.info(s"Verifying the fix...")
    case _ => log.info("Watching YouTube")
  }

}

object TesterActor {
  def props = Props(new TesterActor)
}

This represents a software tester. Its behavior is pretty natural, and the tester can process 3 cases:

  • New feature received. The tester starts its testing. There is a probability that the tester will find a bug. As a result, it sends the bug to the developer who worked on this feature and it needs to fix it.
    If there are no bugs found, nothing happens next.
  • A fix received. The tester verifies it.
  • Some other message received. The tester watches YouTube.

Let’s look at the DeveloperActor:

import akka.actor.{ActorRef, Props, Actor}
import akka.event.Logging

class DeveloperActor(tester: ActorRef) extends Actor {
  import DeveloperActor._
  val log = Logging.getLogger(context.system, this)

  def receive = {
    case feature: NewFeature => {
      log.info(s"Working on feature '${feature.name}'")
      tester ! feature
    }
    case Bug => {
      log.info("Fixing a bug")
      tester ! Fix
    }
    case _ => log.info("Playing StarCraft2")
  }

}

object DeveloperActor {

  def props(tester: ActorRef) = Props(new DeveloperActor(tester))

  case class NewFeature(name: String)
  object Bug
  object Fix

}

There are two things which I want to emphasize:

  • The Actor has the argument in the constructor.
  • When the Actor receives the NewFeature it sends it to the Actor passed as the constructor argument.

Finally here is a demonstration:

import akka.actor.ActorSystem
import com.actor.DeveloperActor.NewFeature

object Demo {
  def main(args: Array[String]): Unit = {

    val system = ActorSystem.create("outsource-company")

    val tester = system.actorOf(TesterActor.props, "Sam")
    val developer = system.actorOf(DeveloperActor.props(tester), "Bob")

    developer ! NewFeature("Social integration")

    system.terminate()

  }
}

There are two possible results for the code snippet above:

...[akka://outsource-company/user/Bob] Working on feature 'Social integration'
...[akka://outsource-company/user/Sam] Testing 'Social integration'...
...[akka://outsource-company/user/Sam] A bug was found
...[akka://outsource-company/user/Bob] Fixing a bug
...[akka://outsource-company/user/Sam] Verifying the fix...

and

...[akka://outsource-company/user/Bob] Working on feature 'Social integration'
...[akka://outsource-company/user/Sam] Testing 'Social integration'...

That’s how we can use constructors for passing some values in the Actors. Also, it’s very important to know how to send messages directly from Actors and how to send responses to them.

Use this tool to look at the contents of GitHub and classify code based on the programming language used.  Content provided by IBM Developer.

Topics:
akka

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}