Receive message either from ServerHealthKafka or ServerHealthSap type

Hi all

I have the following type definition:

object ServerMonitoring {

  sealed trait ServerHealthKafka

  case object KafkaInactiveConfirmed extends ServerHealthKafka

  case object KafkaActiveConfirmed extends ServerHealthKafka

  sealed trait ServerHealthSap

  case object SapInactiveConfirmed extends ServerHealthSap

  case object SapActiveConfirmed extends ServerHealthSap

}

and I would like to create an actor that accept either ServerHealthKafka or ServerHealthSap .

I’ve started to create but I do not know how to continue:

object DetectorStateMachine {

  case class State(kafka: ServerHealthKafka, sap: ServerHealthSap)

  def create(informant: ActorRef[InformantEvent]): Behavior[State] =
    Behaviors.setup { context =>

      context.log.info(s"=============> Start DetectorStateMachine <=============")

      def loop(state: State): Behavior[State] = {

        Behaviors.receiveMessage {
          case KafkaActiveConfirmed =>
          case KafkaInactiveConfirmed =>
          case SapActiveConfirmed
          case SapInactiveConfirmed =>
        }

      }

      loop(State(KafkaInactiveConfirmed, SapInactiveConfirmed))


    }

}

Do I have to use here the message adapter?

Thanks

Sounds like a good use case of message adapter. Something like this:

object DetectorStateMachine {
  import ServerMonitoring._

  trait Command

  private case class ServerHealthKafkaWrapper(kafka: ServerHealthKafka) extends Command
  private case class ServerHealthSapWrapper(kafka: ServerHealthSap) extends Command

  case class State(kafka: ServerHealthKafka, sap: ServerHealthSap)

  def create(informant: ActorRef[InformantEvent]): Behavior[Command] =
    Behaviors.setup { context =>
      context.log.info(s"=============> Start DetectorStateMachine <=============")

      val kafkaAdapter: ActorRef[ServerHealthKafka] =
        context.messageAdapter[ServerHealthKafka](ServerHealthKafkaWrapper.apply)
      val sapAdapter: ActorRef[ServerHealthSap] =
        context.messageAdapter[ServerHealthSap](ServerHealthSapWrapper.apply)

      // TODO: initiate the interaction with Kafka using the `kafkaAdapter` as `replyTo`
      // TODO: initiate the interaction with SAP using the `sapAdapter` as `replyTo`

      def loop(state: State): Behavior[Command] = {

        Behaviors.receiveMessage {
          case ServerHealthKafkaWrapper(KafkaActiveConfirmed) =>
            loop(state.copy(kafka = KafkaActiveConfirmed))
          case ServerHealthKafkaWrapper(KafkaInactiveConfirmed) =>
            loop(state.copy(kafka = KafkaInactiveConfirmed))
          case ServerHealthSapWrapper(SapActiveConfirmed) =>
            loop(state.copy(sap = SapActiveConfirmed))
          case ServerHealthSapWrapper(SapInactiveConfirmed) =>
            loop(state.copy(sap = SapInactiveConfirmed))
        }

      }

      loop(State(KafkaInactiveConfirmed, SapInactiveConfirmed))

    }

}
1 Like

Awesome. Thanks a lot for your help.