add akka.Main and use that to add Hello World docs
- akka.Main will start an ActorSystem and one actor; when that actor terminates the system is shut down - HelloWorld sample with two actors
This commit is contained in:
parent
a4b485968c
commit
651f699893
11 changed files with 254 additions and 2 deletions
26
akka-docs/rst/java/code/docs/actor/japi/Greeter.java
Normal file
26
akka-docs/rst/java/code/docs/actor/japi/Greeter.java
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.actor.japi;
|
||||
|
||||
import akka.actor.UntypedActor;
|
||||
import java.io.Serializable;
|
||||
|
||||
//#greeter
|
||||
public class Greeter extends UntypedActor {
|
||||
|
||||
public static enum Msg {
|
||||
GREET, DONE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Object msg) {
|
||||
if (msg == Msg.GREET) {
|
||||
System.out.println("Hello World!");
|
||||
getSender().tell(Msg.DONE, getSelf());
|
||||
} else unhandled(msg);
|
||||
}
|
||||
|
||||
}
|
||||
//#greeter
|
||||
31
akka-docs/rst/java/code/docs/actor/japi/HelloWorld.java
Normal file
31
akka-docs/rst/java/code/docs/actor/japi/HelloWorld.java
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.actor.japi;
|
||||
|
||||
//#hello-world
|
||||
import akka.actor.Props;
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.actor.ActorRef;
|
||||
|
||||
public class HelloWorld extends UntypedActor {
|
||||
|
||||
@Override
|
||||
public void preStart() {
|
||||
// create the greeter actor
|
||||
final ActorRef greeter =
|
||||
getContext().actorOf(Props.create(Greeter.class), "greeter");
|
||||
// tell it to perform the greeting
|
||||
greeter.tell(Greeter.Msg.GREET, getSelf());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Object msg) {
|
||||
if (msg == Greeter.Msg.DONE) {
|
||||
// when the greeter is done, stop this actor and with it the application
|
||||
getContext().stop(getSelf());
|
||||
} else unhandled(msg);
|
||||
}
|
||||
}
|
||||
//#hello-world
|
||||
43
akka-docs/rst/java/hello-world.rst
Normal file
43
akka-docs/rst/java/hello-world.rst
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
##########################
|
||||
The Obligatory Hello World
|
||||
##########################
|
||||
|
||||
Since every programming paradigm needs to solve the tough problem of printing a
|
||||
well-known greeting to the console we’ll introduce you to the actor-based
|
||||
version.
|
||||
|
||||
.. includecode:: ../java/code/docs/actor/japi/HelloWorld.java#hello-world
|
||||
|
||||
The ``HelloWorld`` actor is the application’s “main” class; when it terminates
|
||||
the application will shut down—more on that later. The main business logic
|
||||
happens in the :meth:`preStart` method, where a ``Greeter`` actor is created
|
||||
and instructed to issue that greeting we crave for. When the greeter is done it
|
||||
will tell us so by sending back a message, and when that message has been
|
||||
received it will be passed into the :meth:`onReceive` method where we can
|
||||
conclude the demonstration by stopping the ``HelloWorld`` actor. You will be
|
||||
very curious to see how the ``Greeter`` actor performs the actual task:
|
||||
|
||||
.. includecode:: ../java/code/docs/actor/japi/Greeter.java#greeter
|
||||
|
||||
This is extremely simple now: after its creation this actor will not do
|
||||
anything until someone sends it a message, and if that happens to be an
|
||||
invitation to greet the world then the ``Greeter`` complies and informs the
|
||||
requester that the deed has been done.
|
||||
|
||||
As a Java developer you will probably want to tell us that there is no
|
||||
``static public void main(...)`` anywhere in these classes, so how do we run
|
||||
this program? The answer is that the appropriate :meth:`main` method is
|
||||
implemented in the generic launcher class :class:`akka.Main` which expects only
|
||||
one command line argument: the class name of the application’s main actor. This
|
||||
main method will then create the infrastructure needed for running the actors,
|
||||
start the given main actor and arrange for the whole application to shut down
|
||||
once the main actor terminates. Thus you will be able to run the above code
|
||||
with a command similar to the following::
|
||||
|
||||
java -classpath <all those JARs> akka.Main com.example.HelloWorld
|
||||
|
||||
This conveniently assumes placement of the above class definitions in package
|
||||
``com.example`` and it further assumes that you have the required JAR files for
|
||||
``scala-library`` and ``akka-actor`` available. The easiest would be to manage
|
||||
these dependencies with a build tool, see :ref:`build-tool`.
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue