O'Reilly logo

Akka Concurrency by Derek Wyatt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 15

Sharing Data with Agents

Actors hide their data in a secure fortress, ensuring that access from the outside world is controlled and safe. When you want to get a value from an actor, you send it a message, and the result comes back at some point later. For reactive-based programming, this is great, but when all you want to do is modify or access some sort of data that is effectively shared, it can be a little less than optimal. Agents are designed for this sort of situation.

Agents invert a concept that you normally would write on your own. For example, if you want an actor to represent some sort of counter, you might write this:

  object CounterActor {
    case class AlterBy(value: Int)
    case object GetValue
  }
  class CounterActor extends ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required