It is possible that several received messages match in Akka FSM

Is it possible to match multiple messages in one case / Event using Akka FSM event function. Meaning I would like to collapse two events into one.

case Event(response : GoodResponse, session:CustomerSession) =>
case Event(response : Timeout, session:CustomerSession) =>
case Event(response : Rejected, session:CustomerSession) => 

      

will become something like

case Event(response : GoodResponse, session:CustomerSession) =>
case Event(response : Timeout || response : Rejected , session:CustomerSession) =>

      

Both error cases call the same handling function and return the same result, so I'd rather advertise this fact with event handling

+3


source to share


2 answers


You can simply have Timeout

and Rejected

implement the same feature and match that feature.

If, as you write in the comment, you cannot change the Timeout and Rejected types / implementation, you can use a case that simply ignores the type. This should work if you've done all the other stuff before. I would look like



case Event(response : GoodResponse, session:CustomerSession) =>
case Event(response : _, session:CustomerSession) =>

      

I'm not sure if I have the syntax on the right, but something along this line should work.

+6


source


Another option is to define a custom extractor, for example:

object RejectedOrTimeout{
  def unapply(value:Any) = {
    if (value.isInstanceOf[Rejected] || value.isInstanceOf[Timeout]) Some(value) 
    else None
  }
}

      



And then use it in your conformance assertion like this:

case Event(RejectedOrTimeout(response), session:CustomerSession) =>

      

+1


source







All Articles