我有一组运行TCP服务器的设备。在我的应用中,我想为每个设备维护一个TCP连接。为了从故障中恢复,每个连接都是受监督的Akka角色,从中央角色TcpClientManager
开始。
无论当前上下文如何,我也想从REST调用中重新启动这样的连接。
这就是为什么我要从父actor(即TcpClientManager` actor)中重新启动它。在这种情况下,我似乎只能停止 supervising 演员。那是对的吗?应该如何重新启动基础参与者?
class TcpClientManager(devices: Seq[Device]) extends Actor with ActorLogging {
var idToActor = Map.empty[String, ActorRef]
override def preStart(): Unit =
devices(device => {
val tcpClientProps =
TcpClient.props(device)
val tcpClientSupervisorActor = BackoffSupervisor.props(
Backoff.onStop(
childProps = tcpClientProps,
childName = s"tcpCient-${device.id}",
minBackoff = 1 seconds,
maxBackoff = 5 seconds,
randomFactor = 0.2,
maxNrOfRetries = -1
))
idToActor += device.id -> context.actorOf(
tcpClientSupervisorActor,
name = s"tcpClientSupervisorActor-${device.id}")
})
override def receive: Receive = {
case ResetConnection(id) => {
println(s"reset connection for deviceId $id")
idToActor.get(id).foreach(context.stop(_)) // not really... this stops the supervisor
}
}
}