从数据库初始化actor的最佳实践方法是什么?

时间:2021-06-04 20:05:59

I have a top level actor (under the guardian), called Groups which on startup needs to load the list of groups from the database and create a bunch of child actors based on those groups in the database.

我有一个*演员(在守护者下),称为组,在启动时需要从数据库加载组列表,并根据数据库中的这些组创建一组子actor。

I have placed the database load code inside of the preStart function as I don't want any messages to be processed before the groups are loaded.

我已将数据库加载代码放在preStart函数中,因为我不希望在加载组之前处理任何消息。

Currently my Groups actor looks like this;

目前我的群组演员看起来像这样;

var groups: Map[String, ActorRef] = Map()

override def preStart() = {
  groups = getGroupsFromDB() map createGroup
}

def createGroup(pair: (String, Long)) = {
  val (name, id) = pair
  val group = context.actorOf(Props(new Group(id, name)), name = name)

  name -> group
}

However I don't believe this is the best way to handle this, as what happens if the database server is not available? So what is the best pratice way of handling data initialization from a database?

但是,我不相信这是处理这种情况的最佳方法,如果数据库服务器不可用会发生什么?那么从数据库处理数据初始化的最佳实践方法是什么?

1 个解决方案

#1


1  

The Akka documentation explains how to supervise top level actors for fault-tolerance.

Akka文档解释了如何监督*演员的容错能力。

You can apply the principles there to manage the exceptions you may find if the DB is not available.

您可以在那里应用原则来管理在DB不可用时可能发现的异常。

#1


1  

The Akka documentation explains how to supervise top level actors for fault-tolerance.

Akka文档解释了如何监督*演员的容错能力。

You can apply the principles there to manage the exceptions you may find if the DB is not available.

您可以在那里应用原则来管理在DB不可用时可能发现的异常。