Skip to content
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

"Unexpected sequence number on stored event." exception when dispatching an event from an @EventHandler method #306

Closed
twerno opened this issue Mar 18, 2017 · 0 comments
Assignees
Labels
Priority 1: Must Highest priority. A release cannot be made if this issue isn’t resolved. Status: Resolved Use to signal that work on this issue is done. Type: Bug Use to signal issues that describe a bug within the system.
Milestone

Comments

@twerno
Copy link

twerno commented Mar 18, 2017

When dispatching an Event inside EventHandler method the nested event is processed and added to the EventStore before the root event is.

import static org.axonframework.commandhandling.model.AggregateLifecycle.apply;

import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.commandhandling.model.AggregateIdentifier;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.test.aggregate.AggregateTestFixture;
import org.axonframework.test.aggregate.FixtureConfiguration;
import org.junit.Before;
import org.junit.Test;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Value;

public class Example {
	
	private FixtureConfiguration<Aggregate> fixture;
	private final String ID = "id";
	
	@Before
	public void setUp() {
		fixture = new AggregateTestFixture<Aggregate>(Aggregate.class);
	}
	
	@Test
	public void testKontoUtworz() {
		fixture.givenNoPriorActivity()
			.when(new Command(ID))
			.expectEvents(new Event_1(ID), new Event_2(ID));
	}

}

@Value class Command {private String id;}
@Value class Event_1 {private String id;}
@Value class Event_2 {private String id; 
	public Event_2(String id) {
		this.id = id;
	}
}

@Data @NoArgsConstructor class Aggregate {
	
	@AggregateIdentifier
	private String id;
	
	@CommandHandler
	public Aggregate(Command command) {
		apply(new Event_1(command.getId()));
	}
	
	@EventSourcingHandler
	public void on(Event_1 event) {
		this.id = event.getId();
		apply(new Event_2(event.getId()));
	}
	
	@EventSourcingHandler
	public void on(Event_2 event) {}
}

Attached test finished with exception:

org.axonframework.eventsourcing.IncompatibleAggregateException: Aggregate identifier must be non-null after applying an event. Make sure the aggregate identifier is initialized at the latest when handling the creation event.

which direct cause is

Unexpected sequence number on stored event.

(The last exception is consumed silently due to default fixture configuration)

@twerno twerno changed the title "Unexpected sequence number on stored event." exception when dispatching event from an @EventHandler method "Unexpected sequence number on stored event." exception when dispatching an event from an @EventHandler method Mar 18, 2017
@abuijze abuijze added this to the Release 3.0.3 milestone Mar 22, 2017
@abuijze abuijze added the Type: Bug Use to signal issues that describe a bug within the system. label Mar 22, 2017
abuijze added a commit that referenced this issue Mar 22, 2017
An exception stating that the aggregate identifier has not been updated is thrown when an event is applied in the `@EventSourcingHandler` of the creation event. The cause was a missing "applying" flag when applying this creation event, causing the applied event to quickly take precedence.

Fixes issue #306
@abuijze abuijze closed this as completed Mar 22, 2017
@smcvb smcvb added Priority 1: Must Highest priority. A release cannot be made if this issue isn’t resolved. Status: Resolved Use to signal that work on this issue is done. labels Feb 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority 1: Must Highest priority. A release cannot be made if this issue isn’t resolved. Status: Resolved Use to signal that work on this issue is done. Type: Bug Use to signal issues that describe a bug within the system.
Projects
None yet
Development

No branches or pull requests

3 participants