首页 文章

如何使用JAVA代码在AKKA中创建和重用ActorSystem

提问于
浏览
1

首先感谢您的快速回复 .

对不起,我仍然怀疑,因为我对AKKA很新 .

现在我们正在运行具有3层架构的Web应用程序[动作层,业务逻辑层,数据访问对象层] .

所以我需要在我的业务逻辑层之后使用AKKA .

对于例如

  • Sender_BLL_1是非Actor java类

1)非Actor调用Java类

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)第一位演员

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);
        }
    }
}

但是假设当我想从另一个BLL文件调用另一个Actor时,我再次需要编写“ActorSystem system = ActorSystem.create(”MySystem“);”用于创建ActorSystem .

对于例如

  • Sender_BLL_2是非Actor java类

1)非Actor调用Java类

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)第二位演员

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);
        }
    }
 }

这意味着我为2个业务逻辑文件创建了2个ActorSystem [它会随着我的BLL文件运行而增加],就像我的Web应用程序中有超过500个业务逻辑文件一样 .

但据我所知,ActorSystem是一个重量级的对象,所以我们只需要为每个逻辑应用程序创建一个 .

那么为任何Web应用程序创建仅1个ActorSystem或检查现有ActorSystem的方法是什么 .

1 回答

  • 1

    您可以使用java singleton对象来保存ActorSystem并使用所有BLL类中的单例 . 对于例如

    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;
        }
    }
    

    用法:

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

    现在,您将在所有BLL类中获得相同的actor系统 .

相关问题