I have 2 events OrderCreatedEvent and ProductReservedEvent. Saga event handlers are as follows:
@StartSaga
@SagaEventHandler(associationProperty = "orderId")
public void handle(OrderCreatedEvent event){
LOGGER.info("Saga Started - OrderCreatedEvent");
ReserveProductCommand reserveProductCommand = ReserveProductCommand.builder()
.orderId(event.getOrderId())
.productId(event.getProductId())
.orderId(event.getOrderId())
.quantity(event.getQuantity())
.userId(event.getUserId())
.build();
LOGGER.info("Created Reserve Product Command");
// SagaLifecycle.associateWith("userId", event.getUserId()); //not working
LOGGER.info("Will call next event in saga");
commandGateway.send(reserveProductCommand);
}
@SagaEventHandler(associationProperty = "orderId")
public void handle(ProductReservedEvent productReservedEvent){
LOGGER.info("Saga Step - ProductReservedEvent");
//Process under payment
String userId = productReservedEvent.getUserId();
FetchUserPaymentDetailsQuery fetchUserDetails = new FetchUserPaymentDetailsQuery(userId);
User userPaymentDetails = null;
try {
LOGGER.info("Querying User Service with id: {}",userId);
userPaymentDetails = queryGateway.query(fetchUserDetails, ResponseTypes.instanceOf(User.class)).join();
}catch (Exception exception){
LOGGER.error(exception.getLocalizedMessage());
//start compensating transaction
return;
}
if(userPaymentDetails == null){
//start compensating transaction
return;
}
LOGGER.error("Fetched user details for user {}",userPaymentDetails.getUserId());
}
The problem is the handle() method of "ProductReservedEvent" is not getting invoked from "handle(OrderCreatedEvent event)" method.