diff --git a/akka-docs/dev/index.rst b/akka-docs/dev/index.rst index 8ea96ee473..17fcb2427c 100644 --- a/akka-docs/dev/index.rst +++ b/akka-docs/dev/index.rst @@ -5,7 +5,6 @@ Information for Developers :maxdepth: 2 building-akka - extending-akka multi-jvm-testing developer-guidelines documentation diff --git a/akka-docs/java/code/akka/docs/extension/ExtensionDocTest.scala b/akka-docs/java/code/akka/docs/extension/ExtensionDocTest.scala new file mode 100644 index 0000000000..4cd5014863 --- /dev/null +++ b/akka-docs/java/code/akka/docs/extension/ExtensionDocTest.scala @@ -0,0 +1,5 @@ +package akka.docs.extension + +import org.scalatest.junit.JUnitSuite + +class ExtensionDocTest extends ExtensionDocTestBase with JUnitSuite diff --git a/akka-docs/java/code/akka/docs/extension/ExtensionDocTestBase.java b/akka-docs/java/code/akka/docs/extension/ExtensionDocTestBase.java new file mode 100644 index 0000000000..2d8f914902 --- /dev/null +++ b/akka-docs/java/code/akka/docs/extension/ExtensionDocTestBase.java @@ -0,0 +1,70 @@ +package akka.docs.extension; + +//#imports +import akka.actor.*; +import java.util.concurrent.atomic.AtomicLong; + +//#imports + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +public class ExtensionDocTestBase { + + //#extension + public static class CountExtensionImpl implements Extension { + //Since this Extension is a shared instance + // per ActorSystem we need to be threadsafe + private final AtomicLong counter = new AtomicLong(0); + + //This is the operation this Extension provides + public long increment() { + return counter.incrementAndGet(); + } + } + //#extension + + //#extensionid + static class CountExtensionId extends AbstractExtensionId { + //This method will be called by Akka + // to instantiate our Extension + public CountExtensionImpl createExtension(ActorSystemImpl i) { + return new CountExtensionImpl(); + } + } + + //This will be the identifier of our CountExtension + public final static CountExtensionId CountExtension = new CountExtensionId(); + //#extensionid + + //#extensionid-provider + static class CountExtensionIdProvider implements ExtensionIdProvider { + public CountExtensionId lookup() { + return CountExtension; //The public static final + } + } + //#extensionid-provider + + //#extension-usage-actor + static class MyActor extends UntypedActor { + public void onReceive(Object msg) { + CountExtension.get(getContext().system()).increment(); + } + } + //#extension-usage-actor + + + @Test public void demonstrateHowToCreateAndUseAnAkkaExtensionInJava() { + final ActorSystem system = null; + try { + //#extension-usage + CountExtension.get(system).increment(); + //#extension-usage + } catch(Exception e) { + //do nothing + } + } + +} diff --git a/akka-docs/dev/extending-akka.rst b/akka-docs/java/extending-akka.rst similarity index 73% rename from akka-docs/dev/extending-akka.rst rename to akka-docs/java/extending-akka.rst index 8af8e43b7f..7cc46cc93d 100644 --- a/akka-docs/dev/extending-akka.rst +++ b/akka-docs/java/extending-akka.rst @@ -19,50 +19,42 @@ Details on how to make that happens are below, in the "Loading from Configuratio Since an extension is a way to hook into Akka itself, the implementor of the extension needs to ensure the thread safety of his/her extension. + +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/ExtensionDocTestBase.java + :include: imports,extension + +Then we need to create an ``ExtensionId`` for our extension so we can grab ahold of it. + +.. includecode:: code/akka/docs/extension/ExtensionDocTestBase.java + :include: imports,extensionid + +Wicked! Now all we need to do is to actually use it: + +.. includecode:: code/akka/docs/extension/ExtensionDocTestBase.java + :include: extension-usage + +Or from inside of an Akka Actor: + +.. includecode:: code/akka/docs/extension/ExtensionDocTestBase.java + :include: extension-usage-actor + +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``. -Building an Extension (Java) ----------------------------- - -So let's create a sample extension that just lets us count the number of times something has happened. - -FIXME - -Building an Extension (Scala) ------------------------------ - -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 - -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 - -Wicked! Now all we need to do is to actually use it: - -.. includecode:: code/akka/docs/extension/ExtensionDocSpec.scala - :include: extension-usage - -Or from inside of an Akka Actor: - -.. includecode:: code/akka/docs/extension/ExtensionDocSpec.scala - :include: extension-usage-actor - -You can also hide extension behind traits: - -.. includecode:: code/akka/docs/extension/ExtensionDocSpec.scala - :include: extension-usage-actor-trait - -That's all there is to it! +.. includecode:: code/akka/docs/extension/ExtensionDocTestBase.java + :include: extensionid-provider Applicability ------------- diff --git a/akka-docs/java/index.rst b/akka-docs/java/index.rst index c04e5bc259..05273b84b5 100644 --- a/akka-docs/java/index.rst +++ b/akka-docs/java/index.rst @@ -17,3 +17,4 @@ Java API dispatchers routing guice-integration + extending-akka diff --git a/akka-docs/dev/code/akka/docs/extension/ExtensionDocSpec.scala b/akka-docs/scala/code/akka/docs/extension/ExtensionDocSpec.scala similarity index 100% rename from akka-docs/dev/code/akka/docs/extension/ExtensionDocSpec.scala rename to akka-docs/scala/code/akka/docs/extension/ExtensionDocSpec.scala diff --git a/akka-docs/scala/extending-akka.rst b/akka-docs/scala/extending-akka.rst new file mode 100644 index 0000000000..a13326b626 --- /dev/null +++ b/akka-docs/scala/extending-akka.rst @@ -0,0 +1,53 @@ +.. _extending-akka: + +Akka Extensions +=============== + +.. sidebar:: Contents + + .. contents:: :local: + + +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 + +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 + +Wicked! Now all we need to do is to actually use it: + +.. includecode:: code/akka/docs/extension/ExtensionDocSpec.scala + :include: extension-usage + +Or from inside of an Akka Actor: + +.. includecode:: code/akka/docs/extension/ExtensionDocSpec.scala + :include: extension-usage-actor + +You can also hide extension behind traits: + +.. includecode:: code/akka/docs/extension/ExtensionDocSpec.scala + :include: extension-usage-actor-trait + +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``. + +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? \ No newline at end of file diff --git a/akka-docs/scala/index.rst b/akka-docs/scala/index.rst index a36fe9513f..5f08468e39 100644 --- a/akka-docs/scala/index.rst +++ b/akka-docs/scala/index.rst @@ -19,3 +19,4 @@ Scala API routing fsm testing + extending-akka