From b5960253a22eb79a537f076d79ad48c2cb249d90 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Mon, 23 Apr 2012 14:50:26 +0200 Subject: [PATCH] #1990 - Adding Java and Scala version of the microkernel docs --- akka-docs/java/index.rst | 1 + akka-docs/java/microkernel.rst | 67 +++++++++++++++++++ akka-docs/modules/index.rst | 1 - akka-docs/scala/index.rst | 1 + akka-docs/{modules => scala}/microkernel.rst | 2 +- .../sample/kernel/hello/java/HelloKernel.java | 43 ++++++++++++ 6 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 akka-docs/java/microkernel.rst rename akka-docs/{modules => scala}/microkernel.rst (99%) create mode 100644 akka-samples/akka-sample-hello-kernel/src/main/java/sample/kernel/hello/java/HelloKernel.java diff --git a/akka-docs/java/index.rst b/akka-docs/java/index.rst index 4b7dcb5ebf..981e07f869 100644 --- a/akka-docs/java/index.rst +++ b/akka-docs/java/index.rst @@ -23,3 +23,4 @@ Java API fsm extending-akka zeromq + microkernel diff --git a/akka-docs/java/microkernel.rst b/akka-docs/java/microkernel.rst new file mode 100644 index 0000000000..7416838537 --- /dev/null +++ b/akka-docs/java/microkernel.rst @@ -0,0 +1,67 @@ + +.. _microkernel: + +############# + Microkernel (Java) +############# + +The Akka Microkernel is included in the Akka download found at `downloads`_. + +.. _downloads: http://akka.io/downloads + +To run an application with the microkernel you need to create a Bootable class +that handles the startup and shutdown the application. An example is included below. + +Put your application jar in the ``deploy`` directory to have it automatically +loaded. + +To start the kernel use the scripts in the ``bin`` directory, passing the boot +classes for your application. + +There is a simple example of an application setup for running with the +microkernel included in the akka download. This can be run with the following +command (on a unix-based system): + +.. code-block:: none + + bin/akka sample.kernel.hello.HelloKernel + +Use ``Ctrl-C`` to interrupt and exit the microkernel. + +On a Windows machine you can also use the bin/akka.bat script. + +The code for the Hello Kernel example (see the ``HelloKernel`` class for an example +of creating a Bootable): + +.. includecode:: ../../akka-samples/akka-sample-hello-kernel/src/main/java/sample/kernel/hello/java/HelloKernel.java + + +Distribution of microkernel application +--------------------------------------- + +To make a distribution package of the microkernel and your application the ``akka-sbt-plugin`` provides +``AkkaKernelPlugin``. It creates the directory structure, with jar files, configuration files and +start scripts. + +To use the sbt plugin you define it in your ``project/plugins.sbt``: + +.. includecode:: ../../akka-sbt-plugin/sample/project/plugins.sbt + +Then you add it to the settings of your ``project/Build.scala``. It is also important that you add the ``akka-kernel`` dependency. +This is an example of a complete sbt build file: + +.. includecode:: ../../akka-sbt-plugin/sample/project/Build.scala + +Run the plugin with sbt:: + + > dist + > dist:clean + +There are several settings that can be defined: + +* ``outputDirectory`` - destination directory of the package, default ``target/dist`` +* ``distJvmOptions`` - JVM parameters to be used in the start script +* ``configSourceDirs`` - Configuration files are copied from these directories, default ``src/config``, ``src/main/config``, ``src/main/resources`` +* ``distMainClass`` - Kernel main class to use in start script +* ``libFilter`` - Filter of dependency jar files +* ``additionalLibs`` - Additional dependency jar files diff --git a/akka-docs/modules/index.rst b/akka-docs/modules/index.rst index 603eeb2084..1abb0b7a0e 100644 --- a/akka-docs/modules/index.rst +++ b/akka-docs/modules/index.rst @@ -6,6 +6,5 @@ Modules durable-mailbox http - microkernel camel spring diff --git a/akka-docs/scala/index.rst b/akka-docs/scala/index.rst index 46a84fe064..0019271e2d 100644 --- a/akka-docs/scala/index.rst +++ b/akka-docs/scala/index.rst @@ -26,3 +26,4 @@ Scala API testing extending-akka zeromq + microkernel diff --git a/akka-docs/modules/microkernel.rst b/akka-docs/scala/microkernel.rst similarity index 99% rename from akka-docs/modules/microkernel.rst rename to akka-docs/scala/microkernel.rst index 7600e1ebd2..236149964c 100644 --- a/akka-docs/modules/microkernel.rst +++ b/akka-docs/scala/microkernel.rst @@ -2,7 +2,7 @@ .. _microkernel: ############# - Microkernel + Microkernel (Scala) ############# The Akka Microkernel is included in the Akka download found at `downloads`_. diff --git a/akka-samples/akka-sample-hello-kernel/src/main/java/sample/kernel/hello/java/HelloKernel.java b/akka-samples/akka-sample-hello-kernel/src/main/java/sample/kernel/hello/java/HelloKernel.java new file mode 100644 index 0000000000..d0ccc4ad79 --- /dev/null +++ b/akka-samples/akka-sample-hello-kernel/src/main/java/sample/kernel/hello/java/HelloKernel.java @@ -0,0 +1,43 @@ +/** + * Copyright (C) 2009-2012 Typesafe Inc. + */ +package sample.kernel.hello.java; + +import akka.actor.ActorRef; +import akka.actor.UntypedActor; +import akka.actor.ActorSystem; +import akka.actor.Props; +import akka.kernel.Bootable; + +public class HelloKernel implements Bootable { + final ActorSystem system = ActorSystem.create("hellokernel"); + + static class HelloActor extends UntypedActor { + final ActorRef worldActor = + getContext().actorOf(new Props(WorldActor.class)); + + public void onReceive(Object message) { + if (message == "start") + worldActor.tell("Hello"); + else if (message instanceof String) + System.out.println("Received message '%s'".format((String)message)); + else unhandled(message); + } +} + +static class WorldActor extends UntypedActor { + public void onReceive(Object message) { + if (message instanceof String) + getSender().tell(((String)message).toUpperCase() + " world!"); + else unhandled(message); + } +} + + public void startup() { + system.actorOf(new Props(HelloActor.class)).tell("start"); + } + + public void shutdown() { + system.shutdown(); + } +}