0

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.

  • Make sure your `associationProperty` is correct on your handlers and for the associations you set from previous handlers. Also are you sure your `ProductReservedEvent` is properly published? – Vaelyr Jun 11 '22 at 07:55
  • Could you share the code responsible for handling the ReserveProductCommand? – KDW Jun 12 '22 at 18:58

1 Answers1

0

I think the issue is that an @Eventhandler is called after an AggregateLifecycle.apply() and the commandGateway.send(reserveProductCommand); will trigger an @Command annotated method.

So there is a mixup with the annotations in the expected flow.

Jan
  • 61
  • 6