Merge pull request #236 from jboner/wip-1702-settings-ext-patriknw
DOC: Extension sample for application specific settings. See #1702
This commit is contained in:
commit
45d178c276
11 changed files with 350 additions and 119 deletions
|
|
@ -3,16 +3,13 @@
|
|||
*/
|
||||
package akka.docs.extension
|
||||
|
||||
import org.scalatest.WordSpec
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
|
||||
//#imports
|
||||
import akka.actor._
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
|
||||
//#imports
|
||||
import akka.actor.Actor
|
||||
import akka.testkit.AkkaSpec
|
||||
|
||||
//#extension
|
||||
import akka.actor.Extension
|
||||
|
||||
class CountExtensionImpl extends Extension {
|
||||
//Since this Extension is a shared instance
|
||||
// per ActorSystem we need to be threadsafe
|
||||
|
|
@ -24,6 +21,10 @@ class CountExtensionImpl extends Extension {
|
|||
//#extension
|
||||
|
||||
//#extensionid
|
||||
import akka.actor.ExtensionId
|
||||
import akka.actor.ExtensionIdProvider
|
||||
import akka.actor.ActorSystemImpl
|
||||
|
||||
object CountExtension
|
||||
extends ExtensionId[CountExtensionImpl]
|
||||
with ExtensionIdProvider {
|
||||
|
|
@ -39,39 +40,37 @@ object CountExtension
|
|||
}
|
||||
//#extensionid
|
||||
|
||||
//#extension-usage-actor
|
||||
import akka.actor.Actor
|
||||
object ExtensionDocSpec {
|
||||
//#extension-usage-actor
|
||||
|
||||
class MyActor extends Actor {
|
||||
def receive = {
|
||||
case someMessage ⇒
|
||||
CountExtension(context.system).increment()
|
||||
}
|
||||
}
|
||||
//#extension-usage-actor
|
||||
|
||||
//#extension-usage-actor-trait
|
||||
import akka.actor.Actor
|
||||
|
||||
trait Counting { self: Actor ⇒
|
||||
def increment() = CountExtension(context.system).increment()
|
||||
}
|
||||
class MyCounterActor extends Actor with Counting {
|
||||
def receive = {
|
||||
case someMessage ⇒ increment()
|
||||
}
|
||||
}
|
||||
//#extension-usage-actor-trait
|
||||
|
||||
class ExtensionDocSpec extends WordSpec with MustMatchers {
|
||||
|
||||
"demonstrate how to create an extension in Scala" in {
|
||||
val system: ActorSystem = null
|
||||
intercept[Exception] {
|
||||
//#extension-usage
|
||||
CountExtension(system).increment
|
||||
//#extension-usage
|
||||
class MyActor extends Actor {
|
||||
def receive = {
|
||||
case someMessage ⇒
|
||||
CountExtension(context.system).increment()
|
||||
}
|
||||
}
|
||||
//#extension-usage-actor
|
||||
|
||||
//#extension-usage-actor-trait
|
||||
|
||||
trait Counting { self: Actor ⇒
|
||||
def increment() = CountExtension(context.system).increment()
|
||||
}
|
||||
class MyCounterActor extends Actor with Counting {
|
||||
def receive = {
|
||||
case someMessage ⇒ increment()
|
||||
}
|
||||
}
|
||||
//#extension-usage-actor-trait
|
||||
}
|
||||
|
||||
class ExtensionDocSpec extends AkkaSpec {
|
||||
import ExtensionDocSpec._
|
||||
|
||||
"demonstrate how to create an extension in Scala" in {
|
||||
//#extension-usage
|
||||
CountExtension(system).increment
|
||||
//#extension-usage
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,78 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
package akka.docs.extension
|
||||
|
||||
//#imports
|
||||
import akka.actor.Extension
|
||||
import akka.actor.ExtensionId
|
||||
import akka.actor.ExtensionIdProvider
|
||||
import akka.actor.ActorSystemImpl
|
||||
import akka.util.Duration
|
||||
import com.typesafe.config.Config
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
//#imports
|
||||
|
||||
import akka.actor.Actor
|
||||
import akka.testkit.AkkaSpec
|
||||
|
||||
//#extension
|
||||
class SettingsImpl(config: Config) extends Extension {
|
||||
val DbUri: String = config.getString("myapp.db.uri")
|
||||
val CircuitBreakerTimeout: Duration = Duration(config.getMilliseconds("myapp.circuit-breaker.timeout"), TimeUnit.MILLISECONDS)
|
||||
}
|
||||
//#extension
|
||||
|
||||
//#extensionid
|
||||
object Settings extends ExtensionId[SettingsImpl] with ExtensionIdProvider {
|
||||
|
||||
override def lookup = Settings
|
||||
|
||||
override def createExtension(system: ActorSystemImpl) = new SettingsImpl(system.settings.config)
|
||||
}
|
||||
//#extensionid
|
||||
|
||||
object SettingsExtensionDocSpec {
|
||||
|
||||
val config = """
|
||||
//#config
|
||||
myapp {
|
||||
db {
|
||||
uri = "mongodb://example1.com:27017,example2.com:27017"
|
||||
}
|
||||
circuit-breaker {
|
||||
timeout = 30 seconds
|
||||
}
|
||||
}
|
||||
//#config
|
||||
"""
|
||||
|
||||
//#extension-usage-actor
|
||||
|
||||
class MyActor extends Actor {
|
||||
val settings = Settings(context.system)
|
||||
val connection = connect(settings.DbUri, settings.CircuitBreakerTimeout)
|
||||
|
||||
//#extension-usage-actor
|
||||
def receive = {
|
||||
case someMessage ⇒
|
||||
}
|
||||
|
||||
def connect(dbUri: String, circuitBreakerTimeout: Duration) = {
|
||||
"dummy"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class SettingsExtensionDocSpec extends AkkaSpec(SettingsExtensionDocSpec.config) {
|
||||
|
||||
"demonstrate how to create application specific settings extension in Scala" in {
|
||||
//#extension-usage
|
||||
val dbUri = Settings(system).DbUri
|
||||
val circuitBreakerTimeout = Settings(system).CircuitBreakerTimeout
|
||||
//#extension-usage
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
.. _extending-akka-scala:
|
||||
|
||||
Akka Extensions
|
||||
===============
|
||||
#########################
|
||||
Akka Extensions (Scala)
|
||||
#########################
|
||||
|
||||
|
||||
.. sidebar:: Contents
|
||||
|
||||
|
|
@ -9,19 +11,19 @@ Akka Extensions
|
|||
|
||||
|
||||
Building an Extension
|
||||
---------------------
|
||||
=====================
|
||||
|
||||
So let's create a sample extension that just lets us count the number of times something has happened.
|
||||
|
||||
First, we define what our ``Extension`` should do:
|
||||
|
||||
.. includecode:: code/akka/docs/extension/ExtensionDocSpec.scala
|
||||
:include: imports,extension
|
||||
:include: extension
|
||||
|
||||
Then we need to create an ``ExtensionId`` for our extension so we can grab ahold of it.
|
||||
|
||||
.. includecode:: code/akka/docs/extension/ExtensionDocSpec.scala
|
||||
:include: imports,extensionid
|
||||
:include: extensionid
|
||||
|
||||
Wicked! Now all we need to do is to actually use it:
|
||||
|
||||
|
|
@ -41,13 +43,37 @@ You can also hide extension behind traits:
|
|||
That's all there is to it!
|
||||
|
||||
Loading from Configuration
|
||||
--------------------------
|
||||
==========================
|
||||
|
||||
To be able to load extensions from your Akka configuration you must add FQCNs of implementations of either ``ExtensionId`` or ``ExtensionIdProvider``
|
||||
in the "akka.extensions" section of the config you provide to your ``ActorSystem``.
|
||||
in the ``akka.extensions`` section of the config you provide to your ``ActorSystem``.
|
||||
|
||||
Applicability
|
||||
-------------
|
||||
=============
|
||||
|
||||
The sky is the limit!
|
||||
By the way, did you know that Akka's ``Typed Actors``, ``Serialization`` and other features are implemented as Akka Extensions?
|
||||
|
||||
.. _extending-akka-scala.settings:
|
||||
|
||||
Application specific settings
|
||||
-----------------------------
|
||||
|
||||
The :ref:`configuration` can be used for application specific settings. A good practice is to place those settings in an Extension.
|
||||
|
||||
Sample configuration:
|
||||
|
||||
.. includecode:: code/akka/docs/extension/SettingsExtensionDocSpec.scala
|
||||
:include: config
|
||||
|
||||
The ``Extension``:
|
||||
|
||||
.. includecode:: code/akka/docs/extension/SettingsExtensionDocSpec.scala
|
||||
:include: imports,extension,extensionid
|
||||
|
||||
|
||||
Use it:
|
||||
|
||||
.. includecode:: code/akka/docs/extension/SettingsExtensionDocSpec.scala
|
||||
:include: extension-usage-actor
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
.. _akka-testkit:
|
||||
|
||||
#####################
|
||||
Testing Actor Systems
|
||||
#####################
|
||||
##############################
|
||||
Testing Actor Systems (Scala)
|
||||
##############################
|
||||
|
||||
.. toctree::
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
.. _testkit-example:
|
||||
|
||||
###############
|
||||
TestKit Example
|
||||
###############
|
||||
########################
|
||||
TestKit Example (Scala)
|
||||
########################
|
||||
|
||||
Ray Roestenburg's example code from `his blog <http://roestenburg.agilesquad.com/2011/02/unit-testing-akka-actors-with-testkit_12.html>`_ adapted to work with Akka 1.1.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue