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
  2.  
  3. import akka.actor.{ Actor, ActorSystem, Props }
  4. import akka.kernel.Bootable
  5.  
  6. case object Start
  7.  
  8. class HelloActor extends Actor {
  9. val worldActor = context.actorOf(Props[WorldActor])
  10.  
  11. def receive = {
  12. case Start => worldActor ! "Hello"
  13. case message: String =>
  14. println("Received message '%s'" format message)
  15. }
  16. }
  17.  
  18. class WorldActor extends Actor {
  19. def receive = {
  20. case message: String => sender() ! (message.toUpperCase + " world!")
  21. }
  22. }
  23.  
  24. class HelloKernel extends Bootable {
  25. val system = ActorSystem("hellokernel")
  26.  
  27. def startup = {
  28. system.actorOf(Props[HelloActor]) ! Start
  29. }
  30.  
  31. def shutdown = {
  32. system.shutdown()
  33. }
  34. }

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

Use Ctrl-C to interrupt and exit the microkernel.

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