New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Batch Command / Multi Command Container for single Aggregate #991
Comments
I maybe found a solution, but I'm not sure at all if this behavior is intended. Some feedback would be highly appreciated! @Aggregate
class MyAggregate {
@CommandHandler
public void handle(FooCommand cmd) {
// validate cmd
apply(new FooEvent(...));
}
@CommandHandler
public void handle(BarCommand cmd) {
if("1".equals("1")) { // simulate validation exception
throw new RuntimeException("??");
}
apply(new BarEvent(...));
}
@CommandHandler
public void handle(
MyAggregateBatchCommand cmd,
CommandGateway commandGateway,
UnitOfWork<?> unitOfWork) {
try {
FooCommand cmd1 = cmd.getFooCommand();
cmd1.setTargetAggregateIdentifier(cmd.getTargetAggregateIdentifier());
cmd1.setTargetAggregateVersion(cmd.getTargetAggregateVersion());
commandGateway.sendAndWait(cmd1); // important to use "andWait" !
BarCommand cmd2 = cmd.getBarCommand();
cmd2.setTargetAggregateIdentifier(cmd.getTargetAggregateIdentifier());
cmd2.setTargetAggregateVersion(cmd.getTargetAggregateVersion() + 1);
commandGateway.sendAndWait(cmd2); // important to use "andWait" !
} catch(Exception ex) {
unitOfWork.rollback(ex);
}
}
} This works as expected. I guess Axon has some kind of If I remove the Is this the way to go? Or what can go wrong? Will it load the Aggregate only once? (I'm not sure how Axon internally handles this) |
Batch commands are usually best handled in an "external Command Handler". I.e. a Command Handler that is not inside an Aggregate, but rather on a "singleton" bean. Since this seems to be a question, rather than an issue, I'm closing it here. You can refer to the mailinglist (axonframework@googlegroups.com) for questions, or comment here if you feel this is an issue. |
For our API it would be nice to have some kind of
BatchCommand
. This is basically a command, which contains a List of commands for a single Aggregate.If you have fine granular commands (and no cache), this would provide some huge performance improvements, because the aggregate must only be loaded once. And the resulting events can be persisted with a single write operation.
At the moment I use a custom
MyAggregateBatchCommand
, which gets handled byMyAggregate
and this then calls the matching@CommandHandler
s on this Aggregate. Something like this:But this has a few issues:
@CommandHandler
requires additional parameters like@MetaData
or some Spring Bean injection, theMyAggregateBatchCommand
command handler will become really huge and ugly.MyAggregateBatchCommand
command handler, which makes the code even more coupled, and ugly.As far as I've seen
CommandBus
andCommandGateway
only provide the execution of a single command. This makes sense, because each command can contain a different@TargetAggregateIdentifier
. Though I guess, I won't make sense to add adispatchBatch
orsendBatch
method.Basically all I need is to dispatch more commands from the
MyAggregateBatchCommand
command handler, and stay within the sameUnitOfWork
(as far as I understood the concept) in order to have a "all or nothing" transaction.So my question is: How can this be achieved?
The text was updated successfully, but these errors were encountered: