ActorSystem java,如何建立及建立使用JAVA代码在AKKA中重用ActorSystem

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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值