[akka-user] Re: [akka-http] java completeWith mapping future

2016-02-15 Thread 'Erik Frister' via 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

On Thursday, November 12, 2015 at 12:00:23 PM UTC+1, Benoit Guillon 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 handleGetTask(RequestContext ctx){
> return Patterns.ask(actorSystem.actorFor("myActor"), buildMessage(), 
> 1000)
> .map(new Mapper(){
> 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 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 
>>> cannot be applied to given types;
>>>   required: 
>>> scala.Function1,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 https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Re: [akka-http] java completeWith mapping future

2015-11-17 Thread Patrik Nordwall
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

On Thu, Nov 12, 2015 at 12:00 PM, Benoit Guillon 
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 handleGetTask(RequestContext ctx){
> return Patterns.ask(actorSystem.actorFor("myActor"), buildMessage(),
> 1000)
> .map(new Mapper(){
> 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 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
>>> cannot be applied to given types;
>>>   required:
>>> scala.Function1,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  -  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.


[akka-user] Re: [akka-http] java completeWith mapping future

2015-11-12 Thread Benoit Guillon
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 handleGetTask(RequestContext ctx){
return Patterns.ask(actorSystem.actorFor("myActor"), buildMessage(), 
1000)
.map(new Mapper(){
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 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 
>> cannot be applied to given types;
>>   required: 
>> scala.Function1,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.


[akka-user] Re: [akka-http] java completeWith mapping future

2015-11-11 Thread André
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 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 
> cannot be applied to given types;
>   required: 
> scala.Function1,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.