1
votes

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.

1

1 Answers

2
votes

You can use a java singleton object to hold your ActorSystem and use that singleton from all your BLL classes. For e.g.

public class ActorSysContainer {
    private ActorSystem sys;
    private ActorSysContainer() {
        sys = ActorSystem.create("MySystem1");
    }

    public ActorSystem getSystem() {
        return sys
    }

    private static ActorSysContainer instance = null;

    public static synchronized ActorSysContainer getInstance() {
        if (instance == null) {
            instance = new ActorSysContainer();
        }
        return instance;
    }
}

Usage:

ActorSystem s = ActorSysContainer.getInstance().getSystem();
s.actorOf(......);

Now you will get the same actor system in all your BLL classes.