Expose id/getListenerId in base EventListener/ApplicationListener (pulled up from tx)
Includes removeApplicationListeners(Predicate) method in ApplicationEventMulticaster. Closes gh-26638
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -59,6 +59,20 @@ public interface TransactionalApplicationListener<E extends ApplicationEvent>
|
||||
return Ordered.LOWEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an identifier for the listener to be able to refer to it individually.
|
||||
* <p>It might be necessary for specific completion callback implementations
|
||||
* to provide a specific id, whereas for other scenarios an empty String
|
||||
* (as the common default value) is acceptable as well.
|
||||
* @see ApplicationListener#getListenerId()
|
||||
* @see TransactionalEventListener#id
|
||||
* @see #addCallback
|
||||
*/
|
||||
@Override
|
||||
default String getListenerId() {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link TransactionPhase} in which the listener will be invoked.
|
||||
* <p>The default phase is {@link TransactionPhase#AFTER_COMMIT}.
|
||||
@@ -67,17 +81,6 @@ public interface TransactionalApplicationListener<E extends ApplicationEvent>
|
||||
return TransactionPhase.AFTER_COMMIT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an identifier for the listener to be able to refer to it individually.
|
||||
* <p>It might be necessary for specific completion callback implementations
|
||||
* to provide a specific id, whereas for other scenarios an empty String
|
||||
* (as the common default value) is acceptable as well.
|
||||
* @see #addCallback
|
||||
*/
|
||||
default String getListenerId() {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a callback to be invoked on processing within transaction synchronization,
|
||||
* i.e. when {@link #processEvent} is being triggered during actual transactions.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -25,11 +25,8 @@ import org.springframework.context.event.ApplicationListenerMethodAdapter;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.context.event.GenericApplicationListener;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link GenericApplicationListener} adapter that delegates the processing of
|
||||
@@ -55,9 +52,6 @@ public class TransactionalApplicationListenerMethodAdapter extends ApplicationLi
|
||||
|
||||
private final TransactionPhase transactionPhase;
|
||||
|
||||
@Nullable
|
||||
private volatile String listenerId;
|
||||
|
||||
private final List<SynchronizationCallback> callbacks = new CopyOnWriteArrayList<>();
|
||||
|
||||
|
||||
@@ -84,31 +78,6 @@ public class TransactionalApplicationListenerMethodAdapter extends ApplicationLi
|
||||
return this.transactionPhase;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getListenerId() {
|
||||
String id = this.listenerId;
|
||||
if (id == null) {
|
||||
id = this.annotation.id();
|
||||
if (id.isEmpty()) {
|
||||
id = getDefaultListenerId();
|
||||
}
|
||||
this.listenerId = id;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the default id for the target listener, to be applied in case of
|
||||
* no {@link TransactionalEventListener#id() annotation-specified id value}.
|
||||
* <p>The default implementation builds a method name with parameter types.
|
||||
* @see #getListenerId()
|
||||
*/
|
||||
protected String getDefaultListenerId() {
|
||||
Method method = getTargetMethod();
|
||||
return ClassUtils.getQualifiedMethodName(method) +
|
||||
"(" + StringUtils.arrayToDelimitedString(method.getParameterTypes(), ",") + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCallback(SynchronizationCallback callback) {
|
||||
Assert.notNull(callback, "SynchronizationCallback must not be null");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -64,13 +64,6 @@ public @interface TransactionalEventListener {
|
||||
*/
|
||||
TransactionPhase phase() default TransactionPhase.AFTER_COMMIT;
|
||||
|
||||
/**
|
||||
* An optional identifier to uniquely reference the listener.
|
||||
* @since 5.3
|
||||
* @see TransactionalApplicationListener#getListenerId()
|
||||
*/
|
||||
String id() default "";
|
||||
|
||||
/**
|
||||
* Whether the event should be processed if no transaction is running.
|
||||
*/
|
||||
@@ -98,6 +91,17 @@ public @interface TransactionalEventListener {
|
||||
* <p>The default is {@code ""}, meaning the event is always handled.
|
||||
* @see EventListener#condition
|
||||
*/
|
||||
@AliasFor(annotation = EventListener.class, attribute = "condition")
|
||||
String condition() default "";
|
||||
|
||||
/**
|
||||
* An optional identifier for the listener, defaulting to the fully-qualified
|
||||
* signature of the declaring method (e.g. "mypackage.MyClass.myMethod()").
|
||||
* @since 5.3
|
||||
* @see EventListener#id
|
||||
* @see TransactionalApplicationListener#getListenerId()
|
||||
*/
|
||||
@AliasFor(annotation = EventListener.class, attribute = "id")
|
||||
String id() default "";
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user