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.

The Akka Microkernel is included in the Akka download found at 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 and additional dependencies in the lib directory to have them automatically loaded and placed on the classpath.

To start the kernel use the scripts in the bin directory, passing the boot classes for your application.

The start script adds config directory first in the classpath, followed by lib/*. It runs java with main class akka.kernel.Main and the supplied Bootable class as argument.

Example command (on a unix-based system):

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):

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");

  public static class HelloActor extends UntypedActor {
    final ActorRef worldActor = getContext().actorOf(
        Props.create(WorldActor.class));

    public void onReceive(Object message) {
      if (message == "start")
        worldActor.tell("Hello", getSelf());
      else if (message instanceof String)
        System.out.println(String.format("Received message '%s'", message));
      else
        unhandled(message);
    }
  }

  public static class WorldActor extends UntypedActor {
    public void onReceive(Object message) {
      if (message instanceof String)
        getSender().tell(((String) message).toUpperCase() + " world!",
            getSelf());
      else
        unhandled(message);
    }
  }

  public void startup() {
    system.actorOf(Props.create(HelloActor.class)).tell("start", null);
  }

  public void shutdown() {
    system.shutdown();
  }
}

Contents