Skip to content

Commit

Permalink
Add sequenceIdentifier method to the EventHandlerInvoker
Browse files Browse the repository at this point in the history
A dead lettering EventHandlerInvoker should be able to retrieve the
sequence id for a given event from its delegate. Without this, it will
not be able to correctly store the events in the right order. By added
EventHandlerInvoker#sequenceIdentifier, any specific implementation can
retrieve the seqId for any event from its delegate.

#2021
  • Loading branch information
smcvb committed Dec 3, 2021
1 parent 4a00975 commit 3a35dc9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2021. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -90,4 +90,15 @@ default <R> void performReset(R resetContext) {
);
}
}

/**
* Retrieve the sequence identifier for the given {@code event}, used to handle events in the desired order.
* Defaults to {@link EventMessage#getIdentifier()}.
*
* @param event the {@link EventMessage} to deduce the sequence identifier for
* @return the sequence identifier for the given {@code event}
*/
default Object sequenceIdentifier(EventMessage<?> event) {
return event.getIdentifier();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public boolean canHandle(EventMessage<?> eventMessage, Segment segment) {
return hasHandler(eventMessage) && sequencingPolicyMatchesSegment(eventMessage, segment);
}

private boolean sequencingPolicyMatchesSegment(EventMessage<?> message, Segment segment) {
return segment.matches(Objects.hashCode(sequenceIdentifier(message)));
}

@Override
public boolean canHandleType(Class<?> payloadType) {
return wrappedEventHandlers.stream().anyMatch(eh -> eh.canHandleType(payloadType));
Expand All @@ -146,14 +150,6 @@ public boolean supportsReset() {
return true;
}

private boolean sequencingPolicyMatchesSegment(EventMessage<?> message, Segment segment) {
return segment.matches(Objects.hashCode(sequenceIdentifier(message)));
}

protected Object sequenceIdentifier(EventMessage<?> event) {
return getOrDefault(sequencingPolicy.getSequenceIdentifierFor(event), event::getIdentifier);
}

@Override
public void performReset() {
performReset(null);
Expand All @@ -166,6 +162,11 @@ public <R> void performReset(R resetContext) {
}
}

@Override
public Object sequenceIdentifier(EventMessage<?> event) {
return getOrDefault(sequencingPolicy.getSequenceIdentifierFor(event), event::getIdentifier);
}

/**
* Return the {@link ListenerInvocationErrorHandler} as configured for this {@link EventHandlerInvoker}.
*
Expand Down

0 comments on commit 3a35dc9

Please sign in to comment.