ufasoli
ufasoli

Reputation: 1068

Reactor and Spring state machine execute Mono then Flux sequentially

I have the following method that is called the method does not have a return value but gets an object from another service metadataService that returns a Mono and then some processing is done with the object returned by the Mono, and once this is done I need to send a signal to the StateMachine so that next step can be triggered.

         public void safeExecute(
             StateContext<StateMachineStates, StateMachineEvents> context){
        metadataService.getMetadata(context.getId())
            .doOnSuccess(metadata ->{
              // perform some operation here

              context.getStateMachine()
             // returns a Flux<StateMachineEventResult<S, E>>   
             .sendEvent(Mono.just(
                  MessageBuilder.withPayload(Events.E_GOTO_NEXT_STATE).build()
                 ))
              .subscribe()
            })
           .
        }

I get however the warning :

Calling 'subscribe' in non-blocking context is not recommended

That I can apparently resolve by calling publishOn(Schedulers.boundedElastic()) however the warning is still there.

My question is how can you send the event to the StateMachine only after the code block within onSuccess is done? I tried using concatWith or doFinally but I do not have enough good understanding of reactive programming.

My current stack :

Upvotes: 0

Views: 1410

Answers (1)

Douglas Santos
Douglas Santos

Reputation: 595

I don't think you should use subscribe here, your goal its to send the mono of the Message, you need to receive this message as a mono of your message somewhere, and there you would use the contents of it.

monoMessage.map(message -> doSomethingWithYourMessage(message))

Upvotes: 0

Related Questions