| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (HakkerTracker.scala)

This example Akka source code file (HakkerTracker.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

actor, akka, domainevent, eventsourcedprocessor, hakkerstatechange, hakkertracker, persistence, receive, startedeating, state, stoppedeating, string, trackhakker

The HakkerTracker.scala Akka example source code

package akka.sample.osgi.internal

import akka.persistence.EventsourcedProcessor
import akka.sample.osgi.api.HakkerStateChange
import akka.sample.osgi.api.SubscribeToHakkerStateChanges
import akka.sample.osgi.api.EatingCount
import akka.sample.osgi.api.GetEatingCount
import akka.sample.osgi.api.TrackHakker

object HakkerTracker {
  sealed trait DomainEvent
  final case class StartedEating(name: String) extends DomainEvent
  final case class StoppedEating(name: String) extends DomainEvent

  object State {
    val empty: State = new State(Map.empty)
  final case class State private (eatingCounts: Map[String, Int]) {
    def updated(event: DomainEvent): State = event match {
      case StartedEating(name) =>
        val c = eatingCounts.getOrElse(name, 0) + 1
        copy(eatingCounts = eatingCounts + (name -> c))
      case StoppedEating(name) =>

class HakkerTracker extends EventsourcedProcessor {
  import HakkerTracker._

  var state = State.empty

  override def receiveRecover: Receive = {
    case evt: DomainEvent =>
      state = state.updated(evt)

  override def receiveCommand: Receive = {
    case TrackHakker(hakker) =>
      hakker ! SubscribeToHakkerStateChanges

    case HakkerStateChange(name, _, "eating") =>
      persist(StartedEating(name)) { evt =>
        state = state.updated(evt)

    case HakkerStateChange(name, "eating", _) =>
      persist(StoppedEating(name)) { evt =>
        state = state.updated(evt)

    case GetEatingCount(name) =>
      sender() ! EatingCount(name, 17)


Other Akka source code examples

Here is a short list of links related to this Akka HakkerTracker.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.