From db17a28238041375582aa91d3687d85130cab80f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 13 Jan 2017 11:40:54 +0100 Subject: [PATCH] DATACMNS-975 - Make sure repository method is invoked before event publication. To make sure that event handlers can read the state of the aggregate persisted, we now invoke the call to the repository before publishing the events. That shouldn't make too much of a difference to most listeners as the event usually contains a reference to the aggregate. However, the new model generally aligns with the approach of reporting what *already has happened* better that our previous approach of first publishing the events and then invoking the repository method. Related tickets: DATACMNS-928. --- ...tPublishingRepositoryProxyPostProcessor.java | 6 ++++-- ...ngRepositoryProxyPostProcessorUnitTests.java | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java b/src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java index 90200607d..3311c9160 100644 --- a/src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java +++ b/src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java @@ -87,15 +87,17 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr @Override public Object invoke(MethodInvocation invocation) throws Throwable { + Object result = invocation.proceed(); + if (!invocation.getMethod().getName().equals("save")) { - return invocation.proceed(); + return result; } for (Object argument : invocation.getArguments()) { eventMethod.publishEventsFrom(argument, publisher); } - return invocation.proceed(); + return result; } } diff --git a/src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java index 1f63c6996..32dccf308 100644 --- a/src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java @@ -174,6 +174,23 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests { verify(publisher).publishEvent(any(SomeEvent.class)); } + @Test // DATACMNS-975 + public void publishesEventsAfterSaveInvocation() throws Throwable { + + doReturn(SampleRepository.class.getMethod("save", Object.class)).when(invocation).getMethod(); + doReturn(new Object[] { OneEvent.of(new SomeEvent()) }).when(invocation).getArguments(); + + doThrow(new IllegalStateException()).when(invocation).proceed(); + + try { + EventPublishingMethodInterceptor// + .of(EventPublishingMethod.of(OneEvent.class), publisher)// + .invoke(invocation); + } catch (IllegalStateException o_O) { + verify(publisher, never()).publishEvent(any(SomeEvent.class)); + } + } + @Value(staticConstructor = "of") static class MultipleEvents { @Getter(onMethod = @__(@DomainEvents)) Collection events;