Skip to content

Instantly share code, notes, and snippets.

@patriknw
patriknw / ActorSelectionSample.scala
Created July 8, 2013 06:51
ActorSelection spotlight
val selection = context.actorSelection(
"akka.tcp://sys@10.0.0.1:2552/user/world")
selection ! "hello"
@patriknw
patriknw / LoggingMailbox.scala
Last active January 5, 2023 08:12
Logs the mailbox size when exceeding the configured limit. Implemented in Scala and Java. Copy one of them to your project and define the configuration. This code is licensed under the Apache 2 license.
/**
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.contrib.mailbox
import scala.concurrent.duration._
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
import com.typesafe.config.Config
import akka.actor.{ ActorContext, ActorRef, ActorSystem, ExtendedActorSystem }
@patriknw
patriknw / application.conf
Created July 5, 2013 08:10
Spotlight min members of role
akka.cluster.role {
frontend.min-nr-of-members = 1
backend.min-nr-of-members = 2
}
@patriknw
patriknw / Client.scala
Created July 5, 2013 07:03
ClusterClient spotlight
// on the client
val initialContacts = Set(
system.actorSelection("akka.tcp://Other@host1:2552/user/receptionist"),
system.actorSelection("akka.tcp://Other@host2:2552/user/receptionist"))
val c = system.actorOf(ClusterClient.props(initialContacts))
c ! ClusterClient.Send("/user/serviceA", "hello", localAffinity = true)
@patriknw
patriknw / Publisher.scala
Created July 5, 2013 06:33
Pub/Sub Spotlight
class Publisher extends Actor {
import DistributedPubSubMediator.Publish
// activate the extension
val mediator = DistributedPubSubExtension(context.system).mediator
def receive = {
case in: String ⇒
val out = in.toUpperCase
mediator ! Publish("content", out)
}
@patriknw
patriknw / RegisterOnMemberUp.scala
Created July 4, 2013 08:13
Spotlight registerOnMemberUp
Cluster(system) registerOnMemberUp {
system.actorOf(Props(classOf[FactorialFrontend], upToN, true),
name = "factorialFrontend")
}
@patriknw
patriknw / MetricsMailbox.scala
Created June 15, 2013 03:39
Sample of mailbox extension that counts messages in queue
package akka.contrib.mailbox
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicInteger
import com.typesafe.config.Config
import akka.actor.{ ActorContext, ActorRef, ActorSystem, ExtendedActorSystem, Extension, ExtensionId, ExtensionIdProvider }
import akka.dispatch.{ Envelope, MailboxType, MessageQueue, UnboundedMailbox, UnboundedQueueBasedMessageQueue }
object MetricsMailboxExtension extends ExtensionId[MetricsMailboxExtension] with ExtensionIdProvider {
def lookup = this
@patriknw
patriknw / MemberAge.scala
Last active December 18, 2015 11:39
Code for 2.2 spotlight: Cluster Member Age
// sort by age, oldest first
val ageOrdering = Ordering.fromLessThan[Member] { (a, b) =>
a.isOlderThan(b)
}
var membersByAge: SortedSet[Member] = SortedSet.empty(ageOrdering)
def receive = {
case state: CurrentClusterState =>
membersByAge = SortedSet.empty(ageOrdering) ++ state.members
}
@patriknw
patriknw / Backend.scala
Created June 10, 2013 18:23
Code for 2.2. Spotlight: Cluster Node Roles
class TransformationBackend extends Actor {
val cluster = Cluster(context.system)
override def preStart(): Unit =
cluster.subscribe(self, classOf[MemberUp])
override def postStop(): Unit =
cluster.unsubscribe(self)
def receive = {
case state: CurrentClusterState ⇒
@patriknw
patriknw / ClusterRegistrySpec.scala
Last active May 28, 2019 10:58
Example of how to implement a simple cluster wide actor registry.
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.contrib.pattern
import language.postfixOps
import scala.concurrent.duration._
import com.typesafe.config.ConfigFactory
import akka.actor.Actor