All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.newicom.dddd.writefront.GlobalOfficeClientSupport.scala Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
package pl.newicom.dddd.writefront

import akka.actor._
import akka.cluster.client.{ClusterClient, ClusterClientSettings}
import pl.newicom.dddd.office.OfficeId

import scala.collection.mutable

trait GlobalOfficeClientSupport {
  this: Actor =>

  def contactPoints: Seq[String]

  private lazy val officeClientMap: mutable.Map[String, ActorRef] = mutable.Map.empty

  private lazy val clusterClient: ActorRef = {
    val initialContacts: Seq[ActorPath] = contactPoints.map {
      case AddressFromURIString(address) ⇒ RootActorPath(address) / "system" / "receptionist"
    }
    system.actorOf(ClusterClient.props(ClusterClientSettings(system).withInitialContacts(initialContacts.toSet)), "clusterClient")
  }

  def officeActor(officeId: OfficeId): ActorRef =
    officeClientMap.getOrElseUpdate(officeId.id, system.actorOf(Props(new OfficeClient(officeId))))

  class OfficeClient(officeId: OfficeId) extends Actor {
    override def receive: Receive = {
      case msg => clusterClient forward ClusterClient.Send(s"/system/sharding/${officeId.id}", msg, localAffinity = true)
    }
  }

  private def system = context.system
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy