=doc #3689 Make activator template for akka.Main

This commit is contained in:
Patrik Nordwall 2013-11-15 11:53:21 +01:00
parent 8b6b2965e5
commit 362074177a
25 changed files with 428 additions and 184 deletions

View file

@ -0,0 +1,13 @@
Copyright 2013 Typesafe, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View file

@ -0,0 +1,4 @@
name=akka-sample-main-java
title=Akka Main in Java
description=Actor based version of obligatory Hello World program using the generic launcher class akka.Main.
tags=Basics,akka,java,java,starter

View file

@ -0,0 +1,10 @@
name := "akka-sample-main-java"
version := "1.0"
scalaVersion := "2.10.3"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.3-SNAPSHOT"
)

View file

@ -0,0 +1 @@
sbt.version=0.13.0

View file

@ -0,0 +1,23 @@
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package sample.hello;
import akka.actor.UntypedActor;
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);
}
}

View file

@ -0,0 +1,29 @@
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package sample.hello;
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);
}
}

View file

@ -0,0 +1,11 @@
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package sample.hello;
public class Main {
public static void main(String[] args) {
akka.Main.main(new String[] { HelloWorld.class.getName() });
}
}

View file

@ -0,0 +1,43 @@
/**
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package sample.hello;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.Terminated;
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
public class Main2 {
public static void main(String[] args) {
ActorSystem system = ActorSystem.create("Hello");
ActorRef a = system.actorOf(Props.create(HelloWorld.class), "helloWorld");
system.actorOf(Props.create(Terminator.class, a), "terminator");
}
public static class Terminator extends UntypedActor {
private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
private final ActorRef ref;
public Terminator(ActorRef ref) {
this.ref = ref;
getContext().watch(ref);
}
@Override
public void onReceive(Object msg) {
if (msg instanceof Terminated) {
log.info("{} has terminated, shutting down system", ref.path());
getContext().system().shutdown();
} else {
unhandled(msg);
}
}
}
}

View file

@ -0,0 +1,96 @@
<!-- <html> -->
<head>
<title>The Obligatory Hello World</title>
</head>
<body>
<div>
<h2>The Obligatory Hello World</h2>
<p>
Since every programming paradigm needs to solve the tough problem of printing a
well-known greeting to the console well introduce you to the actor-based
version.
</p>
<p>
Open <a href="#code/src/main/java/sample/hello/HelloWorld.java" class="shortcut">HelloWorld.java</a>
</p>
<p>
The <code>HelloWorld</code> actor is the applications “main” class; when it terminates
the application will shut down—more on that later. The main business logic
happens in the <code>preStart</code> method, where a <code>Greeter</code> 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 behavior described by the <code>receive</code>
method where we can conclude the demonstration by stopping the <code>HelloWorld</code>
actor.
</p>
</div>
<div>
<h2>The Greeter</h2>
<p>
You will be very curious to see how the <code>Greeter</code> actor performs the
actual task. Open <a href="#code/src/main/java/sample/hello/Greeter.java" class="shortcut">Greeter.java</a>.
</p>
<p>
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 <code>Greeter</code> complies and informs the
requester that the deed has been done.
</p>
</div>
<div>
<h2>Main class</h2>
<p>
Go to the <a href="#run" class="shortcut">Run</a> tab, and start the application main class
<code>sample.hello.Main</code>. In the log output you can see the "Hello World!" greeting.
</p>
<p>
<a href="#code/src/main/java/sample/hello/Main.java" class="shortcut">Main.java</a>
is actually just a small wrapper around the generic launcher class <code>akka.Main</code>,
which expects only one argument: the class name of the applications 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 application with a
command similar to the following:
</p>
<pre><code>
java -classpath <all those JARs> akka.Main sample.hello.HelloWorld
</code></pre>
<p>
This conveniently assumes placement of the above class definitions in package
<code>sample.hello</code> and it further assumes that you have the required JAR files for
<code>scala-library</code>, <code>typesafe-config</code> and <code>akka-actor</code> available.
The easiest would be to manage these dependencies with a
<a href="http://doc.akka.io/docs/akka/2.3-SNAPSHOT/intro/getting-started.html#Using_a_build_tool" target="_blank">build tool</a>.
</p>
<p>
If you need more control of the startup code than what is provided by <code>akka.Main</code>
you can easily write your own main class such as
<a href="#code/src/main/java/sample/hello/Main2.java" class="shortcut">Main2.java</a>
</p>
<p>
Try to run the <code>sample.hello.Main2</code> class
by selecting it in the 'Main class' menu in the <a href="#run" class="shortcut">Run</a> tab.
</p>
</div>
</body>
</html>