Merge pull request #15054 from akka/wip-13890-akka-java8-template-master-ban

(Forward Port) +sam #13890 Add Akka Main Activator template for Java 8
This commit is contained in:
Björn Antonsson 2014-04-25 08:58:50 +02:00
commit 4612d6d3f6
13 changed files with 400 additions and 0 deletions

View file

@ -0,0 +1,121 @@
Creative Commons Legal Code
CC0 1.0 Universal
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
HEREUNDER.
Statement of Purpose
The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work of
authorship and/or a database (each, a "Work").
Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without fear
of later claims of infringement build upon, modify, incorporate in other
works, reuse and redistribute as freely as possible in any form whatsoever
and for any purposes, including without limitation commercial purposes.
These owners may contribute to the Commons to promote the ideal of a free
culture and the further production of creative, cultural and scientific
works, or to gain reputation or greater distribution for their Work in
part through the use and efforts of others.
For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under its
terms, with knowledge of his or her Copyright and Related Rights in the
Work and the meaning and intended legal effect of CC0 on those rights.
1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights ("Copyright and
Related Rights"). Copyright and Related Rights include, but are not
limited to, the following:
i. the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
ii. moral rights retained by the original author(s) and/or performer(s);
iii. publicity and privacy rights pertaining to a person's image or
likeness depicted in a Work;
iv. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
v. rights protecting the extraction, dissemination, use and reuse of data
in a Work;
vi. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation
thereof, including any amended or successor version of such
directive); and
vii. other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.
2. Waiver. To the greatest extent permitted by, but not in contravention
of, applicable law, Affirmer hereby overtly, fully, permanently,
irrevocably and unconditionally waives, abandons, and surrenders all of
Affirmer's Copyright and Related Rights and associated claims and causes
of action, whether now known or unknown (including existing as well as
future claims and causes of action), in the Work (i) in all territories
worldwide, (ii) for the maximum duration provided by applicable law or
treaty (including future time extensions), (iii) in any current or future
medium and for any number of copies, and (iv) for any purpose whatsoever,
including without limitation commercial, advertising or promotional
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
member of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal or
equitable action to disrupt the quiet enjoyment of the Work by the public
as contemplated by Affirmer's express Statement of Purpose.
3. Public License Fallback. Should any part of the Waiver for any reason
be judged legally invalid or ineffective under applicable law, then the
Waiver shall be preserved to the maximum extent permitted taking into
account Affirmer's express Statement of Purpose. In addition, to the
extent the Waiver is so judged Affirmer hereby grants to each affected
person a royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer's Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future
time extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"License"). The License shall be deemed effective as of the date CC0 was
applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder
of the License, and in such case Affirmer hereby affirms that he or she
will not (i) exercise any of his or her remaining Copyright and Related
Rights in the Work or (ii) assert any associated claims and causes of
action with respect to the Work, in either case contrary to Affirmer's
express Statement of Purpose.
4. Limitations and Disclaimers.
a. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
b. Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy, or
the present or absence of errors, whether or not discoverable, all to
the greatest extent permissible under applicable law.
c. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person's Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the
Work.
d. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to
this CC0 or use of the Work.

View file

@ -0,0 +1,10 @@
Activator Template by Typesafe
Licensed under Public Domain (CC0)
To the extent possible under law, the person who associated CC0 with
this Activator Tempate has waived all copyright and related or neighboring
rights to this Activator Template.
You should have received a copy of the CC0 legalcode along with this
work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.

View file

@ -0,0 +1,7 @@
name=akka-sample-main-java-lambda
title=Akka Main in Java with Lambdas
description=Actor based version of obligatory Hello World program using the generic launcher class akka.Main.
tags=Basics,akka,java,java8,starter
authorName=Akka Team
authorLink=http://akka.io/
sourceLink=https://github.com/akka/akka

View file

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

View file

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>akka-sample-main-java-lambda</artifactId>
<groupId>com.typesafe.akka.samples</groupId>
<name>Akka Main in Java</name>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.10</artifactId>
<version>2.4-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<compilerArgs>
<arg>-Xlint</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

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

View file

@ -0,0 +1,19 @@
package sample.hello;
import akka.actor.AbstractActor;
import akka.japi.pf.ReceiveBuilder;
public class Greeter extends AbstractActor {
public static enum Msg {
GREET, DONE;
}
public Greeter() {
receive(ReceiveBuilder.
matchEquals(Msg.GREET, m -> {
System.out.println("Hello World!");
sender().tell(Msg.DONE, self());
}).build());
}
}

View file

@ -0,0 +1,26 @@
package sample.hello;
import akka.actor.AbstractActor;
import akka.actor.Props;
import akka.actor.ActorRef;
import akka.japi.pf.ReceiveBuilder;
import static sample.hello.Greeter.Msg;
public class HelloWorld extends AbstractActor {
public HelloWorld() {
receive(ReceiveBuilder.
matchEquals(Msg.DONE, m -> {
// when the greeter is done, stop this actor and with it the application
context().stop(self());
}).build());
}
@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, self());
}
}

View file

@ -0,0 +1,8 @@
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,28 @@
package sample.hello;
import akka.actor.*;
import akka.japi.pf.ReceiveBuilder;
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 AbstractLoggingActor {
private final ActorRef ref;
public Terminator(ActorRef ref) {
this.ref = ref;
getContext().watch(ref);
receive(ReceiveBuilder.
match(Terminated.class, t -> {
log().info("{} has terminated, shutting down system", ref.path());
context().system().shutdown();
}).build());
}
}
}

View file

@ -0,0 +1,3 @@
akka {
loglevel = INFO
}

View file

@ -0,0 +1,119 @@
<!-- <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.4-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>
<div>
<h2>Run with Maven</h2>
<p>
This sample also includes a Maven <a href="#code/pom.xml" class="shortcut">pom.xml</a>.
</p>
<p>
You can run the main classes with <code>mvn</code> from a terminal window using the
<a href="http://mojo.codehaus.org/exec-maven-plugin/" target="_blank">Exec Maven Plugin</a>.
</p>
<pre><code>
mvn compile exec:java -Dexec.mainClass="akka.Main" -Dexec.args="sample.hello.HelloWorld"
</code></pre>
<pre><code>
mvn compile exec:java -Dexec.mainClass="sample.hello.Main2"
</code></pre>
</div>
</body>
</html>

View file

@ -103,3 +103,10 @@ mvncleantest "$java8_home" "akka-samples/akka-sample-fsm-java-lambda"
mvncleantest "$java8_home" "akka-samples/akka-sample-persistence-java-lambda"
mvncleantest "$java8_home" "akka-samples/akka-sample-supervision-java-lambda"
sample_dir=akka-samples/akka-sample-main-java-lambda
tmp="$script_dir/../../$sample_dir"
try cd "$tmp" "can't step into project directory: $tmp"
export JAVA_HOME="$java8_home"
try mvn clean compile exec:java -Dexec.mainClass="akka.Main" -Dexec.args="sample.hello.HelloWorld" "mvn execution in $sample_dir failed"
try mvn exec:java -Dexec.mainClass="sample.hello.Main2" "mvn execution in $sample_dir failed"