Hi again.
I’m having problems in sending commands from inside events. Please remember this is just a prototype for a POC with which I’m messing around, so I’m most probably making many mistakes. In this case I’m doing this:
A controller gets a request from a client that triggers a command:
`
TestStartCommand c1 = new TestStartCommand(id);
commandGateway.sendAndWait(c1);
`
there is a Aggregate accepting this command that triggers a event, and a event handler that handles it:
`
@CommandHandler
public TestService(TestStartCommand command, RestTemplate restTemplate, CommandGateway commandGateway) {
this.restTemplate = restTemplate;
this.commandGateway = commandGateway;
this.id = command.getId();
apply(new TestStartedEvent(command.getId(), command.getData()));
}
@EventHandler
public void on(TestStartedEvent event) {
GetProcessCommand command = new GetProcessCommand(event.getId(),event.getData());
commandGateway.sendAndWait(command); <----------------- EventStreamNotFoundException: Aggregate of type [TestService] with identifier cannot be found
apply(new GotProcessEvent(id, result));
}
`
This happens because the second command tries to load the aggregate that was never saved
`
private T loadAggregate(CommandMessage<?> command) {
VersionedAggregateIdentifier iv = commandTargetResolver.resolveTarget(command);
return repository.load(iv.getIdentifier(), iv.getVersion()); <------------------------
}
public DomainEventStream readEvents(String type, Object aggregateIdentifier) {
try {
if (!eventFileResolver.eventFileExists(type, aggregateIdentifier)) {
throw new EventStreamNotFoundException(type, aggregateIdentifier); <------------------------------
}
`
So what am I’m doing wrong here? The problem is the Aggregate not being saved after the aggregate constructor, or this kind of “nesting” should not be used?
I did the search for this, and I found in [1]
The UnitOfWork is nesting aware. That means it is possible to create a single transaction that deals with commands that cause other commands (via events, usually)
but this it seems was in version 1 of Axon (BTW, the forum is hard to search because no posts are tagged with version info)
Note that my “real” intention is to model Sagas like this, but I thought it was better to start with Aggregates (concept that I’m not entirely familiar with).
Thanks for helping.
[1] https://groups.google.com/d/msg/axonframework/md4kvkkqx3E/9qDllswgEjUJ
