Akka Actor Per Request Java Sample

625 views
Skip to first unread message

John Haigh

unread,
Jul 19, 2014, 10:39:08 PM7/19/14
to akka...@googlegroups.com
Hello,

I have seen the Scala Actor Per Request sample at https://github.com/NET-A-PORTER/spray-actor-per-request, and I am wondering if there is a Java version out there in some form? 


I found the following java REST samples but I want to understand what the best method for implementation is. 


Thanks,

John



Konrad Malawski

unread,
Jul 20, 2014, 12:20:45 PM7/20/14
to akka...@googlegroups.com
Hello John,
I don't think there is an equivalent repo to this out there...
Although I assume you've seem this post http://techblog.net-a-porter.com/2013/12/ask-tell-and-per-request-actors/ where this code sample originates from?
The concepts are pretty generic, and I would recommend checking out the concepts if you have not yet already.

Is there a specific question you'd like to see answered here?

-- 
Konrad `ktoso` Malawski
hAkker @ Typesafe 

Thomas Lockney

unread,
Jul 20, 2014, 12:27:30 PM7/20/14
to akka...@googlegroups.com

At least some of this code is necessarily in Scala as Spray does not support Java. You could likely get some of this working (or something approximating it) with the new akka-http module, but it does not yet have an equivalent for spray-routing, so you would have to manually handle the route construction.

--
Thomas Lockney
tho...@lockney.net
http://about.me/tlockney

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

John Haigh

unread,
Jul 21, 2014, 8:15:45 PM7/21/14
to akka...@googlegroups.com
I was looking to know how to write an actor per request. I want to understand this pattern so I could create a sample similar to the one at https://github.com/muuki88/activator-play-cluster-sample that creates a frontend for an akka cluster in Java. 

I'm struggling with how to have a frontend actor send a send a message to an akka cluster. At this point hearing how this would be implemented would be great so I could write this in simple form even using a basic Java console application just to understand the basics of to have a frontend send a message to an akka cluster.

Thanks,

John

Akka Team

unread,
Jul 22, 2014, 4:16:09 AM7/22/14
to Akka User List
Hi John,

I was looking to know how to write an actor per request. I want to understand this pattern so I could create a sample similar to the one at https://github.com/muuki88/activator-play-cluster-sample that creates a frontend for an akka cluster in Java. 

I'm struggling with how to have a frontend actor send a send a message to an akka cluster. At this point hearing how this would be implemented would be great so I could write this in simple form even using a basic Java console application just to understand the basics of to have a frontend send a message to an akka cluster.

We need a bit more information about what you want to achieve. Sending a message from an actor to another actor (irregardless whether it is in a cluster or not) is just like an ordinary message send. Is your problem how to aquire remote ActorRefs from the cluster?

Btw, you can have your frontend be part of the cluster and use simply a cluster-aware router if you have a worker pool for example. You can use cluster sharding if you need persistent stateful entities maintained in the cluster. There are many ways to interact with clustered actor systems. In fact, all that core clustering does is maintaining membership information, all the others are just patterns on top of this, using remoting to send messages to remote actors.

You can look at the list of activator templates, maybe you find something that fits your need: http://akka.io/downloads/

-Endre
 

Thanks,

John


On Sunday, July 20, 2014 12:20:45 PM UTC-4, Konrad Malawski wrote:
Hello John,
I don't think there is an equivalent repo to this out there...
Although I assume you've seem this post http://techblog.net-a-porter.com/2013/12/ask-tell-and-per-request-actors/ where this code sample originates from?
The concepts are pretty generic, and I would recommend checking out the concepts if you have not yet already.

Is there a specific question you'd like to see answered here?

-- 
Konrad `ktoso` Malawski
hAkker @ Typesafe 

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



--
Akka Team
Typesafe - The software stack for applications that scale
Blog: letitcrash.com
Twitter: @akkateam

John Haigh

unread,
Jul 23, 2014, 10:09:03 PM7/23/14
to akka...@googlegroups.com
Hi Endre,

Thanks for the feedback and URL.

I want to achieve the following:

- Send a message from a front end actor (http) to a backend actor.

My question is this.

- In a front end actor is the sending of a message as simple as attaining a reference (in frontend actor onreceive) to the backend actor via an ActorRef and then sending a message to the backend actor?

Thanks,

John

Akka Team

unread,
Jul 24, 2014, 4:43:21 AM7/24/14
to Akka User List
Hi John,



My question is this.

- In a front end actor is the sending of a message as simple as attaining a reference (in frontend actor onreceive) to the backend actor via an ActorRef and then sending a message to the backend actor?

Yes, once you have the ActorRef of an actor you can send a message to it:

  ref ! msg

... and it looks like the same whether "ref" is remote or not (although you need to enable remoting to be able to communicate with remote actors, but that does not change the way you send messages).

-Endre

John Haigh

unread,
Jul 24, 2014, 10:02:26 PM7/24/14
to akka...@googlegroups.com
HI Endre,

Thanks again for the info. 

Just to confirm this sample on akka activator appears to be a frontend/backend sample that I have been looking for: akka-sample-cluster-java 

I have some feedback below. 

//#frontend
public class FactorialFrontend extends UntypedActor {
  final int upToN;
  final boolean repeat;

  LoggingAdapter log = Logging.getLogger(getContext().system(), this);

// Here a reference to the backend actor is created
  ActorRef backend = getContext().actorOf(FromConfig.getInstance().props(),
      "factorialBackendRouter");

  public FactorialFrontend(int upToN, boolean repeat) {
    this.upToN = upToN;
    this.repeat = repeat;
  }

  @Override
  public void preStart() {
    sendJobs();
    getContext().setReceiveTimeout(Duration.create(10, TimeUnit.SECONDS));
  }

  @Override
  public void onReceive(Object message) {
    if (message instanceof FactorialResult) {
      FactorialResult result = (FactorialResult) message;
      if (result.n == upToN) {
        log.debug("{}! = {}", result.n, result.factorial);
        if (repeat)
          sendJobs();
        else
          getContext().stop(getSelf());
      }

    } else if (message instanceof ReceiveTimeout) {
      log.info("Timeout");
      sendJobs();

    } else {
      unhandled(message);
    }
  }

  void sendJobs() {
    log.info("Starting batch of factorials up to [{}]", upToN);
    for (int n = 1; n <= upToN; n++) {

// here a message is sent to akka via tell in fire and forget fashion

      backend.tell(n, getSelf());
    }
  }



Endre

Akka Team

unread,
Jul 25, 2014, 5:23:06 AM7/25/14
to Akka User List
Hi John,


// Here a reference to the backend actor is created
  ActorRef backend = getContext().actorOf(FromConfig.getInstance().props(),
      "factorialBackendRouter");

No, in the above code the actual actor is created and you get back its reference. It is different from looking up the ActorRef of an existing actor. You should probably take a look at the following pages from the documentation:

  Addressing in general:
    http://doc.akka.io/docs/akka/2.3.4/general/addressing.html especially the section http://doc.akka.io/docs/akka/2.3.4/general/addressing.html#How_are_Actor_References_obtained_
  Lifecycle of an Actor and its relation to ActorRefs and ActorPaths: http://doc.akka.io/docs/akka/2.3.4/java/untyped-actors.html#Actor_Lifecycle
  Looking up actor references via path: http://doc.akka.io/docs/akka/2.3.4/java/untyped-actors.html#Identifying_Actors_via_Actor_Selection


-Endre

John Haigh

unread,
Jul 28, 2014, 1:10:56 PM7/28/14
to akka...@googlegroups.com


Further to this I created a sample at https://github.com/haighis/java-jersey-akka-backend-actor. This is written entirely in Java and is based on the sample at https://github.com/pofallon/jersey2-akka-java

This sample demonstrates:

- using actor selection - see below. https://github.com/haighis/java-jersey-akka-backend-actor/blob/master/src/main/java/com/paulsamiq/jersey2akka/ExampleService.java
- a backend actor that will contain jdbc data access code to save to a database

@Context ActorSystem actorSystem;
LoggingAdapter log;

@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response save(LogMessage message)
{
ActorSelection selection = actorSystem.actorSelection("akka://ExampleSystem/user/frontend");

selection.tell(new LogMessage(message.getTitle()), ActorRef.noSender());

String result = "done " + message;
return Response.status(201).entity(result).build();
}

Roland Kuhn

unread,
Aug 12, 2014, 9:30:42 AM8/12/14
to akka-user
Thanks for sharing!


Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


Reply all
Reply to author
Forward
0 new messages