本文介绍了如何建立及建立使用JAVA代码在AKKA中重用ActorSystem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先感谢您的及时答复。



对不起,但我仍然对此表示怀疑,因为我对AKKA非常陌生。



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



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



例如



-> Sender_BLL_1是非演员java类



1)非演员调用Java类

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

公共类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;

公共类AkkaActor1扩展了UntypedActor {

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

public void onReceive(Object object)引发异常{

if(object instanceof String){
String str =(String)object;
log.info( AkkaActor1中收到的字符串消息:{},str);
} else {
unhandled(object);
}
}
}

但是假设我想打电话



为了创建另一个Actor,我再次需要写
ActorSystem system = ActorSystem.create( MySystem);用于创建ActorSystem。



例如



-> Sender_BLL_2是非演员Java类



1)非演员调用Java类

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

公共类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;

公共类AkkaActor2扩展了UntypedActor {

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

public void onReceive(Object object)引发异常{

if(object instanceof String){
String str =(String)object;
log.info( AkkaActor2中收到的字符串消息:{},str);
} else {
unhandled(object);
}
}
}

这意味着我已经创建了2用于2个业务逻辑文件的ActorSystem [它会随着我的BLL文件的运行而增加],就像我们
在Web应用程序中有500多个业务逻辑文件一样。



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



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

解决方案

您可以使用Java单例对象来保存ActorSystem并从您所有的BLL课程。例如,

 公共类ActorSysContainer {
private ActorSystem sys;
私有ActorSysContainer(){
sys = ActorSystem.create( MySystem1);
}

public ActorSystem getSystem(){
return sys
}

private static ActorSysContainer instance = null;

公共静态同步ActorSysContainer getInstance(){
if(instance == null){
instance = new ActorSysContainer();
}
返回实例;
}
}

用法:

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

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


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

解决方案

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.

这篇关于如何建立及建立使用JAVA代码在AKKA中重用ActorSystem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 20:25