Calling 3rd party REST service

Hi,

I need to make a REST call to a 3rd party application besides calling my own microservices in a transaction flow implemented in Saga.

Should I create a command side microservice to encapsulate the 3rd party service and make the REST call in command handler. If the call succeed (e.g. status 200) then I call
AggregateLifecycle#apply(new SucceedEvent) otherwise call apply(new FailedEvent)?

Regards,
Nick

Hi Nick,

It would indeed be beneficial to have a small component/service which service as the context mapping between your application and the third party REST service.
This piece of code would translate your API (your commands, events, queries) to their REST knowledge and vice versa, effectively working as an anti-corruption layer.
Whether this should be a command, event or query handler depends on the kind of request you are doing if you ask me.

What I would steer away from is using the AggregateLifecycle in this case.
This anti-corruption component would not act as an Aggregate, hence publishing events through the AggregateLifecycle#apply method wouldn’t get you the desired effect.
If you want to publish events outside of the context of an Aggregate, you’d better use the EventBus or EventGateway (i.e. a convenience API around the EventBus).

Hope this helps!

Cheers,
Steven

Hi Steven
Thanks for the idea of anti-corruption pattern.

“Whether this should be a command, event or query handler depends on the kind of request you are doing if you ask me.”

Assuming I choose to implement it as command which is triggered from my Saga (for example, commandGateway.send(prepareShipmentCommand) and within the

@CommandHandler
public Shipment(PrepareShipmentCommand command) throws InterruptedException {

// call REST api

AggregateLifecycle.apply(new ShipmentPreparedEvent(id, command.getOrderId(), invoice));

}

Without AggregateLifecycle#apply for event publishing, how can I orchestrate the whole transaction flow in my Saga?

@SagaEventHandler(associationProperty = “orderId”)
public void on(ShipmentPreparedEvent event) {

}

Regards,
Nick

Hi Nick,

If you would be dispatching a command, you can react on the result of the command handling directly on the dispatching end.

The CommandGateway#send(Object) method returns you a CompletableFuture, which can either resolve successfully or exceptionally.
Every thorough complex transaction management (like sagas) needs to be able to coop with both scenarios.

So, the orchestration here lies in the Saga, not so much in the Aggregate’s Command Handler.
Added, from the perspective of the Saga it doesn’t care about the fact an Aggregate is handling that command.
It just simplest has the requirement that somebody/something needs to perform that command.
As such, there is no real need to use the AggregateLifecycle for coordination in the Saga, as those are two distinct things.

Hope this helps you out further Nick.

Cheers,
Steven

PS. It is important to note that this mailing list will be discontinued as specified in this thread.
The thread also specifies where to look further for help when it comes to Axon.