akka-http respond with result of an actor call (java-api)

634 views
Skip to first unread message

Peter

unread,
Jan 8, 2015, 6:07:58 AM1/8/15
to akka...@googlegroups.com
Hello,
I'm playing arround with the Java-API of the new akka-http server and I would like to reply to a request with the result of an asynchronous actor (i.e. some kind of DB-query).

I would have expected that I somehow can pass a Future to the context.complete or a "Future" entity. But this seams not be possible.

One  solution seams to be to generate a streaming entity and stream the result back. But how do I do it if I just want to have a "traditional" http response?

import akka.actor.ActorRef;

import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.http.model.japi.HttpMethods;
import akka.http.model.japi.HttpResponse;
import akka.http.model.japi.headers.AccessControlAllowHeaders;
import akka.http.model.japi.headers.AccessControlAllowMethods;
import akka.http.model.japi.headers.AccessControlAllowOrigin;
import akka.http.model.japi.headers.HttpOriginRange;
import akka.http.server.japi.HttpApp;
import akka.http.server.japi.RequestContext;
import akka.http.server.japi.Route;
import akka.pattern.Patterns;


public class MetaApi extends HttpApp {
   
   
private static ActorSystem actorSystem;
   
private static ActorRef handlerActor;
 
   
public static class HandlerActor extends UntypedActor {
     
@Override
     
public void onReceive( Object message ) throws Exception {
         
if ( message instanceof RequestContext ) {
           
RequestContext ctx = (RequestContext)message;
// ...
         
} else {
            unhandled
( message );
         
}
     
}
   
}
   
@Override
   
public Route createRoute() {
     
return route( path( "meta" ).route(
           
get( handleWith( ctx -> {
               
HttpResponse response = HttpResponse.create()
                     
.addHeader( AccessControlAllowOrigin.create( HttpOriginRange.ALL ) )
                     
.addHeader(
                           
AccessControlAllowHeaders.create( "Access-Control-Allow-Origin",
                                 
"Access-Control-Allow-Method", "Content-Type" ) )
                     
.addHeader(
                           
AccessControlAllowMethods.create( HttpMethods.GET, HttpMethods.POST, HttpMethods.PUT,
                                 
HttpMethods.OPTIONS, HttpMethods.DELETE ) )
                     
.withEntity( "Response of actor?" ); // Patterns.ask( handlerActor, ctx, 10_000 );
               
return ctx.complete( response );
           
} ) ) ) );
   
}


   
public static void main(String ... args) throws Exception {
     
MetaApi api = new MetaApi();
      actorSystem
= ActorSystem.create();
         handlerActor
= actorSystem.actorOf( Props.create( HandlerActor.class ), "Get Handler" );
      api
.bindRoute("localhost",8080, actorSystem);
   
}


}


Thanks & best regards
Peter

Asaf Shakarzy

unread,
Jan 12, 2015, 12:40:13 PM1/12/15
to akka...@googlegroups.com
+1! ping? anyone?

Martynas Mickevičius

unread,
Jan 20, 2015, 10:57:46 AM1/20/15
to akka...@googlegroups.com

--
>>>>>>>>>> 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.



--
Martynas Mickevičius
TypesafeReactive Apps on the JVM

pja...@tibco.com

unread,
Jun 19, 2015, 3:23:35 AM6/19/15
to akka...@googlegroups.com
Hi Peter,

We exactly want this, we want to hand it off this to an actor and handle the response asynchronously. What did you do about this problem?

Konrad Malawski

unread,
Jun 26, 2015, 9:08:43 AM6/26/15
to Akka User List
Have you looked into the examples inside the Spec Martynas linked to?

--
>>>>>>>>>> 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.



--
Cheers,
Konrad 'ktoso' Malawski

Johannes Rudolph

unread,
Jul 10, 2015, 8:30:08 AM7/10/15
to akka...@googlegroups.com
Hi,

in the Java API there are currently two ways to deal with Futures. You can use `RequestContext.completeWith` to "transform" a `Future<RouteResult>` into a `RouteResult`. Or, if you use the reflective `handleWith` directive you can point it to a method that returns a `Future<RouteResult>` instead of a `RouteResult`. Both ways allow you to pass the `RequestContext` around to complete asynchronously but instantly return a `Future<RouteResult>`.

HTH
Johannes
Reply all
Reply to author
Forward
0 new messages