欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AKKA笔记2

程序员文章站 2022-07-10 22:12:49
...

 

 1.Actor是个 scala写的接口

 

AKKA笔记2
            
    
    博客分类: javaspark  

 

除了获取sender  self   recieve  其他的打眼一看面向切面的n多方法

 

其中: preStart 用于发送前初始化参数 postStop用于结束后回收资源

 

2. Actor  sender , self , path 每个actor都有系统内唯一的一个path 

 

     master.tell("start", master);

 

AKKA笔记2
            
    
    博客分类: javaspark  

 

path :  akka://test/user/master 

 

Actor之间发送消息:

 

定义另一个 Actor1 和 Actor2

 

public class Actor1 extends UntypedActor{

 

@Override

public void onReceive(Object msg) throws Exception {

System.out.println(""+this.getClass().getSimpleName()+"::sender:"+this.getSender());

if (msg instanceof String){

String s = (String) msg;

this.getSender().tell("akka.apply.actor.Actor2", this.getSelf());

}else

this.unhandled(msg);

}

 

}

 

 

public class Actor2 extends UntypedActor{

 

@Override

public void onReceive(Object msg) throws Exception {

System.out.println(""+this.getClass().getSimpleName()+"::sender:"+this.getSender());

if (msg instanceof String){

String s = (String) msg;

this.getSender().tell("akka.apply.actor.Actor1", this.getSelf());

}else

this.unhandled(msg);

}

 

}

 

修改 MasterActor :  onReceive方法

@Override

public void onReceive(Object msg) throws Exception {

System.out.println(""+this.getClass().getSimpleName()+"::sender:"+this.getSender()+"  ::self:"+this.getSelf());

//System.out.println(this.getSelf());

if (msg instanceof String){

if ( true)

return;

String s = (String) msg;

if ( s.equals("start")){

 

ActorRef a1 = this.getContext().actorOf(Props.create(Actor1.class), "Actor1");

ActorRef a2 = this.getContext().actorOf(Props.create(Actor2.class), "Actor2");

a1.tell("akka.apply.actor.Actor1", a2);

}

}else

this.unhandled(msg);

 

}

 

虽然启始消息是MasterActor的方法中定义发送的,但 a1.tell("akka.apply.actor.Actor1", a2); 定义了 接受方是 Actor1,发送方是 Actor2,(相当于master在方法中,发给a1一个消息,告诉a1,这个消息不是我发的是a1发的,虽然这个方式很灵活,但现在还不太理解这么设计的目的。)这两者又互相将消息发给对方,执行结果如下:

MasterActor::sender:Actor[akka://test/user/master#-2085464928]  ::self:Actor[akka://test/user/master#-2085464928]

Actor1::sender:Actor[akka://test/user/master/Actor2#1627072295]  ::self:Actor[akka://test/user/master/Actor1#-2006482622]

Actor2::sender:Actor[akka://test/user/master/Actor1#-2006482622]  ::self:Actor[akka://test/user/master/Actor2#1627072295]

Actor1::sender:Actor[akka://test/user/master/Actor2#1627072295]  ::self:Actor[akka://test/user/master/Actor1#-2006482622]

Actor2::sender:Actor[akka://test/user/master/Actor1#-2006482622]  ::self:Actor[akka://test/user/master/Actor2#1627072295]

Actor1::sender:Actor[akka://test/user/master/Actor2#1627072295]  ::self:Actor[akka://test/user/master/Actor1#-2006482622]

Actor2::sender:Actor[akka://test/user/master/Actor1#-2006482622]  ::self:Actor[akka://test/user/master/Actor2#1627072295]

........

 

 

修改Actor1的方法,将消息发给MasterActor,

 

public void onReceive(Object msg) throws Exception {

System.out.println(""+this.getClass().getSimpleName()+"::sender:"+this.getSender()+"  ::self:"+this.getSelf());

//System.out.println(this.getSelf());

if (msg instanceof String){

String s = (String) msg;

ActorRef m = this.getContext().actorOf(Props.create(MasterActor.class), "master");

m.tell("return Master", this.getSelf());

//this.getSender().tell("akka.apply.actor.Actor2", this.getSelf());

//this.getSelf().tell(s, getSender());

//ActorRef a = this.getContext().actorOf(Props.create( Class.forName(s)), s);

//a.tell(s, a);

//this.getContext().stop(a);

}else

this.unhandled(msg);

}

运行结果:

MasterActor::sender:Actor[akka://test/user/master#-1998415775]  ::self:Actor[akka://test/user/master#-1998415775]

Actor1::sender:Actor[akka://test/user/master/Actor2#-63616032]  ::self:Actor[akka://test/user/master/Actor1#-795382741]

MasterActor::sender:Actor[akka://test/user/master/Actor1#-795382741]  ::self:Actor[akka://test/user/master/Actor1/master#2020933176]

 

发现MasterActor的path发生了变化,AKKA的Actor采用树结构,父类的Actor负责管理子类,当子类由于错误关闭,父类会重建子类发送消息(后面找机会验证一下)

 

停止消息

 

在MasterActor的onReceive方法中添加

this.getContext().stop(a1);

 

但发现并不是消息传递到Action1的时候 就会停止,而是会在Actor1 Actor2相互交互多次,而且次数不固定,说明这个停止是异步的。

 

 

 

 

  • AKKA笔记2
            
    
    博客分类: javaspark  
  • 大小: 15.5 KB
  • AKKA笔记2
            
    
    博客分类: javaspark  
  • 大小: 33.1 KB