Slide 121
Slide 121 text
@Saga
class SecureStockIssueSaga {
@Transient
@Autowired
private lateinit var commandGateway: CommandGateway
private lateinit var sagaState: SecureStockIssuerSagaState
@StartSaga
@SagaEventHandler(associationProperty = "secureStockId")
fun on(event: SecureStockEvents.SecureStockIssued, @MessageIdentifier messageIdentifier: String) {
sagaState = SecureStockIssuerSagaState(event.secureStockId, secureItemIdToNr = event.itemIdToNr)
log("[START SAGA] Handling the Secure Stock Issued Event within the Saga ($sagaState).")
sagaState.secureItemIdToNr.keys.forEach {
SagaLifecycle.associateWith("itemId", it.asString())
}
val commands = event.itemIdToNr.map { (itemId, nr) ->
ItemAggregateProtocol.SecureItem(itemId, event.secureStockId, nr, messageIdentifier)
}
val replies = commands.map { commandGateway.sendAndWait(it) }
if (replies.any { it is ItemAggregateProtocol.SecureItemFailed }) {
val secureSucceededList = replies.filterIsInstance()
compensateSecureStockIssued(secureSucceededList)
}
}
}