2011-12-19 11:07:59 +01:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-12-19 11:07:59 +01:00
|
|
|
*/
|
2012-05-22 11:37:09 +02:00
|
|
|
package docs.extension
|
2011-12-15 01:27:59 +01:00
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicLong
|
2012-01-20 11:30:33 +01:00
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.testkit.AkkaSpec
|
2011-12-15 01:27:59 +01:00
|
|
|
|
|
|
|
|
//#extension
|
2012-01-20 11:30:33 +01:00
|
|
|
import akka.actor.Extension
|
|
|
|
|
|
2011-12-15 01:27:59 +01:00
|
|
|
class CountExtensionImpl extends Extension {
|
|
|
|
|
//Since this Extension is a shared instance
|
|
|
|
|
// per ActorSystem we need to be threadsafe
|
|
|
|
|
private val counter = new AtomicLong(0)
|
|
|
|
|
|
|
|
|
|
//This is the operation this Extension provides
|
|
|
|
|
def increment() = counter.incrementAndGet()
|
|
|
|
|
}
|
|
|
|
|
//#extension
|
|
|
|
|
|
|
|
|
|
//#extensionid
|
2012-01-20 11:30:33 +01:00
|
|
|
import akka.actor.ExtensionId
|
|
|
|
|
import akka.actor.ExtensionIdProvider
|
2012-01-24 11:33:40 +01:00
|
|
|
import akka.actor.ExtendedActorSystem
|
2012-01-20 11:30:33 +01:00
|
|
|
|
2011-12-15 01:27:59 +01:00
|
|
|
object CountExtension
|
|
|
|
|
extends ExtensionId[CountExtensionImpl]
|
|
|
|
|
with ExtensionIdProvider {
|
|
|
|
|
//The lookup method is required by ExtensionIdProvider,
|
|
|
|
|
// so we return ourselves here, this allows us
|
|
|
|
|
// to configure our extension to be loaded when
|
|
|
|
|
// the ActorSystem starts up
|
|
|
|
|
override def lookup = CountExtension
|
|
|
|
|
|
|
|
|
|
//This method will be called by Akka
|
|
|
|
|
// to instantiate our Extension
|
2012-01-24 11:33:40 +01:00
|
|
|
override def createExtension(system: ExtendedActorSystem) = new CountExtensionImpl
|
2011-12-15 01:27:59 +01:00
|
|
|
}
|
|
|
|
|
//#extensionid
|
|
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
object ExtensionDocSpec {
|
2012-01-30 16:47:33 +01:00
|
|
|
|
|
|
|
|
val config = """
|
|
|
|
|
//#config
|
|
|
|
|
akka {
|
2012-05-22 11:37:09 +02:00
|
|
|
extensions = ["docs.extension.CountExtension$"]
|
2012-01-30 16:47:33 +01:00
|
|
|
}
|
|
|
|
|
//#config
|
|
|
|
|
"""
|
|
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
//#extension-usage-actor
|
2011-12-15 01:27:59 +01:00
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
class MyActor extends Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case someMessage ⇒
|
|
|
|
|
CountExtension(context.system).increment()
|
|
|
|
|
}
|
2011-12-15 01:27:59 +01:00
|
|
|
}
|
2012-01-20 11:30:33 +01:00
|
|
|
//#extension-usage-actor
|
2011-12-15 01:27:59 +01:00
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
//#extension-usage-actor-trait
|
2011-12-15 01:27:59 +01:00
|
|
|
|
2012-01-20 11:30:33 +01:00
|
|
|
trait Counting { self: Actor ⇒
|
|
|
|
|
def increment() = CountExtension(context.system).increment()
|
2011-12-15 01:27:59 +01:00
|
|
|
}
|
2012-01-20 11:30:33 +01:00
|
|
|
class MyCounterActor extends Actor with Counting {
|
|
|
|
|
def receive = {
|
|
|
|
|
case someMessage ⇒ increment()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#extension-usage-actor-trait
|
2011-12-15 01:27:59 +01:00
|
|
|
}
|
|
|
|
|
|
2012-01-30 16:47:33 +01:00
|
|
|
class ExtensionDocSpec extends AkkaSpec(ExtensionDocSpec.config) {
|
2012-01-20 11:30:33 +01:00
|
|
|
import ExtensionDocSpec._
|
2011-12-15 01:27:59 +01:00
|
|
|
|
|
|
|
|
"demonstrate how to create an extension in Scala" in {
|
2012-01-20 11:30:33 +01:00
|
|
|
//#extension-usage
|
|
|
|
|
CountExtension(system).increment
|
|
|
|
|
//#extension-usage
|
2011-12-15 01:27:59 +01:00
|
|
|
}
|
|
|
|
|
|
2012-01-30 16:47:33 +01:00
|
|
|
"demonstrate how to lookup a configured extension in Scala" in {
|
|
|
|
|
//#extension-lookup
|
|
|
|
|
system.extension(CountExtension)
|
|
|
|
|
//#extension-lookup
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-15 01:27:59 +01:00
|
|
|
}
|