Microkernel
Loading

Microkernel

The purpose of the Akka Microkernel is to offer a bundling mechanism so that you can distribute an Akka application as a single payload, without the need to run in a Java Application Server or manually having to create a launcher script.

Warning

Akka Microkernel will be deprecated and removed. It will be replaced by using an ordinary user defined main class and packaging with sbt-native-packager or Typesafe ConductR.

To run an application with the microkernel you need to create a Bootable class that handles the startup and shutdown the application.

The code for the Hello Kernel example (see the HelloKernel class for an example of creating a Bootable):

  1. package sample.kernel.hello.java;
  2.  
  3. import akka.actor.ActorRef;
  4. import akka.actor.UntypedActor;
  5. import akka.actor.ActorSystem;
  6. import akka.actor.Props;
  7. import akka.kernel.Bootable;
  8.  
  9. public class HelloKernel implements Bootable {
  10. final ActorSystem system = ActorSystem.create("hellokernel");
  11.  
  12. public static class HelloActor extends UntypedActor {
  13. final ActorRef worldActor = getContext().actorOf(
  14. Props.create(WorldActor.class));
  15.  
  16. public void onReceive(Object message) {
  17. if (message == "start")
  18. worldActor.tell("Hello", getSelf());
  19. else if (message instanceof String)
  20. System.out.println(String.format("Received message '%s'", message));
  21. else
  22. unhandled(message);
  23. }
  24. }
  25.  
  26. public static class WorldActor extends UntypedActor {
  27. public void onReceive(Object message) {
  28. if (message instanceof String)
  29. getSender().tell(((String) message).toUpperCase() + " world!",
  30. getSelf());
  31. else
  32. unhandled(message);
  33. }
  34. }
  35.  
  36. public void startup() {
  37. system.actorOf(Props.create(HelloActor.class)).tell("start", null);
  38. }
  39.  
  40. public void shutdown() {
  41. system.shutdown();
  42. }
  43. }

sbt-native-packager is the recommended tool for creating distributions of Akka applications when using sbt.

Define sbt version in project/build.properties file:

  1. sbt.version=0.13.7

Add sbt-native-packager in project/plugins.sbt file:

  1. addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.0.0-RC1")

Use the package settings and specify the mainClass in build.sbt file:

  1. import NativePackagerHelper._
  2.  
  3. name := "hello-kernel"
  4.  
  5. version := "0.1"
  6.  
  7. val akkaVersion = "2.3.16"
  8.  
  9. libraryDependencies ++= Seq(
  10. "com.typesafe.akka" %% "akka-kernel" % akkaVersion,
  11. "com.typesafe.akka" %% "akka-actor" % akkaVersion,
  12. "com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
  13. "ch.qos.logback" % "logback-classic" % "1.0.7"
  14. )
  15.  
  16. mainClass in Compile := Some("akka.kernel.Main")
  17.  
  18. enablePlugins(JavaServerAppPackaging)
  19.  
  20. mappings in Universal ++= {
  21. // optional example illustrating how to copy additional directory
  22. directory("scripts") ++
  23. // copy configuration files to config directory
  24. contentOf("src/main/resources").toMap.mapValues("config/" + _)
  25. }
  26.  
  27. // add 'config' directory first in the classpath of the start script,
  28. // an alternative is to set the config file locations via CLI parameters
  29. // when starting the application
  30. scriptClasspath := Seq("../config/") ++ scriptClasspath.value

Note

Use the JavaServerAppPackaging. Don't use AkkaAppPackaging (previously named packageArchetype.akka_application, since it doesn't have the same flexibility and quality as the JavaServerAppPackaging.

Use sbt task dist package the application.

To start the application (on a unix-based system):

  1. cd target/universal/
  2. unzip hello-kernel-0.1.zip
  3. chmod u+x hello-kernel-0.1/bin/hello-kernel
  4. hello-kernel-0.1/bin/hello-kernel sample.kernel.hello.java.HelloKernel

Use Ctrl-C to interrupt and exit the microkernel.

On a Windows machine you can also use the bin\hello-kernel.bat script.