如何在Akka中扩展超级演员的行为 [英] How extend behaviour of super actor in akka

查看:91
本文介绍了如何在Akka中扩展超级演员的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用akka actor实现CRUD操作。我是akka的新手,所以不知道akka演员的设计基础。

I want to implement CRUD operation using akka actor. I am a new in akka so dont know the designing fundamentals of akka actors.

我想在多个子演员中分享akka演员的行为。

I want to share the behaviours of akka actors in multiple sub actors.

例如,我想保存和删除学生,老师和其他实体。

Fir example i want to save and delete student , teacher and other entity.

我为StudentDao.scala创建了演员

I have created actor for StudentDao.scala

class StudentDao extends Actor with ActorLogging{

 override def Receive = {

    case Add(student) =>
      // Add to database
    case Delete =>
      //Delete from database     
   // Some other cases related to Student entity
  }
}
case object StudentDao{
  case class Add(user : Student)
  case class Delete(id : String)
}

与我相同为TeacherDao.scala提供演员

Same I have actor for TeacherDao.scala

class TeacherDao extends Actor with ActorLogging{

 override def Receive = {

    case Add(teacher) =>
      // Add to database
    case Delete =>
      //Delete from database      
    // Some other cases related to teacher entity

  }
 }

object TeacherDao{
  case class Add(user : teacher)
  case class Delete(id : String)
}

我想为两个dao抽象删除方法。
所以我创建了BaseDao.scala

I want to abstract delete method for both dao. So i have create BaseDao.scala

class BaseDao extends Actor with ActorLogging{

  override def Receive = {

    case Delete =>
      //Delete from database   dao.delete
  }

我如何抽象

推荐答案

orElse 是扩展参与者行为的方法,因为演员的 Receive 只是 PartialFunction [Any,Unit] 的别名。下面是用例的具体说明。

orElse is the way to extend actor behaviors, because an actor's Receive is simply an alias for PartialFunction[Any, Unit]. Below is a concrete illustration with your use case.

首先,定义必须与参与者混合的特征中的基本行为。为了避免重复,请将 Delete 案例类移到该特征的伴随对象中。

First, define the base behavior in a trait that must be mixed in with an actor. To avoid duplication, move the Delete case class into this trait's companion object.

trait BaseDao { this: Actor with ActorLogging =>
  import BaseDao._

  def baseBehavior: Receive = {
    case Delete(id) =>
      log.info(s"Deleting $id from db")
      // delete from db
  }
}

object BaseDao {
  case class Delete(id: String)
}

然后,混合以上特征与您的其他演员并通过 orElse 链接行为。请注意,我创建了虚拟 Student Teacher 案例类,以便可以编译此代码。 StudentDao

Then, mix in the above trait into your other actors and chain the behaviors with orElse. Note that I created dummy Student and Teacher case classes so that this code would compile. StudentDao:

class StudentDao extends Actor with ActorLogging with BaseDao {
  import StudentDao._

  def studentBehavior: Receive = {
    case Add(student) =>
      log.info(s"Adding student: $student")
    // some other cases related to Student
  }

  def receive = studentBehavior orElse baseBehavior
}

object StudentDao {
  case class Add(user: Student)
}
case class Student(name: String)

TeacherDao

class TeacherDao extends Actor with ActorLogging with BaseDao {
  import TeacherDao._

  def teacherBehavior: Receive = {
    case Add(teacher) =>
      log.info(s"Adding teacher: $teacher")
    // some other cases related to Teacher
  }

  def receive = teacherBehavior orElse baseBehavior
}

object TeacherDao {
  case class Add(user: Teacher)
}
case class Teacher(name: String)

这篇关于如何在Akka中扩展超级演员的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆