event order handling

Hi all,

I’ve already seen other topics about event order inside an aggregate, but in my case, it is about event handler in read side part.
It is an Event Sourcing architecture:

I have a command

`
CreateUserCommand(String username, String password)

`

Inside UserCommandHandler class :

`
User user = new User(createUserCommand.getUsername());
user.changePassword(passwordHasherService, createUserCommand.getPassword());
userRepository.add(user);

`

Where passwordHasherService is a domain service which generate hash for password.

User constructor raises UserCreatedEvent(String username);
User changePassword method raises UserPasswordChangedEvent(String username, String hashedPassword);

Inside UserQueryListener, i handle UserCreatedEvent and UserPasswordChangedEvent.

How to be sure that UserCreatedEvent is completed before UserPasswordChangedEvent is called in my read side listener ?

Thanks.

Baptiste.

Hi Baptiste,

the order in which event handlers are invoked depends primarily on the cluster that the handlers are in. If you use the SimpleEventBus, you’re effectively using a SimpleCluster. It’s a cluster that dispatches (and handles events) in the thread that publishes them. This means that when two events are generated by a unit of work, they are handled one after another in the same thread.

If you use another cluster type, it is the implementation of that cluster that defines the ordering semantics. Some clusters (like the async one) use a SequencingPolicy that allows you to define which events may be handled concurrently, and which must be handled sequentially.

Hope this help.
Cheers,

Allard