Hello Everyone,
In my EventSourcedBehavior, I’d like to call some functions that return a Future in the command handler.
Based on the example in the documentation, I start using pipeToSelf to get the Future response back into the running actor (behavior)
Behaviors.setup[Command]{ context =>
EventSourcedBehavior
.withEnforcedReplies(
persistenceId,
MyState("new"), 0),
commandHandler = { (state, command) =>
commandHandler(context, state, command)
},
eventHandler = eventHandler
)
But getting the context used for pipeToSelf in the behavior is still quite a puzzle.
val futureResult = callSomefunctionThatGivesAFuture(....)
context.pipeToSelf(futureResult) {
case Success(answer: String) => WrappedUpdateResult(UpdateSuccess(answer), replyTo)
case Failure(e) => WrappedUpdateResult(UpdateFailure("something", e.getMessage), replyTo)
}
but gives me compilation errors:
found : scala.util.Try[String] => Any
[error] required: scala.util.Try[String] => _$2
Looking at the sample, it expects to return the behavior as the event sourced functions return an Effect, so this may not be the right direction at all.
Another option is to have a combination of behavior and effects ?
So next to the fact that some hints on the error, some advice on the best approach would be really appreciated.
Kind regards,
Olger