alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

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

This example Akka source code file (ConsistentHashingRouterDocSpec.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 akka.io; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

akka, consistenthashable, consistenthashableenvelope, consistenthashingrouterdocspec, entry, evict, get, hello, hi, implicitsender, route, routing, string, test, testing, testkit

The ConsistentHashingRouterDocSpec.scala Akka example source code

/**
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */
package docs.routing

import akka.testkit.AkkaSpec
import akka.testkit.ImplicitSender
import akka.routing.FromConfig
import akka.actor.ActorRef

object ConsistentHashingRouterDocSpec {

  //#cache-actor
  import akka.actor.Actor
  import akka.routing.ConsistentHashingRouter.ConsistentHashable

  class Cache extends Actor {
    var cache = Map.empty[String, String]

    def receive = {
      case Entry(key, value) => cache += (key -> value)
      case Get(key)          => sender() ! cache.get(key)
      case Evict(key)        => cache -= key
    }
  }

  final case class Evict(key: String)

  final case class Get(key: String) extends ConsistentHashable {
    override def consistentHashKey: Any = key
  }

  final case class Entry(key: String, value: String)
  //#cache-actor

}

class ConsistentHashingRouterDocSpec extends AkkaSpec with ImplicitSender {

  import ConsistentHashingRouterDocSpec._

  "demonstrate usage of ConsistentHashableRouter" in {

    def context = system

    //#consistent-hashing-router
    import akka.actor.Props
    import akka.routing.ConsistentHashingPool
    import akka.routing.ConsistentHashingRouter.ConsistentHashMapping
    import akka.routing.ConsistentHashingRouter.ConsistentHashableEnvelope

    def hashMapping: ConsistentHashMapping = {
      case Evict(key) => key
    }

    val cache: ActorRef =
      context.actorOf(ConsistentHashingPool(10, hashMapping = hashMapping).
        props(Props[Cache]), name = "cache")

    cache ! ConsistentHashableEnvelope(
      message = Entry("hello", "HELLO"), hashKey = "hello")
    cache ! ConsistentHashableEnvelope(
      message = Entry("hi", "HI"), hashKey = "hi")

    cache ! Get("hello")
    expectMsg(Some("HELLO"))

    cache ! Get("hi")
    expectMsg(Some("HI"))

    cache ! Evict("hi")
    cache ! Get("hi")
    expectMsg(None)

    //#consistent-hashing-router

  }

}

Other Akka source code examples

Here is a short list of links related to this Akka ConsistentHashingRouterDocSpec.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, alvinalexander.com
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.