From 8b4b28c6f51b905270b6dd8eac49be9ae065fc4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Antonsson?= Date: Wed, 16 Apr 2014 08:42:32 +0200 Subject: [PATCH] +sam #13890 Add Akka Main Activator template for Java 8 --- .../akka-sample-main-java-lambda/COPYING | 121 ++++++++++++++++++ .../akka-sample-main-java-lambda/LICENSE | 10 ++ .../activator.properties | 7 + .../akka-sample-main-java-lambda/build.sbt | 10 ++ .../akka-sample-main-java-lambda/pom.xml | 41 ++++++ .../project/build.properties | 1 + .../src/main/java/sample/hello/Greeter.java | 19 +++ .../main/java/sample/hello/HelloWorld.java | 26 ++++ .../src/main/java/sample/hello/Main.java | 8 ++ .../src/main/java/sample/hello/Main2.java | 28 ++++ .../src/main/resources/application.conf | 3 + .../tutorial/index.html | 119 +++++++++++++++++ scripts/build/extra-build-steps.sh | 7 + 13 files changed, 400 insertions(+) create mode 100644 akka-samples/akka-sample-main-java-lambda/COPYING create mode 100644 akka-samples/akka-sample-main-java-lambda/LICENSE create mode 100644 akka-samples/akka-sample-main-java-lambda/activator.properties create mode 100644 akka-samples/akka-sample-main-java-lambda/build.sbt create mode 100644 akka-samples/akka-sample-main-java-lambda/pom.xml create mode 100644 akka-samples/akka-sample-main-java-lambda/project/build.properties create mode 100644 akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Greeter.java create mode 100644 akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/HelloWorld.java create mode 100644 akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Main.java create mode 100644 akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Main2.java create mode 100644 akka-samples/akka-sample-main-java-lambda/src/main/resources/application.conf create mode 100644 akka-samples/akka-sample-main-java-lambda/tutorial/index.html diff --git a/akka-samples/akka-sample-main-java-lambda/COPYING b/akka-samples/akka-sample-main-java-lambda/COPYING new file mode 100644 index 0000000000..0e259d42c9 --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/COPYING @@ -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. diff --git a/akka-samples/akka-sample-main-java-lambda/LICENSE b/akka-samples/akka-sample-main-java-lambda/LICENSE new file mode 100644 index 0000000000..287f8dd7fa --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/LICENSE @@ -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 . diff --git a/akka-samples/akka-sample-main-java-lambda/activator.properties b/akka-samples/akka-sample-main-java-lambda/activator.properties new file mode 100644 index 0000000000..4a09a754df --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/activator.properties @@ -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 diff --git a/akka-samples/akka-sample-main-java-lambda/build.sbt b/akka-samples/akka-sample-main-java-lambda/build.sbt new file mode 100644 index 0000000000..f97a28d328 --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/build.sbt @@ -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" +) + diff --git a/akka-samples/akka-sample-main-java-lambda/pom.xml b/akka-samples/akka-sample-main-java-lambda/pom.xml new file mode 100644 index 0000000000..4209674d57 --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/pom.xml @@ -0,0 +1,41 @@ + + + + 4.0.0 + akka-sample-main-java-lambda + com.typesafe.akka.samples + Akka Main in Java + 1.0 + + + UTF-8 + + + + + com.typesafe.akka + akka-actor_2.10 + 2.4-SNAPSHOT + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + true + + -Xlint + + + + + + diff --git a/akka-samples/akka-sample-main-java-lambda/project/build.properties b/akka-samples/akka-sample-main-java-lambda/project/build.properties new file mode 100644 index 0000000000..37b489cb6e --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/project/build.properties @@ -0,0 +1 @@ +sbt.version=0.13.1 diff --git a/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Greeter.java b/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Greeter.java new file mode 100644 index 0000000000..4992a8ea84 --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Greeter.java @@ -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()); + } +} diff --git a/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/HelloWorld.java b/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/HelloWorld.java new file mode 100644 index 0000000000..1be9dcc28c --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/HelloWorld.java @@ -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()); + } +} diff --git a/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Main.java b/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Main.java new file mode 100644 index 0000000000..f96fbcd486 --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Main.java @@ -0,0 +1,8 @@ +package sample.hello; + +public class Main { + + public static void main(String[] args) { + akka.Main.main(new String[] { HelloWorld.class.getName() }); + } +} diff --git a/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Main2.java b/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Main2.java new file mode 100644 index 0000000000..cc67f6bbcc --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/src/main/java/sample/hello/Main2.java @@ -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()); + } + } +} diff --git a/akka-samples/akka-sample-main-java-lambda/src/main/resources/application.conf b/akka-samples/akka-sample-main-java-lambda/src/main/resources/application.conf new file mode 100644 index 0000000000..26cc28fc6b --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/src/main/resources/application.conf @@ -0,0 +1,3 @@ +akka { + loglevel = INFO +} diff --git a/akka-samples/akka-sample-main-java-lambda/tutorial/index.html b/akka-samples/akka-sample-main-java-lambda/tutorial/index.html new file mode 100644 index 0000000000..2c4917fad4 --- /dev/null +++ b/akka-samples/akka-sample-main-java-lambda/tutorial/index.html @@ -0,0 +1,119 @@ + + + The Obligatory Hello World + + + + +
+

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. +

+ +

+Open HelloWorld.java +

+ +

+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 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 behavior described by the receive +method where we can conclude the demonstration by stopping the HelloWorld +actor. +

+ +
+
+ +

The Greeter

+ +

+You will be very curious to see how the Greeter actor performs the +actual task. Open Greeter.java. +

+ +

+ +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. +

+ +
+
+ +

Main class

+ +

+Go to the Run tab, and start the application main class +sample.hello.Main. In the log output you can see the "Hello World!" greeting. +

+ +

+Main.java +is actually just a small wrapper around the generic launcher class akka.Main, +which expects only one 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 application with a +command similar to the following: +

+ +

+java -classpath  akka.Main sample.hello.HelloWorld
+
+ +

+This conveniently assumes placement of the above class definitions in package +sample.hello and it further assumes that you have the required JAR files for +scala-library, typesafe-config and akka-actor available. +The easiest would be to manage these dependencies with a +build tool. +

+ +

+If you need more control of the startup code than what is provided by akka.Main +you can easily write your own main class such as +Main2.java +

+ +

+Try to run the sample.hello.Main2 class +by selecting it in the 'Main class' menu in the Run tab. +

+ +
+ +
+ +

Run with Maven

+ +

+This sample also includes a Maven pom.xml. +

+ +

+You can run the main classes with mvn from a terminal window using the +Exec Maven Plugin. +

+ +

+mvn compile exec:java -Dexec.mainClass="akka.Main" -Dexec.args="sample.hello.HelloWorld"
+
+ +

+mvn compile exec:java -Dexec.mainClass="sample.hello.Main2"
+
+ +
+ + + diff --git a/scripts/build/extra-build-steps.sh b/scripts/build/extra-build-steps.sh index d633e35203..51cbc13afb 100755 --- a/scripts/build/extra-build-steps.sh +++ b/scripts/build/extra-build-steps.sh @@ -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"