Skip to content

Instantly share code, notes, and snippets.

import akka.actor.{ActorSystem, ActorRef, Props, Actor}
import com.typesafe.config.{Config, ConfigFactory}
class Echo extends Actor {
def receive = {
case m =>
println("%s: %s".format(m, sender.path))
sender ! m
}
}
@derekwyatt
derekwyatt / typeresolve.scala
Created February 9, 2012 14:14
type resolution ignorance in scala
trait SomeTrait {
case class X(s: String)
def getThings: Set[X]
}
object SomeTraitStub {
var things = Set.empty[SomeTrait#X]
}
trait SomeTraitStub extends SomeTrait {
@derekwyatt
derekwyatt / PartialShapeless.scala
Created February 16, 2012 19:49
Attempt to simplify function composition from multiple partials using shapeless...
import shapeless._
import HList._
import Record._
object Shapeless {
type MyFunction = PartialFunction[Any, String]
object behaviourA extends Field[MyFunction] { override def toString = "BehaviourA" }
object behaviourB extends Field[MyFunction] { override def toString = "BehaviourB" }
@derekwyatt
derekwyatt / ReceiveComp.scala
Created February 17, 2012 19:27
An experiment in receive method composition for Akka
trait ReceiveComposiingActor extends Actor {
trait Key
lazy val receivePartials = scala.collection.mutable.Map.empty[Key, Receive]
// Fronts 'context.become' to alter map before recomposition
def becomeNew(key: Key, behaviour: Receive) {
receivePartials += (key -> behaviour)
context.become(composeReceive)
}
@derekwyatt
derekwyatt / dataflowstuff.scala
Created February 27, 2012 16:50
Combining Dataflow with Futures and Actors
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.dispatch._
import akka.dispatch.Future.flow
import akka.pattern.ask
import akka.util.duration._
import akka.util.Timeout
import akka.util.Timeout._
case object Ping
@derekwyatt
derekwyatt / actorcontext_helpers.scala
Created March 15, 2012 13:27
Akka 2.0 ActorContext actorFor Helpers
def tryActorFor(path: String)(implicit ctx: ActorContext): Option[ActorRef] = ctx.actorFor(path) match {
case a if a.isTerminated => None
case a => Some(a)
}
def tryAncestorActorFor(name: String)(implicit ctx: ActorContext, me: ActorRef): Option[ActorRef] = {
def helper(revPath: Seq[String]): Option[ActorRef] = revPath match {
case Seq() => None
case Seq(`name`, _*) => Some(ctx.actorFor("/" + revPath.reverse.mkString("/")))
case _ => helper(revPath.tail)
}
@derekwyatt
derekwyatt / syncstop.scala
Created March 15, 2012 16:41
Synchronous stop of an Actor for testing of Akka 2.0 stuff
trait SyncStopper { this: TestKit =>
def syncActorStop(name: String) {
val a = system.actorFor(name)
watch(a)
system.stop(a)
expectMsg(akka.actor.Terminated(a))
unwatch(a)
}
}
@derekwyatt
derekwyatt / ACoupleOfUseExamples.scala
Created July 18, 2012 20:51
Scalatest Fixtures and Akka - parallel fixture isolation - sequential fixture isolation - and no fixture
// Examples showing how to use the specification helpers
//
import akka.actor.ActorSystem
import akka.testkit.{TestKit, ImplicitSender}
import org.scalatest.{WordSpec, BeforeAndAfterAll}
import org.scalatest.matchers.MustMatchers
// Sequentially runs the tests, isolating the Fixture in each test. The Fixture carries the TestKit
// so each test gets its own ActorSystem
class MyAkkaTestSpec extends SequentialAkkaSpecWithIsolatedFixture {
@derekwyatt
derekwyatt / TransAgent.scala
Created July 31, 2012 13:37
Transactional Agent oddness
import akka.actor.ActorSystem
import akka.util.duration._
import akka.util.Timeout
import akka.agent.Agent
import scala.concurrent.stm._
object Main {
implicit val sys = ActorSystem()
implicit val awaitTimeout = Timeout(5 seconds)
@derekwyatt
derekwyatt / MasterProtocol.scala
Created August 7, 2012 18:34
Message protocol between the Master and its Workers in the Master / Worker remote node dispatch pattern
object MasterWorkerProtocol {
// Messages from Workers
case class WorkerCreated(worker: ActorRef)
case class WorkerRequestsWork(worker: ActorRef)
case class WorkIsDone(worker: ActorRef)
// Messages to Workers
case class WorkToBeDone(work: Any)
case object WorkIsReady
case object NoWorkToBeDone