Skip to content

Instantly share code, notes, and snippets.

package demo
class TopLevel extends Actor {
val db = context.actorOf(Props[Database])
context.watch(db)
val web = context.actorOf(Props[WebServer])
web ! Start(database = db)
override val supervisorStrategy = OneForOneStrategy() {
@rkuhn
rkuhn / theVar.scala
Created July 23, 2013 11:26
“unbecoming” blog post races
var isDownloading = false
// set up a scheduler to sync the registry
system.scheduler.schedule(0 millis, 3 hours) {
if(!isDownloading) {
system.actorOf(Props[Downloader]) ! Download
}
}
/*
@rkuhn
rkuhn / SupervisorLogging.java
Last active December 19, 2015 02:29
supervisorStrategy with adapted logging
private static SupervisorStrategy strategy =
new OneForOneStrategy(10, Duration.create("1 minute"),
new Function<Throwable, Directive>() {
@Override
public Directive apply(Throwable t) {
if (t instanceof ArithmeticException) {
return resume();
} else if (t instanceof NullPointerException) {
log.warning("restarting misbehaving child");
return restart();
@rkuhn
rkuhn / bad.java
Last active December 16, 2015 17:49
sample snippets for Props spotlight blogpost
final ActorRef child = getContext().actorOf(new Props(
new UntypedActorFactory() {
@Override public UntypedActor create() {
// getSender() will not yield what you think it should
return new OtherActor(getSender());
}
}))
@rkuhn
rkuhn / FullSample.scala
Last active December 24, 2018 17:27
Sample code for the blog post about Typed Channels in Akka
/**
* Copyright (C) 2013 Typesafe Inc. <http://www.typesafe.com>
*/
package docs.channels
import akka.actor.{ Actor, ActorSystem }
import akka.channels._
import akka.util.Timeout
import scala.concurrent.duration._
@rkuhn
rkuhn / Timer.scala
Created January 11, 2013 22:47
draft of new lockless Timer implementation for Akka
/**
* Copyright (C) 2013 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.util
import java.util.concurrent.atomic.AtomicReferenceArray
import scala.annotation.tailrec
import scala.concurrent.ExecutionContext
@rkuhn
rkuhn / Test.scala
Last active December 10, 2015 22:58
Makkros blog post
def eval(code: String, compileOptions: String =
"-cp akka-actor/target/classes:akka-macros/target/classes"): Any = {
val tb = mkToolbox(compileOptions)
tb.eval(tb.parse(code))
}
def mkToolbox(compileOptions: String = ""): ToolBox[_ <: Universe] = {
val m = scala.reflect.runtime.currentMirror
m.mkToolBox(options = compileOptions)
}
@rkuhn
rkuhn / count-repl-session.txt
Created December 9, 2012 20:13
too simple benchmark
Welcome to Scala version 2.10.0-RC5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_07).
Type in expressions to have them evaluated.
Type :help for more information.
scala> system=ActorSystem("repl",ConfigFactory.parseString("pinned{type=PinnedDispatcher;executor=thread-pool-executor}").withFallback(config))
[DEBUG] [12/09/2012 21:23:23.535] [run-main] [EventStream(akka://repl)] logger log1-Logging$DefaultLogger started
[DEBUG] [12/09/2012 21:23:23.535] [run-main] [EventStream(akka://repl)] Default Loggers started
system: akka.actor.ActorSystem = akka://repl
scala> case class Add(x: Int)
@rkuhn
rkuhn / continuation.scala
Created December 6, 2012 19:45
message-based continuation pattern
import akka.actor.ActorDSL._
case object Work
case object StopIt
implicit val system = akka.actor.ActorSystem()
actor(new Act {
case class ToDo(items: Int, client: ActorRef, v: Double)
become {
@rkuhn
rkuhn / javatestkit.java
Created November 7, 2012 08:24
JavaTestKit spotlight
new JavaTestKit(system) {{
final Props props = new Props(SomeActor.class);
final ActorRef subject = system.actorOf(props);
subject.tell("request", getRef());
expectMsgEquals(duration("1 second"), "response");
new Within(duration("3 seconds")) {
protected void run() {
subject.tell("hello", getRef());