[akka-http] java completeWith mapping future

352 views
Skip to first unread message

Benoit Guillon

unread,
Nov 11, 2015, 6:01:05 AM11/11/15
to Akka User List
Hi,

I'm using akka http with Java DSL and I'm trying to handle a get request with an actor and map my actor's response to the HTTP response as follows:

private Route getTasksRoute(){
   return path("tasks").route(pathEndOrSingleSlash().route(get(this.handleWith(
          (ctx) -> ctx.completeWith(handleGetTask(ctx))
   ))));
}

private Future<RouteResult> handleGetTask(RequestContext ctx){
   return Patterns.ask(actorSystem.actorFor("myActor"), buildMessage(), 1000)
       .map(o -> ctx.completeAs(Jackson.json(), o), ctx.executionContext());
}

This code does not compile in Java 8 because the API enforces to use scala futures

Error:(74, 17) java: method map in interface scala.concurrent.Future<T> cannot be applied to given types;
  required: scala.Function1<java.lang.Object,S>,scala.concurrent.ExecutionContext
  found: (o)->ctx.c[...]), o),scala.concurrent.ExecutionContext
  reason: cannot infer type-variable(s) S
    (argument mismatch; scala.Function1 is not a functional interface
      multiple non-overriding abstract methods found in interface scala.Function1)

I could not find any relevant code example illustrating this use case in java.

Can you suggest a code pattern to illustrate how the completeWith method is intended to be used in Java 8 in conjonction with actors' ask pattern ?

Thanks for your help.

Benoit

André

unread,
Nov 11, 2015, 10:08:15 AM11/11/15
to Akka User List
Hi Benoit,


Mapper should solve your Problem.

Cheers
André

Benoit Guillon

unread,
Nov 12, 2015, 6:00:23 AM11/12/15
to Akka User List
Hi André,

Thanks for your reply, using Mapper works fine (a bit more verbose than the scala implementation)

For those who have the same issue: here is the solution:

private Future<RouteResult> handleGetTask(RequestContext ctx){
    return Patterns.ask(actorSystem.actorFor("myActor"), buildMessage(), 1000)
            .map(new Mapper<Object, RouteResult>(){
                public RouteResult apply(Object o) {
                    return ctx.completeAs(Jackson.json(), o);
                }
          }, ctx.executionContext());
}

Thanks

Benoit

Patrik Nordwall

unread,
Nov 17, 2015, 10:44:36 AM11/17/15
to akka...@googlegroups.com
The Scala Future API is rather clunky for Java when you get used to Java8 lambas. You can try the scala-java8-compat library and work with CompletionStage instead.

Cheers,
Patrik

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--

Patrik Nordwall
Typesafe Reactive apps on the JVM
Twitter: @patriknw

Erik Frister

unread,
Feb 15, 2016, 12:22:37 PM2/15/16
to Akka User List
Thanks Benoit for posting the explicit solution, that saved my some headaches!

For anyone having similar issues like me: be aware that you need to import the scala Future for the return type of the "handleGetTask" method, not the Java future! Otherwise you'll get compilation errors regarding incorrect mapping of types.

import scala.concurrent.Future;

Furthermore, André's code can be slightly simplified by using "handleWithAsync" and Java 8 method references. Instead of:
private Route getTasksRoute(){
   return path("tasks").route(pathEndOrSingleSlash().route(get(this.handleWith(
          (ctx) -> ctx.completeWith(handleGetTask(ctx))
   ))));
}

You can just write:
private Route getTasksRoute(){
   return path("tasks").route(pathEndOrSingleSlash().route(get(handleWithAsync(
          this::handleGetTask
   ))));
}


Hope that helps anyone.

Cheers
Erik
Reply all
Reply to author
Forward
0 new messages