Announcing Akka 24.05: More Security. More Performance. More Efficiency. Watch the Webinar Replay
java8 akka play-framework

Go Reactive with Java 8

We are very excited about the release of Java 8!  The new Lambda syntax makes it much easier for Java developers to go Reactive. The foundation of Reactive is Event-Driven. To be Event Driven you need an easy way to create and spawn up small independent tasks in an asynchronous fashion. Lambdas gives us exactly this, avoiding the usual anonymous inner classes that make code hard to comprehend. Combining Lambdas with Akka and Play Framework create a concise and cohesive programming model for Reactive applications on the JVM.

Akka provides an Actor-based model for concurrency and clusters.  To communicate with an Actor you send it an async message.  The Actor then chooses based on the type of the message (and/or its payload) what to do.  This is an event-driven model that pairs very well with Lambdas. Here is an example Actor that handles String and Integer messages:

public class MyActor extends AbstractActor {
    @Override public PartialFunction<Object, BoxedUnit> receive() {
        return ReceiveBuilder.
            match(String.class, System.out::println).
            match(Integer.class, i -> System.out.println("Received Integer " + i)).

While not very complicated this example shows off the power of the Lambda by providing a clean way to react to messages in an event driven way. To get working sample applications that use Actors and Lambdas check out the following Activator templates:

Play Framework is an async & non-blocking web framework that also pairs very well with Java 8's Lambdas.  Whether you are doing real-time communication with WebSockets or doing Reactive composition of services, the Lambda syntax provides a very concise and readable way to be async & non-blocking. Here is an example controller that handles a request which makes another request but does not block either connection when waiting for a response:

public static F.Promise<Result> foo() {
    return WS.url("").get().map(response -> ok(response.getBody()));

The .get() call doesn't block but instead returns a handle to something that will in the future have the response. The .map takes a Lambda that transforms that response into the Result that is returned to the user. To get a working sample application that illustrates the different ways to use Lambdas with Play Framework check out the Go Reactive with Java 8 and Play Framework Activator template.

You can also combine Akka and Play Framework for a cohesive Reactive platform that uses Actors and async & non-blocking controllers. To illustrate this we've built a sample application called Reactive Stocks that will walk you through a fully Reactive application which uses Actors, async & non-blocking WebSockets and controllers, and a real-time JavaScript UI. Grab the Reactive Stocks (Java 8) Activator template to get started.

On March 26th we will be hosting a webinar to dive further into Going Reactive with Java 8. This webinar is filling up quick so sign up now! We hope to see you there and please let us know what you think about the new Activator templates.

The Total Economic Impact™
Of Lightbend Akka

  • 139% ROI
  • 50% to 75% faster time-to-market
  • 20x increase in developer throughput
  • <6 months Akka pays for itself