I’ve gathered from the docs that this is the preferred approach but it appears that all of the examples use the COmmandBus directly.
If you use Spring:
Otherwise:
CommandGateway commandGateway = new DefaultCommandGateway(commandBus);
You can also use your own interface.
In Spring:
Or:
GatewayProxyFactory factory = new GatewayProxyFactory(commandBus);
MyGatewayInterface gateway = factory.createGateway(MyGatewayInterface.class);
Cheers,
Allard
When should I use a CommandBus and when should I use a CommandGateway ? Any guidelines or best practices ?
You should use a CommandGateway if you like to have a simpler API towards the Command Bus. The gateway also allows you to configure a “retry-mechanism” which resends a command when it fails under certain conditions.
Otherwise, both approaches are very much the same. It’s just a different API, where the CommandBus requires you to wrap a Command object in a CommandMessage, and the Gateway doesn’t.
Cheers,
Allard