Axonframework, come utilizzare MessageDispatchInterceptor con reattivi repository

0

Domanda

Ho letto il set base di convalida di consistenza blog e voglio convalidare con spedizione interceptor. Seguo l'esempio, ma io uso reattiva repository e non funziona per me. Ho provato sia isolato e non isolato. con il blocco genera l'errore, ma senza blocco non eseguire nulla. ecco il mio codice.

class SubnetCommandInterceptor : MessageDispatchInterceptor<CommandMessage<*>> {

  @Autowired
  private lateinit var privateNetworkRepository: PrivateNetworkRepository

  override fun handle(messages: List<CommandMessage<*>?>): BiFunction<Int, CommandMessage<*>, CommandMessage<*>> {
    return BiFunction<Int, CommandMessage<*>, CommandMessage<*>> { index: Int?, command: CommandMessage<*> ->
      if (CreateSubnetCommand::class.simpleName == (command.payloadType.simpleName)){
        val interceptCommand = command.payload as CreateSubnetCommand
        privateNetworkRepository
          .findById(interceptCommand.privateNetworkId)
          // ..some validation logic here ex.
          // .filter { network -> network.isSubnetOverlap() }
          .switchIfEmpty(Mono.error(IllegalArgumentException("Requested subnet is overlap with the previous subnet.")))
          // .block() also doesn't work here it throws error
         // block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-
      }
      command
    }
  }
}
axon axon-framework kotlin
2021-11-24 06:18:54
1

Migliore risposta

3

La sottoscrizione di un reattivo repository all'interno di un messaggio dispatcher non è veramente raccomandato e può portare a comportamento strano come subalterno ThreadLocal (utilizzato da Axox) non è adatto per essere utilizzato in programmazione reattiva

Invece, check-out Assone Reattivo Estensione e reattiva intercettori sezione.

Per esempio, cosa si potrebbe fare:

reactiveCommandGateway.registerDispatchInterceptor(
        cmdMono -> cmdMono.flatMap(cmd->privateNetworkRepository
      .findById(cmd.privateNetworkId))
.switchIfEmpty(
Mono.error(IllegalArgumentException("Requested subnet is overlap with the previous subnet."))
.then(cmdMono)));
2021-11-24 13:26:24

grazie per il consiglio, btw, non vedo ReactorCommandBus in estensione devo usare ReactroCommandGateway invece?
Patrick

sì, ci sono solo reattiva gateway supportato
schananas

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................