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

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

Reply via email to