Replay Events without Spring

Hi,

we are currently testing Axon 3.2 in a transactional context. By using the MongoEventStorageEngine (new MongoEventStorageEngine(new DefaultMongoTemplate(new MongoClient(new ServerAddress(“127.0.0.1”, 27017)), “axon”))), we persist the events:

Configuration configuration = DefaultConfigurer.defaultConfiguration()
.configureEmbeddedEventStore(store -> new MongoEventStorageEngine(new DefaultMongoTemplate(new MongoClient(new ServerAddress(“127.0.0.1”, 27017)), “axon”)))
.configureAggregate(Participant.class)
.buildConfiguration();
configuration.start();

CommandGateway commandGateway = configuration.commandGateway();

commandGateway.sendAndWait(new CreateCommand());

It works without any problems!

At the moment, however, I can’t find any hints in the documentation and in other projects on GitHub on how to replay the events after a restart.
Is there a minimal example of how this should work without Spring?

Thank you very much for your help!

Hi Jonas,

Replaying events could happen on two sides in typical Axon applications: the command side (your aggregate typically) and the query side (thus you’re event handling components, which do or do not update views).

For the command side this is done for you if you’re following all the defaults, as this would give you an EventSourcingRepository for your Aggregates.

It would in that scenario always read all the events for an Aggregate, replay those against an empty instance of your Aggregate and then handle any command you’d want it to handle.

For the query side to be replayable, you’ll first have to configure your Event Handling Components to be backed by Tracking Event Processors.

A Tracking Event Processor (as the name might already give away) keeps track of which events it has handled.
By telling a Tracking Event Processor it should ‘reset it’s state’ (by calling the TrackingEventProcessor#resetTokens() function), it will remove it’s knowledge of which events it has handled.

This thus leads to it starting from the beginning of (event store) history, leading to a replay.

Hope this gives you some insights Jonas and feel free to ask follow up questions!

Cheers,

Steven

Hi,

thank you for the quick response! The replay for the command side doesn’t seem to work automatically.

This is my configuration:

EventStorageEngine eventStorageEngine = new MongoEventStorageEngine(new DefaultMongoTemplate(new MongoClient(new ServerAddress(“127.0.0.1”, 27017)), “axon”));
Configuration configuration = DefaultConfigurer.defaultConfiguration()
.configureEmbeddedEventStore(store -> eventStorageEngine)
.configureAggregate(Participant.class)
.buildConfiguration();
configuration.start();

This is my aggregate:

public class Participant {
@AggregateIdentifier
private String id = “TEST”;

public Participant() {
}

@CommandHandler
public Participant(CreateCommand command) {
System.out.println(“CreateCommand”);
AggregateLifecycle.apply(new CreatedEvent());
}

@EventSourcingHandler
public void on(CreatedEvent event) {
System.out.println(“CreatedEvent”);
}

public String getId() {
return id;
}
}

When I try to send 5 commands, it seems to be working as expected.

CommandGateway commandGateway = configuration.commandGateway();
for(int i = 0; i < 5; i++) {
commandGateway.sendAndWait(new CreateCommand());
}

This is my command line output:

Apr 26, 2018 11:13:49 AM com.mongodb.diagnostics.logging.JULLogger log
INFORMATION: Opened connection [connectionId{localValue:2, serverValue:19}] to 127.0.0.1:27017
CreateCommand
CreatedEvent
CreateCommand
CreatedEvent
CreateCommand
CreatedEvent
CreateCommand
CreatedEvent
CreateCommand
CreatedEvent

When I restart the application, I expect the events to replay. But this is not the case, the event handlers are not executed again and there is no command line output.

What am I missing? Thank you very much!

Kind regards

Jonas

why do you expect te events to be replayed at the start of the application ? The are replayed on the aggregate as a new command arrives, thus setting the Aggregate in the state of these events, so you can verify your command with that state .
Gerlo.

Hi,

That doesn’t seem to be the case. Even after new commands, the events are not replayed.

Maybe because I don’t use spring?

Kind regards

Jonas

Al your commands are creating new Aggregate instances. The event replay works on when a command arrives on an already created aggregate. So if you send a second command on a aggregate the first (the create command) will be replayed.

I tried your configuration setup and looks like its working.
But your code is strange in that it (tries) to create multiple aggregates with the same id ("TEST) .
See for a example at http://www.axonframework.org/axon-2-quickstart-guide/#step2 how to set the Id in the CreatedEvent and using a @TargetAggregateIdentifier on the (second) command to find the right aggregate .

Hi,

You’re right, this could be the problem. I’ll be back in the office next week and will test it. Thank you very much for your help, I appreciate it!

Kind regards

Jonas

Hi,

the events are now replayed as expected.

Thank you very much!

Kind regards

Jonas