AKKA Actor creation

Actor class definition 

Actor class needs to inherit AbstractActor class

Achieve createReceive method, various types of binding receive different types of messages corresponding to the actor on the business logic processing

The default provides ReceiveBuilder class auxiliary create Receive

ActorOf call to return the instance ActorRef. This is the handle Actor instance, also it interacts with a unique approach.

ActorRef is immutable, and it represents Actor and have one to one relationship. ActorRef also serializable, the sequence of transmission over the network, and use it on the remote host, and it still represents the same Actor on the original node in the network.

The hierarchy Actor

Actor similar hierarchy tree pattern

Who who create management principles:

ActorSystem created by the ActorSystem is responsible for monitoring and management (restart, abnormal, recovery, etc.)
the Actor to create another Actor, regarded as the founder of a parent, is responsible for monitoring and management it creates out of the actor

 

Props 

Props to create Actor configuration options, it is recommended to provide a common props to create a method actor class

note:

1, actor instance of the same name created akka cluster can not be repeated, otherwise an exception will be reported InvalidActorNameException

2, ActorSytem same cluster, each node ActorSytem name must be the same

3, do not allow yourself to create a new actor instance
if you create a new Actor Actor direct way of example an error will be reported ActorInitializationException

 

Example:

<!-- Gradle -->
dependencies {
  compile group: 'com.typesafe.akka', name: 'akka-actor_2.12', version: '2.5.21'
}

 

package akka.demo.actor

import akka.actor.AbstractActor
import akka.actor.ActorRef

import akka.actor.Props
import akka.japi.pf.ReceiveBuilder
import org.slf4j.LoggerFactory

/**
 ** created by tankx
 ** 2019/9/10
 **/
class HelloActor(val name: String) : AbstractActor() {

  //创建子actor
  private val childActor: ActorRef = context.actorOf(ChildActor.props())

  companion object {
    private val log = LoggerFactory.getLogger(HelloActor::class.java)
   //提供静态通用对外的props  
    fun props(name: String): Props {
      //return Props.create(HelloActor::class.java, name)//默认方式
      return Props.create(HelloActor::class.java) {
        HelloActor(name)
      }
    }
  }

  override fun preStart() {
    log.info("preStart")
    super.preStart()
  }

  override fun postStop() {
    log.info("postStop")
    super.postStop()
  }

  override fun createReceive(): Receive {
    return ReceiveBuilder.create().matchAny(::onReceive).build()
  }

  fun onReceive(msg: Any) {

    log.info("$name say: $msg")
    log.info("sender:{}", sender.toString())


  }

}
package akka.demo.actor

import akka.actor.AbstractActor

import akka.actor.Props
import akka.japi.pf.ReceiveBuilder
import org.slf4j.LoggerFactory

/**
 ** created by tankx
 ** 2019/9/10
 **/
class ChildActor : AbstractActor() {

  private val log = LoggerFactory.getLogger(ChildActor::class.java)

  companion object {
    fun props(): Props {
      return Props.create(ChildActor::class.java)
    }
  }

  override fun preStart() {
    log.info("preStart")
    super.preStart()
  }

  override fun postStop() {
    log.info("postStop")
    super.postStop()
  }

  override fun createReceive(): Receive {
    return ReceiveBuilder.create().matchAny(::onReceive).build()
  }

  fun onReceive(msg: Any) {
    log.info("onReceive: $msg")
  }

}

 

 Creating ACTOR, and a message

val system = ActorSystem.create("akka-system")

val actorRef = system.actorOf(HelloActor.props("aa"), HelloActor::class.java.simpleName)

actorRef.tell("hi world", ActorRef.noSender()) //给actor发消息

 

 Dependency Injection

If there is dependency injection, we need to pass dependencies to build Actor

Example:

Package akka.demo.actor 

Import akka.actor.Actor
 Import akka.actor.IndirectActorProducer 

/ ** 
 ** Created by tankx 
 ** 2019/9/11 
 ** this manner if the factory class dependency injection can be used to create the Actor 
 * * / 
class ActorFactory (var applicationContext: String): {IndirectActorProducer 


  the override Fun ActorClass (): class <OUT the Actor> {
     return HelloActor :: class .java 
  } 

  the override Fun Produce (): the Actor { 
    return HelloActor (applicationContext) 
  } 


}

 

Created:

val actorFactoryRef = system.actorOf(Props.create(ActorFactory::class.java, "aaa"), "aaa")
  actorFactoryRef.tell("hi factory", ActorRef.noSender())

 

to sum up:

 Actor AKKA create the need for strict follow the recommended way to create, to avoid breaking Actor package.

 

Guess you like

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