How to receive signals if all you've got is a Behavior instance?

If an API call returns a Behavior instance (that under-the-covers is really a Receive instance) is there any way to call receiveSignals on it (or rewrap it such that one can) without resorting to a cast?

The situation I have - in Akka 2.6 the return type of SpawnProtocol.appy() is Behavior even though the implementation returns the result of calling Behaviors.receive (which returns a Recieve instance).

So I can’t call receiveSignal on the result without resorting to a cast like so:

val behavior: Behavior[SpawnProtocol.Command] = SpawnProtocol()
val receive = behavior.asInstanceOf[Receive[SpawnProtocol.Command]]

receive.receiveSignal {
  case (_, Terminated(ref)) =>
    log.warn(s"$ref terminated")
    same
  case (_, ChildFailed(ref, cause)) =>
    log.error(s"$ref failed: ${cause.getMessage}", cause)
    same
}

Is there a nicer way to do this? Can I avoid having to know about the underlying implementation? Can I (e.g. using one of the Behaviors.receiveX functions) somehow rewrap what I see as a Behavior such that I get essentially the same Receive object but with the right type?

You could use Behaviors.intercept and for the BehaviorInterceptor implement a BehaviorSignalInterceptor.

1 Like