The Scala Future API is rather clunky for Java when you get used to Java8 lambas. You can try the scala-java8-compat <https://github.com/scala/scala-java8-compat> library and work with CompletionStage instead.
Cheers, Patrik On Thu, Nov 12, 2015 at 12:00 PM, Benoit Guillon <benoit.guil...@gmail.com> wrote: > 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 > > > Le mercredi 11 novembre 2015 16:08:15 UTC+1, André a écrit : >> >> Hi Benoit, >> >> you should take a look at >> http://doc.akka.io/docs/akka/snapshot/java/futures.html#Future_is_a_Monad >> . >> >> Mapper should solve your Problem. >> >> Cheers >> André >> >> On Wednesday, November 11, 2015 at 12:01:05 PM UTC+1, Benoit Guillon >> wrote: >>> >>> 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 >>> >>> -- > >>>>>>>>>> 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+unsubscr...@googlegroups.com. > To post to this group, send email to akka-user@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 <http://typesafe.com/> - Reactive apps on the JVM Twitter: @patriknw -- >>>>>>>>>> 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+unsubscr...@googlegroups.com. To post to this group, send email to akka-user@googlegroups.com. Visit this group at http://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.