Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / ActorSelectionSample.scala
Created July 8, 2013 06:51
ActorSelection spotlight
val selection = context.actorSelection(
"akka.tcp://[email protected]:2552/user/world")
selection ! "hello"
@patriknw
patriknw / WatchActorSelectionSample.scala
Created July 8, 2013 06:55
Watch ActorSelection spotlight
val selection = context.actorSelection(
"akka.tcp://[email protected]:2552/user/world")
selection ! Identify(None)
var ref: ActorRef = _
def receive = {
case ActorIdentity(_, Some(actorRef)) =>
ref = actorRef
context watch ref
case ActorIdentity(_, None) => // not alive
@patriknw
patriknw / WatchSample.scala
Created July 9, 2013 06:03
Remote Watch Spotlight
case object Register
class Master extends Actor {
var workers = Set.empty[ActorRef]
def receive = {
case Register =>
workers += sender
context watch sender
case Terminated(ref) =>
@patriknw
patriknw / SimpleRouterApp.scala
Created July 27, 2013 07:26
Minimized sample of cluster router
package sample.cluster.simple
import scala.concurrent.duration._
import akka.actor._
import akka.cluster.Cluster
import akka.cluster.ClusterEvent._
import akka.routing.FromConfig
import com.typesafe.config.ConfigFactory
object SimpleRouterApp {
@patriknw
patriknw / ClientSubscriber.scala
Created August 19, 2013 17:39
Client side subscriber
object TopicAssistant {
case class Subscribe(topic: String)
case object SubscribeAck
}
// Start this actor with name "topicAssistant" on each node in the cluster
class TopicAssistant extends Actor {
import TopicAssistant._
ClusterReceptionistExtension(context.system).registerService(self)
@patriknw
patriknw / ResolveManyCollector.scala
Created August 23, 2013 09:09
Identify actors matching wildcard ActorSelection.
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.contrib.pattern
import akka.actor.ActorIdentity
import akka.actor.Props
import akka.actor.ActorSelection
import scala.concurrent.duration.Deadline
import akka.actor.ReceiveTimeout
@patriknw
patriknw / MyLoggingReceive.scala
Created December 20, 2013 12:33
Example of a custom LoggingReceive
import akka.actor.Actor.Receive
import akka.actor.ActorContext
import akka.actor.ActorLogging
import akka.actor.Actor
import akka.event.LoggingAdapter
object MyLoggingReceive {
def apply(log: LoggingAdapter)(r: Receive)(implicit context: ActorContext): Receive = r match {
case _: MyLoggingReceive ⇒ r