diff --git a/akka-docs/java/remoting.rst b/akka-docs/java/remoting.rst index b32063fa07..6e1e41e663 100644 --- a/akka-docs/java/remoting.rst +++ b/akka-docs/java/remoting.rst @@ -115,3 +115,108 @@ This is also done via configuration:: This configuration setting will clone the actor “aggregation” 10 times and deploy it evenly distributed across the two given target nodes. + +Description of the Remoting Sample +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The sample application included with the Akka sources demonstrates both, remote +deployment and look-up of remote actors. First, let us have a look at the +common setup for both scenarios (this is ``common.conf``): + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/resources/common.conf + +This enables the remoting by installing the :class:`RemoteActorRefProvider` and +chooses the default remote transport. All other options will be set +specifically for each show case. + +.. note:: + + Be sure to replace the default IP 127.0.0.1 with the real address the system + is reachable by if you deploy onto multiple machines! + +.. _remote-lookup-sample-java: + +Remote Lookup +------------- + +In order to look up a remote actor, that one must be created first. For this +purpose, we configure an actor system to listen on port 2552 (this is a snippet +from ``application.conf``): + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/resources/application.conf + :include: calculator + +Then the actor must be created. For all code which follows, assume these imports: + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApplication.java + :include: imports + +The actor doing the work will be this one: + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JSimpleCalculatorActor.java + :include: actor + +and we start it within an actor system using the above configuration + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCalculatorApplication.java + :include: setup + +With the service actor up and running, we may look it up from another actor +system, which will be configured to use port 2553 (this is a snippet from +``application.conf``). + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/resources/application.conf + :include: remotelookup + +The actor which will query the calculator is a quite simple one for demonstration purposes + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupActor.java + :include: actor + +and it is created from an actor system using the aforementioned client’s config. + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApplication.java + :include: setup + +Requests which come in via ``doSomething`` will be sent to the client actor +along with the reference which was looked up earlier. Observe how the actor +system name using in ``actorFor`` matches the remote system’s name, as do IP +and port number. Top-level actors are always created below the ``"/user"`` +guardian, which supervises them. + +Remote Deployment +----------------- + +Creating remote actors instead of looking them up is not visible in the source +code, only in the configuration file. This section is used in this scenario +(this is a snippet from ``application.conf``): + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/resources/application.conf + :include: remotecreation + +For all code which follows, assume these imports: + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApplication.java + :include: imports + +The server actor can multiply or divide numbers: + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JAdvancedCalculatorActor.java + :include: actor + +The client actor looks like in the previous example + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationActor.java + :include: actor + +but the setup uses only ``actorOf``: + +.. includecode:: ../../akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationApplication.java + :include: setup + +Observe how the name of the server actor matches the deployment given in the +configuration file, which will transparently delegate the actor creation to the +remote node. + + + diff --git a/akka-docs/java/untyped-actors.rst b/akka-docs/java/untyped-actors.rst index ddd142ddbd..247bd15fdf 100644 --- a/akka-docs/java/untyped-actors.rst +++ b/akka-docs/java/untyped-actors.rst @@ -261,9 +261,7 @@ Remote actor addresses may also be looked up, if remoting is enabled:: These look-ups return a (possibly remote) actor reference immediately, so you will have to send to it and await a reply in order to verify that ``serviceB`` is actually reachable and running. An example demonstrating actor look-up is -given in :ref:`remote-lookup-sample` (Scala only for the time being). - -.. FIXME make Java sample and link to it +given in :ref:`remote-lookup-sample-java`. Messages and immutability ========================= diff --git a/akka-docs/scala/actors.rst b/akka-docs/scala/actors.rst index 8393b7c2b8..0bf5043030 100644 --- a/akka-docs/scala/actors.rst +++ b/akka-docs/scala/actors.rst @@ -293,7 +293,7 @@ Remote actor addresses may also be looked up, if remoting is enabled:: These look-ups return a (possibly remote) actor reference immediately, so you will have to send to it and await a reply in order to verify that ``serviceB`` is actually reachable and running. An example demonstrating actor look-up is -given in :ref:`remote-lookup-sample`. +given in :ref:`remote-lookup-sample-scala`. Messages and immutability ========================= diff --git a/akka-docs/scala/remoting.rst b/akka-docs/scala/remoting.rst index 9f9e8594f7..d0b613b190 100644 --- a/akka-docs/scala/remoting.rst +++ b/akka-docs/scala/remoting.rst @@ -158,7 +158,7 @@ specifically for each show case. Be sure to replace the default IP 127.0.0.1 with the real address the system is reachable by if you deploy onto multiple machines! -.. _remote-lookup-sample: +.. _remote-lookup-sample-scala: Remote Lookup ------------- diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JAdvancedCalculatorActor.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JAdvancedCalculatorActor.java index 36da1486c6..fa41b94aab 100644 --- a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JAdvancedCalculatorActor.java +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JAdvancedCalculatorActor.java @@ -5,13 +5,16 @@ package sample.remote.calculator.java; import akka.actor.UntypedActor; +//#actor public class JAdvancedCalculatorActor extends UntypedActor { @Override public void onReceive(Object message) throws Exception { + if (message instanceof Op.Multiply) { Op.Multiply multiply = (Op.Multiply) message; System.out.println("Calculating " + multiply.getN1() + " * " + multiply.getN2()); getSender().tell(new Op.MultiplicationResult(multiply.getN1(), multiply.getN2(), multiply.getN1() * multiply.getN2())); + } else if (message instanceof Op.Divide) { Op.Divide divide = (Op.Divide) message; System.out.println("Calculating " + divide.getN1() + " / " + divide.getN2()); @@ -19,3 +22,4 @@ public class JAdvancedCalculatorActor extends UntypedActor { } } } +//#actor diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCalculatorApplication.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCalculatorApplication.java index 5144c90e0f..9887e01511 100644 --- a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCalculatorApplication.java +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCalculatorApplication.java @@ -9,6 +9,7 @@ import akka.actor.Props; import akka.kernel.Bootable; import com.typesafe.config.ConfigFactory; +//#setup public class JCalculatorApplication implements Bootable { private ActorSystem system; @@ -26,3 +27,4 @@ public class JCalculatorApplication implements Bootable { system.shutdown(); } } +//#setup \ No newline at end of file diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationActor.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationActor.java index dc303e55ca..19a957e717 100644 --- a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationActor.java +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationActor.java @@ -8,19 +8,27 @@ import akka.actor.UntypedActor; import java.text.DecimalFormat; import java.text.NumberFormat; +//#actor public class JCreationActor extends UntypedActor { private static final NumberFormat formatter = new DecimalFormat("#0.00"); @Override public void onReceive(Object message) throws Exception { + if (message instanceof InternalMsg.MathOpMsg) { + // forward math op to server actor InternalMsg.MathOpMsg msg = (InternalMsg.MathOpMsg) message; msg.getActor().tell(msg.getMathOp(), getSelf()); + } else if (message instanceof Op.MathResult) { + + // receive reply from server actor + if (message instanceof Op.MultiplicationResult) { Op.MultiplicationResult result = (Op.MultiplicationResult) message; System.out.println("Mul result: " + result.getN1() + " * " + result.getN2() + " = " + result.getResult()); + } else if (message instanceof Op.DivisionResult) { Op.DivisionResult result = (Op.DivisionResult) message; System.out.println("Div result: " + result.getN1() + " / " + @@ -29,3 +37,4 @@ public class JCreationActor extends UntypedActor { } } } +//#actor diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationApplication.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationApplication.java index 82d5ed373f..ca2e961a5b 100644 --- a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationApplication.java +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JCreationApplication.java @@ -9,6 +9,7 @@ import akka.actor.Props; import akka.kernel.Bootable; import com.typesafe.config.ConfigFactory; +//#setup public class JCreationApplication implements Bootable { private ActorSystem system; private ActorRef actor; @@ -33,3 +34,4 @@ public class JCreationApplication implements Bootable { system.shutdown(); } } +//#setup diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupActor.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupActor.java index 52a2f1d67c..2bd32c2483 100644 --- a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupActor.java +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupActor.java @@ -5,18 +5,27 @@ package sample.remote.calculator.java; import akka.actor.UntypedActor; +//#actor public class JLookupActor extends UntypedActor { @Override public void onReceive(Object message) throws Exception { + if (message instanceof InternalMsg.MathOpMsg) { + + // send message to server actor InternalMsg.MathOpMsg msg = (InternalMsg.MathOpMsg) message; msg.getActor().tell(msg.getMathOp(), getSelf()); + } else if (message instanceof Op.MathResult) { + + // receive reply from server actor + if (message instanceof Op.AddResult) { Op.AddResult result = (Op.AddResult) message; System.out.println("Add result: " + result.getN1() + " + " + result.getN2() + " = " + result.getResult()); + } else if (message instanceof Op.SubtractResult) { Op.SubtractResult result = (Op.SubtractResult) message; System.out.println("Sub result: " + result.getN1() + " - " + @@ -25,3 +34,4 @@ public class JLookupActor extends UntypedActor { } } } +//#actor diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApplication.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApplication.java index f70fda5baa..6baf07a49a 100644 --- a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApplication.java +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JLookupApplication.java @@ -3,12 +3,16 @@ */ package sample.remote.calculator.java; +//#imports import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; +import akka.actor.UntypedActor; import akka.kernel.Bootable; import com.typesafe.config.ConfigFactory; +//#imports +//#setup public class JLookupApplication implements Bootable { private ActorSystem system; private ActorRef actor; @@ -33,3 +37,4 @@ public class JLookupApplication implements Bootable { system.shutdown(); } } +//#setup diff --git a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JSimpleCalculatorActor.java b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JSimpleCalculatorActor.java index 738e4c7ae5..927da641f2 100644 --- a/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JSimpleCalculatorActor.java +++ b/akka-samples/akka-sample-remote/src/main/java/sample/remote/calculator/java/JSimpleCalculatorActor.java @@ -5,13 +5,16 @@ package sample.remote.calculator.java; import akka.actor.UntypedActor; +//#actor public class JSimpleCalculatorActor extends UntypedActor { @Override public void onReceive(Object message) { + if (message instanceof Op.Add) { Op.Add add = (Op.Add) message; System.out.println("Calculating " + add.getN1() + " + " + add.getN2()); getSender().tell(new Op.AddResult(add.getN1(), add.getN2(), add.getN1() + add.getN2())); + } else if (message instanceof Op.Subtract) { Op.Subtract subtract = (Op.Subtract) message; System.out.println("Calculating " + subtract.getN1() + " - " + subtract.getN2()); @@ -19,3 +22,4 @@ public class JSimpleCalculatorActor extends UntypedActor { } } } +//#actor \ No newline at end of file