AKKA Inbox Inbox

Due

Get ActorRef can send a message to the actor, but can not receive multiple replies, do not know whether the actor stopped

Inbox Inbox is to solve these two problems arise

 

Examples

Package akka.demo.actor 

Import akka.actor. *
 Import java.time.Duration 

/ ** 
 ** Created by tankx 
 ** 2019/9/12 ask the actor or tell communication mode can not support multiple responses received and observation of other actor life cycle, the raw inbox application 
 * * / 
Fun main () { 


  Val System = ActorSystem.create ( "SYS-Box" ) 

  Val helloActor = system.actorOf (HelloActor.props ( "Tom" )) 
  helloActor.tell ( " hi " , ActorRef.noSender ()) 

  Val Box = Inbox.create (System) 
  box.send (helloActor, " How are you! " ) 

  box.watch (helloActor) //Monitoring the life cycle of the actor, as actor stop, receive Terminated message will receive 

  helloActor.tell (PoisonPill.getInstance (), ActorRef.noSender ()); // poisoned actor 
  
  the while ( to true ) {
     the try { 

      Val receiveObj = Box. the receive (Duration.ofSeconds (2)) // may have received a message back actor 
      the println ( "the receive Box:" + receiveObj) 

      IF (iS receiveObj Terminated) {
         BREAK 
      } 
    } the catch (E: Exception) { 
      the println (e.message ) 
    } 
  } 



  the Thread.sleep ( 2000 ) 
  System.exit ( . 1 ) 

}

 

Guess you like

Origin www.cnblogs.com/tankaixiong/p/11511278.html