如何在Akka中扩展超级演员的行为

编程入门 行业动态 更新时间:2024-10-28 21:21:44
本文介绍了如何在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中扩展超级演员的行为

本文发布于:2023-11-25 03:45:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:演员   如何在   Akka

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!