Re: [akka-user] Reusing ActorSystem / Actor in the web, generating unique names.

335 views
Skip to first unread message

Patrik Nordwall

unread,
Jun 11, 2012, 2:11:33 AM6/11/12
to akka...@googlegroups.com


11 jun 2012 kl. 02:28 skrev dimgel <dim...@mail.ru>:

Hi all,

Since actors are stateful, I have to create an actor tree for each HTTP request I handle, right?

That depends on what you do in the actors. An actor can also be stateless and reused for each req. Note that an actor instance process only one message at a time, so for processing http requests it makes sense to create a new for each request or use a router (see docs) with a pool of actors that process the requests.

Creating many top level actors, concurrently, isn't recommended so in case you decide to create a new actor for each req you should delegate to fixed actor that creates the actor and forwards the req.


Have I to create ActorSystem for each request too,
No. ActorSystem is rather heavyweight. It creates thread pools etc.

or I can create the only one in Servlet.init(), reuse it for all (possibly concurrent) requests, and shut it down in Servlet.destroy()?

Yes


Is there any recommended way of generating unique ActorSystem names if each request needs its own ActorSystem, or unique Actor names otherwise? (AFAIK names are optional and that's OK for standalone server, but anyway maybe someone came upon some guidelines about naming in distributed environment.)

Thanks. =)

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/cyCBgntIxkIJ.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

prakash....@ivgroup.in

unread,
Sep 10, 2014, 11:18:19 AM9/10/14
to akka...@googlegroups.com
Hello All,

Can anyone tell me that how i can create only one ActorSystem & reuse it for all my requests in application ???

On Monday, June 11, 2012 5:58:27 AM UTC+5:30, dimgel wrote:
Hi all,

Since actors are stateful, I have to create an actor tree for each HTTP request I handle, right?

Have I to create ActorSystem for each request too, or I can create the only one in Servlet.init(), reuse it for all (possibly concurrent) requests, and shut it down in Servlet.destroy()?

Akka Team

unread,
Sep 10, 2014, 12:28:38 PM9/10/14
to Akka User List
Just start one, and not many of them... :-)
`val sys = ActorSystem()`

If you're in play or spray you already have one running probably mind you.

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



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

Piyush Mishra

unread,
Sep 10, 2014, 1:24:00 PM9/10/14
to akka...@googlegroups.com
An ActorSystem is a heavyweight object so create only one per logical
application.

You can put it inside a trait and can resue it.

trait ActorSystemHelper {

val system = ActorSystem("system")

prakash....@ivgroup.in

unread,
Sep 11, 2014, 11:06:31 AM9/11/14
to akka...@googlegroups.com


Hello All,

First of all thanks for your prompt reply.

Sorry but i am still having doubt because i am very new to AKKA.

Right now we are running Web Application with 3 tier architecture [Actions Layer, Business Logic Layer, Data Access Object Layer].

So i need to use AKKA after my Business Logic Layer.

For e.g.

-> Sender_BLL_1 is Non-Actor java class

1) Non-Actor Calling Java class

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class Sender_BLL_1 {

private void run() {

ActorSystem system = ActorSystem.create("MySystem1"); <-----
ActorRef myActor = system.actorOf(new Props(AkkaActor1.class), "AkkaActor1");
myActor.tell("Hello");
}
}

2) First Actor

import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;

public class AkkaActor1 extends UntypedActor {

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

public void onReceive(Object object) throws Exception {

if (object instanceof String) {
String str = (String) object;
log.info("Received String message in AkkaActor1 : {}", str);
} else {
unhandled(object);
}
}
}

But suppose when i want call another Actor from another BLL file then again i need to write
" ActorSystem system = ActorSystem.create("MySystem"); " for creating ActorSystem.

For e.g.

-> Sender_BLL_2 is Non-Actor java class

1) Non-Actor Calling Java class

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class Sender_BLL_2 {

private void run() {

ActorSystem system = ActorSystem.create("MySystem2"); <-----
ActorRef myActor = system.actorOf(new Props(AkkaActor2.class), "AkkaActor2");
myActor.tell("Hello");
}
}

2) Second Actor

import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;

public class AkkaActor2 extends UntypedActor {

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

public void onReceive(Object object) throws Exception {

if (object instanceof String) {
String str = (String) object;
log.info("Received String message in AkkaActor2 : {}", str);
} else {
unhandled(object);
}
}
}

That means i have created 2 ActorSystem for 2 Business Logic files [it will increase as my BLL file runs] like that we are
having more then 500 Business Logic files in my Web Application.

But as i know that An ActorSystem is a heavyweight object so we need to create only one per logical application.

So what is the way to create only 1 ActorSystem for any Web Application or to check for existing ActorSystem.

Piyush Mishra

unread,
Sep 11, 2014, 11:37:00 AM9/11/14
to akka...@googlegroups.com
Create a singleton object which contains actorsystem as a instance
member and reuse this object across whole application. .

prakash....@ivgroup.in

unread,
Sep 13, 2014, 3:42:35 AM9/13/14
to akka...@googlegroups.com
Hello All,

It's Done.

Thank you so much for your support & to understand my problem very easily with prompt solution.

Akka Team

unread,
Sep 13, 2014, 6:27:01 AM9/13/14
to Akka User List
Hi Prakash,
you will have to share (as in "pass around") the ActorSystem in one way or another.
While a global variable with it is not the best idea for testing, it does solve the problem, agreed.

Instead though (which does not break testing as much, and allows parallel tests), simply pass around the ActorSystem via constructors of your classes.
If someone needs to create top-level actors, he should get the ActorSystem passed in as an constructor argument.
Please remember that an Actor can also start child-Actors (context.actorOf) which is the recommended way of starting more actors (build hierarhies).

-- Konrad

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

prakash....@ivgroup.in

unread,
Sep 23, 2014, 10:24:56 AM9/23/14
to akka...@googlegroups.com
Again thanks for previous help. Only because of previous help we can moved on for our AKKA Project.

Now need to know in deep.

Problem Statement :-

I am having 1 Table with 1 column in my Database ok.

|---------|
|Amount   |
|---------|
|0        |
|---------|

package com.ivasyncapp.worker;

import akka.actor.UntypedActor;

import com.logsapp.logs.LogWriter;

public class Worker extends UntypedActor {

private static final String TRACE_ID = "Worker";
    LoggingAdapter log = Logging.getLogger(getContext().system(), this);

    public void onReceive(Object object) throws Exception {

        if(object instanceof Double) {
            Double amt = (Double) object;
            log.info("Amount : {}", amt);
            //Assume that is Database Update Query. It will update my column
        } else {
            unhandled(object);
        }
    }
}
Above mentioned Actor will run on each our user request.

Now suppose on my application we have 3 request parallely.

1st request : For updating Amount by 100. 2st request : For updating Amount by 200. 3st request : For updating Amount by 300.

So after complition of my 3 request processing there should be 600 in our table.

|---------|
|Amount   |
|---------|
|600      |
|---------|
But What happing is ?????

As we know AKKA is asynchronous in nature so it will run actors asynchronously.

So It will give output diffrent, sometime it will provides

|---------|
|Amount   |
|---------|
|100      |
|---------|
OR

|---------|
|Amount   |
|---------|
|200      |
|---------|
OR

|---------|
|Amount   |
|---------|
|300      |
|---------|
But i want to synchronise my actor logic of Database update so only 1 actor can use that at a time :

package com.ivasyncapp.worker;

import akka.actor.UntypedActor;

import com.logsapp.logs.LogWriter;

public class Worker extends UntypedActor {

    private static final String TRACE_ID = "Worker";
    LoggingAdapter log = Logging.getLogger(getContext().system(), this);

    //Synchronised Logic so only 1 Actor can use at a time
    public void onReceive(Object object) throws Exception {

        if(object instanceof Double) {
            Double amt = (Double) object;
            log.info("Amount : {}", amt);
            //Assume that is Database Update Query. It will update my column
        } else {
            unhandled(object);
        }
    }
    //Synchronised Logic
}
So please suggest me how to do this & also suggest for this which AKKA concept we have to read & from where ???

Björn Antonsson

unread,
Sep 24, 2014, 6:05:32 AM9/24/14
to akka...@googlegroups.com
Hi,

I don't completely understand how you have designed your system or why you have/think you have a problem.

If there is one actor representing the Amount in your table, and all requests to change the amount go through that actor, then the requests will happen one after each other.

This means that if you split up your requests by user and have one actor per user that handle all requests for that user, then that users requests will happen sequentially, but requests for multiple users will happen concurrently.

There are a number of books explaining Akka and actor programming in this list:


B/
--

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

-- 
Björn Antonsson
Typesafe – Reactive Apps on the JVM
twitter: @bantonsson

Idris Mokhtarzada

unread,
Sep 26, 2014, 6:49:41 AM9/26/14
to akka...@googlegroups.com
Can someone elaborate on this part of Patrik's response? 

Creating many top level actors, concurrently, isn't recommended

Why is that not recommended?  What are the downsides?

Björn Antonsson

unread,
Sep 26, 2014, 7:12:20 AM9/26/14
to akka...@googlegroups.com
Hi,

Two things, system.actorOf is a more costly operation than context.actorOf just synchronization wise, and having a very large number of children at the user level (there is a guardian actor that sits on top of the user level), can make other operations slower, like matching actor paths when using actor selection.

B/
--

>>>>>>>>>> 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.
Reply all
Reply to author
Forward
0 new messages