Introduce generalized MethodFailureEvent for use in EventPublicationInterceptor

Closes gh-36072
This commit is contained in:
Juergen Hoeller
2025-12-24 18:50:04 +01:00
parent 214cb2c1e1
commit 08104100d3
5 changed files with 162 additions and 46 deletions
@@ -17,11 +17,13 @@
package org.springframework.context.event;
import java.lang.reflect.Constructor;
import java.util.function.Function;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.jspecify.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
@@ -29,14 +31,19 @@ import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.util.Assert;
/**
* {@link MethodInterceptor Interceptor} that publishes an
* {@code ApplicationEvent} to all {@code ApplicationListeners}
* registered with an {@code ApplicationEventPublisher} after each
* <i>successful</i> method invocation.
* {@link MethodInterceptor Interceptor} that publishes an {@code ApplicationEvent}
* to all {@code ApplicationListeners} registered with an {@code ApplicationEventPublisher}.
* after each <i>successful</i> method invocation.
*
* <p>Note that this interceptor is only capable of publishing <i>stateless</i>
* events configured via the
* {@link #setApplicationEventClass "applicationEventClass"} property.
* <p>Note that this interceptor is capable of publishing a custom event after each
* <i>successful</i> method invocation, configured via the
* {@link #setApplicationEventClass "applicationEventClass"} property. As of 7.0.3,
* you can configure a {@link #setApplicationEventFactory factory function} instead.
*
* <p>As of 7.0.3, this interceptor publishes a {@link MethodFailureEvent} for
* every exception encountered from a method invocation. This can be conveniently
* tracked via an {@code ApplicationListener<MethodFailureEvent>} class or an
* {@code @EventListener(MethodFailureEvent.class)} method.
*
* @author Dmitriy Kopylenko
* @author Juergen Hoeller
@@ -50,7 +57,7 @@ import org.springframework.util.Assert;
public class EventPublicationInterceptor
implements MethodInterceptor, ApplicationEventPublisherAware, InitializingBean {
private @Nullable Constructor<?> applicationEventClassConstructor;
private @Nullable Function<MethodInvocation, ? extends ApplicationEvent> applicationEventFactory;
private @Nullable ApplicationEventPublisher applicationEventPublisher;
@@ -63,14 +70,16 @@ public class EventPublicationInterceptor
* @throws IllegalArgumentException if the supplied {@code Class} is
* {@code null} or if it is not an {@code ApplicationEvent} subclass or
* if it does not expose a constructor that takes a single {@code Object} argument
* @see #setApplicationEventFactory
*/
public void setApplicationEventClass(Class<?> applicationEventClass) {
public void setApplicationEventClass(Class<? extends ApplicationEvent> applicationEventClass) {
if (ApplicationEvent.class == applicationEventClass ||
!ApplicationEvent.class.isAssignableFrom(applicationEventClass)) {
throw new IllegalArgumentException("'applicationEventClass' needs to extend ApplicationEvent");
}
try {
this.applicationEventClassConstructor = applicationEventClass.getConstructor(Object.class);
Constructor<? extends ApplicationEvent> ctor = applicationEventClass.getConstructor(Object.class);
this.applicationEventFactory = (invocation -> BeanUtils.instantiateClass(ctor, invocation.getThis()));
}
catch (NoSuchMethodException ex) {
throw new IllegalArgumentException("ApplicationEvent class [" +
@@ -78,6 +87,16 @@ public class EventPublicationInterceptor
}
}
/**
* Specify a factory function for {@link ApplicationEvent} instances built from a
* {@link MethodInvocation}, representing a <i>successful</i> method invocation.
* @since 7.0.3
* @see #setApplicationEventClass
*/
public void setApplicationEventFactory(Function<MethodInvocation, ? extends ApplicationEvent> factoryFunction) {
this.applicationEventFactory = factoryFunction;
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
@@ -85,23 +104,28 @@ public class EventPublicationInterceptor
@Override
public void afterPropertiesSet() throws Exception {
if (this.applicationEventClassConstructor == null) {
throw new IllegalArgumentException("Property 'applicationEventClass' is required");
if (this.applicationEventPublisher == null) {
throw new IllegalArgumentException("Property 'applicationEventPublisher' is required");
}
}
@Override
public @Nullable Object invoke(MethodInvocation invocation) throws Throwable {
Object retVal = invocation.proceed();
Assert.state(this.applicationEventClassConstructor != null, "No ApplicationEvent class set");
ApplicationEvent event = (ApplicationEvent)
this.applicationEventClassConstructor.newInstance(invocation.getThis());
Assert.state(this.applicationEventPublisher != null, "No ApplicationEventPublisher available");
this.applicationEventPublisher.publishEvent(event);
Object retVal;
try {
retVal = invocation.proceed();
}
catch (Throwable ex) {
this.applicationEventPublisher.publishEvent(new MethodFailureEvent(invocation, ex));
throw ex;
}
if (this.applicationEventFactory != null) {
this.applicationEventPublisher.publishEvent(this.applicationEventFactory.apply(invocation));
}
return retVal;
}
@@ -0,0 +1,79 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.event;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.context.ApplicationEvent;
import org.springframework.util.ClassUtils;
/**
* Event indicating a method invocation that failed.
*
* @author Juergen Hoeller
* @since 7.0.3
* @see EventPublicationInterceptor
*/
@SuppressWarnings("serial")
public class MethodFailureEvent extends ApplicationEvent {
private final Throwable failure;
/**
* Create a new event for the given method invocation.
* @param invocation the method invocation
* @param failure the exception encountered
*/
public MethodFailureEvent(MethodInvocation invocation, Throwable failure) {
super(invocation);
this.failure = failure;
}
/**
* Return the method invocation that triggered this event.
*/
@Override
public MethodInvocation getSource() {
return (MethodInvocation) super.getSource();
}
/**
* Return the method that triggered this event.
*/
public Method getMethod() {
return getSource().getMethod();
}
/**
* Return the exception encountered.
*/
public Throwable getFailure() {
return this.failure;
}
@Override
public String toString() {
return getClass().getSimpleName() + ": " + ClassUtils.getQualifiedMethodName(getMethod()) +
" [" + getFailure() + "]";
}
}
@@ -16,11 +16,9 @@
package org.springframework.resilience.retry;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.MethodFailureEvent;
import org.springframework.util.ClassUtils;
/**
@@ -36,9 +34,7 @@ import org.springframework.util.ClassUtils;
* @see org.springframework.context.event.EventListener
*/
@SuppressWarnings("serial")
public class MethodRetryEvent extends ApplicationEvent {
private final Throwable failure;
public class MethodRetryEvent extends MethodFailureEvent {
private final boolean retryAborted;
@@ -50,27 +46,11 @@ public class MethodRetryEvent extends ApplicationEvent {
* @param retryAborted whether the current failure led to the retry execution getting aborted
*/
public MethodRetryEvent(MethodInvocation invocation, Throwable failure, boolean retryAborted) {
super(invocation);
this.failure = failure;
super(invocation, failure);
this.retryAborted = retryAborted;
}
/**
* Return the method invocation that triggered this event.
*/
@Override
public MethodInvocation getSource() {
return (MethodInvocation) super.getSource();
}
/**
* Return the method that triggered this event.
*/
public Method getMethod() {
return getSource().getMethod();
}
/**
* Return the exception encountered.
* <p>This may be an exception thrown by the method or emitted by the reactive
@@ -87,7 +67,7 @@ public class MethodRetryEvent extends ApplicationEvent {
* @see java.util.concurrent.TimeoutException
*/
public Throwable getFailure() {
return this.failure;
return super.getFailure();
}
/**