Yes, the @Timestamp annotation is mandatory for @EventSourcingHandler methods in Axon Framework. While @EventHandler methods can automatically resolve Instant parameters to the event timestamp, @EventSourcingHandler methods require explicit annotation. Your current implementation with @Timestamp Instant requestedAt is correct and should be maintained.
//CORRECT USE - @Timestamp must be added
@EventSourcingHandler
public void on(FooEvent event, @Timestamp Instant requestedAt) {
// requestAt now contains the event's timestamp
}
Difference between @EventHandler and @EventSourcingHandler:
In @EventHandler methods, Axon automatically populates Instant parameters with the event timestamp.
In @EventSourcingHandler methods, the explicit @Timestamp annotation is required.
// CORRECT USAGE - @Timestamp must be added.
@EventSourcingHandler
public void on(FooEvent event, @Timestamp Instant requestedAt) {
// requestedAt now contains the event's timestamp.
}
Solution for test error:
Adding @Timestamp will now recognize the parameter, and the "No resource of type Instant" error will disappear.
Alternative solution (carrying timestamp within the event):
// Your Event class
public class FooEvent {
private Instant timestamp;
// getter/setter
}
// In EventSourcingHandler
@EventSourcingHandler
public void on(FooEvent event) {
Instant requestedAt = event.getTimestamp(); // Use the event's own timestamp
}
This may be a documentation oversight/ambiguity, but technically the correct behavior is to require @Timestamp annotation for @EventSourcingHandler. You should continue using your existing working code (with @Timestamp).