scalaplayframeworkakkaplay2-mini

Blending Akka 2, the Play2-mini framework and HTTP


With the release of Akka 2, the Akka HTTP modules have been replaced with the option of using Play2-mini, where Play2-mini looks like Play2 minus model view controller.

The line greys out between implementing a REST service and creating an HTTP client. For example, say I want to create a web service (does not have to be REST) and a HTTP client in one service, ie a HTTP proxy. Do I use Akka or Play2-mini?

I've created such a service in Finagle and would like to redo the exercise with Akka and/or Play2-mini to see how it compares.

At a high level, what does the architecture look like? How do these products fit together?


Solution

  • I would say Spray is your best bet. However, it can't be used with Java. We are using the play2-mini framework, but there are some issues. It's not clear how to connect it to Akka, with Java, as compared to Spray which is entirely built around the notion - when a request comes in, you get a Request message to an actor.

    With Play, you have to roll your own connection: I.e, inside the static (roles eyes) request method:

    Timeout timeout = new Timeout(Duration.parse("20 seconds"));
    Future<Object> future = Patterns.ask(myActor, new ClientMessage(null), timeout);
    
    Promise<Object> sdf = Akka.asPromise(future);
    Promise<Result> r2 = sdf.map(new Function<Object, Result>() {
    
    @Override
    public Result apply(Object a) throws Throwable {
        val wsrm = (MyMessage)a;
        return ok((wsrm).val); // String value from message object
    }
    
    });
    Result test2 = async(r2);
    return test2;
    

    Which works well. And Play uses AKKA eventing in it's system too, so you can create your actor's using it's actor context too.

    Unfortunately, currently, Play2-mini is not mini at all, it depends on the entire Play framework, which also causes more issues. Apparently they're working on a bare bones release, which AFAIK is going to involve splitting apart Play into it's modules, and I don't see that happening any time soon.

    IMO, Spray is a much better choice. It's fluentness fits much better with AKKA, but unfort I have to use Java here, so I couldn't use it: https://github.com/spray/spray/issues/96

    With regards to your http client / services question - AKKA doesn't have any HTTP capabilities itself, so you need to interface with an HTTP server, in this case play. You can use Async requests to keep the connection alive, while your actor system asynchronously passes messages to your http client actor to asynchronously get an http response, sending the message back to the webservice layer, handing back to play.

    Hopefully that clears up some confusion. I was also quite confused, until a couple of days of research ;) If there's anything else I can help clear up, please let me know - for the good of the community! ;)

    Spray can has an async http client, but for us folks stuck in Java land, there's also: https://github.com/sonatype/async-http-client, which you can use with AKKA probably.