Protected
Protected

REST


Module stability: STABLE

When using Akkas embedded servlet container:


Akka supports the upcoming JSR for REST called JAX-RS (JSR-311). It allows you to annotate your Active Objects and/or Actors with JAX-RS annotation to expose them as REST services through JSON, XML, HTML, Thrift, Protobuf etc.

You can deploy your REST services directly into the Akka kernel. All you have to do is to drop the JAR with your application containing the REST services into the ‘$AKKA_HOME/deploy’ directory and define a “boot class”. WAR deployment is coming soon.

Boot configuration class


The boot class is needed for Akka to bootstrap the application and should contain the initial supervisor configuration of the REST components. This is needed in order to register them as JAX-RS services.

The boot class should be a regular POJO with a default constructor in which the initial configuration is done. The boot class then needs to be defined in the ‘$AKKA_HOME/config/akka.conf’ config file like this:
<akka>
  boot = ["sample.java.Boot", "sample.scala.Boot"]   # FQN to the class doing initial active object/actor
                                                     # supervisor bootstrap, should be defined in default constructor
  ...
</akka>

When deploying in another servlet container:


If you deploy Akka in another JEE container, don't forget to add Akkas initialization and cleanup hook:
<web-app>
...
  <listener>
    <listener-class>se.scalablesolutions.akka.servlet.Initializer</listener-class>
  </listener>
...
</web-app>


Here are an Active Object and Actor example of a REST service with a boot class.

Java API: Active Objects


RESTful Active Object.
@Path("/hello")
public class HelloService {
  @GET
  @Produces({"application/json"})
  public String hello() {
    return "hello"
  }
}

Boot class defining the Active Object.
public class Boot {
  public Boot() throws Exception  {
    ActiveObjectManager manager = new ActiveObjectManager();
    manager.configure(
        new RestartStrategy(new OneForOne(), 3, 5000, new Class[]{Exception.class}),
        new Component[] {
          new Component(
            com.biz.HelloService.class,
            new LifeCycle(new Permanent(), 1000),
            1000)
        }).supervise();
    }
}

Scala API: Actors


RESTful Actor.
@Path("/hello")
class HelloActor extends Actor {
  private case object Hello
 
  @GET
  @Produces(Array("application/json"))
  def hello = (this !! Hello).getOrElse("Couldn't say hello")
 
  def receive = {
    case Hello => reply("hello")
  }
}

Boot class defining the Actor.
class Boot {
  val factory = SupervisorFactory(
    SupervisorConfig(
      RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])),
      Supervise(
        new HelloActor,
        LifeCycle(Permanent))
      :: Nil))
 
  factory.newInstance.start
}

Take the Maven POM file from the akka-samples-scala as an example on how to build and deploy this example.

Using Akka with the Pinky REST/MVC framework


Pinky has a slick Akka integration. Read more here
Home
close
Loading...
Home Turn Off "Getting Started"
close
Loading...