Event sourced aggregate with multiple CreatedEvents in 3.0.3

Hi,
I have simple app with following create-related methods in the aggregate

`
@CommandHandler
public Counter(CounterCreateCommand command, MetaData metaData) {
super();
AggregateLifecycle.apply(new CounterCreatedEvent(command.getId()), metaData);
}

@EventSourcingHandler
public void on(CounterCreatedEvent e) {
this.id = e.getId();
this.counter = 0;
}
`

It looks like I am able to issue the CounterCreateCommand multiple times, resulting into the CounterCreatedEvent being written each time.

I thought the framework will prevent double-creation of aggregate. Or am I missing something obvious here?

Hi Tomáš,

What is the id set to in your CounterCreateCommand?
Can you publish two CounterCreateCommands with identical id’s, or is the ‘issuing of multiple commands’ you refer to creating a CounterCreateCommand with unique ids?
As far as I know, that should result in an exception when your CounterCreatedEvent is trying to be stored, because you’d try storing a second CounterCreatedEvent with an identical aggregate identifier and sequence number.

Hope this helps!

Cheers,

Steven

Well that’s exactly what I expected as well - some kind of error flow. And yes, I am using the same id, the configuration is like this:

`
public static void main(String[] args) throws Exception {
Configurer configurer = DefaultConfigurer
.defaultConfiguration()
.configureEmbeddedEventStore(c -> new InMemoryEventStorageEngine())
.configureAggregate(Counter.class);

Configuration configuration = configurer.buildConfiguration();
configuration.start();

String id = “my-static-id”;
configuration.commandGateway().sendAndWait(new CounterCreateCommand(id));
configuration.commandGateway().sendAndWait(new CounterCreateCommand(id));

configuration.eventStore().readEvents(id).asStream().forEach(System.out::println);
}
`

The console output:

DEBUG [main] (Counter.java:28) - +++ CounterCreateCommand received for my-static-id created 391359742 DEBUG [main] (Counter.java:36) - + counter my-static-id consuming create : 391359742 DEBUG [main] (Counter.java:28) - +++ CounterCreateCommand received for my-static-id created 485041780 DEBUG [main] (Counter.java:36) - + counter my-static-id consuming create : 485041780 org.axonframework.eventsourcing.GenericTrackedDomainEventMessage@20322d26 org.axonframework.eventsourcing.GenericTrackedDomainEventMessage@192b07fd

Hi Tomas,

it seems you’re using the InMemoryEventStorageEngine. That one (for some reason) doesn’t check for uniqueness. It just adds all published events to a list.
You’re better off using the JpaEventStorageEngine with an InMemory database if you want to be “production compatible”.

Cheers,

Allard

Thanks Allard. I am actually using the MongoEventStorageEngine

new MongoEventStorageEngine(new DefaultMongoTemplate(new MongoClient("10.128.75.101"), "axonframework", "domainevents", "snapshotevents"))

but with the same result. The Mongo is v3.4 just fresh install. Anyway atm I will move away from mongo in favour of couchbase. I can see that this is solved in AbstractEventStorageEngine by the datastore throwing an exception and being handled properly.

Cheers.

Hi Tomas,

are the indexes in place? There should be a unique index on the aggregateIdentifier and sequenceNumber properties. If there isn’t, you can create it by calling “ensureIndexes” on your MongoEventStorageEngine instance.

Cheers,

Allard

No, they weren’t created. When adding the index manually all worked as expected.