Skip to content

Commit a773b28

Browse files
committed
Add error handler for transactional event
1 parent 1e0ef99 commit a773b28

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

spring-tx/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.core.type.AnnotationMetadata;
3131
import org.springframework.transaction.TransactionManager;
3232
import org.springframework.transaction.config.TransactionManagementConfigUtils;
33+
import org.springframework.transaction.config.TransactionalEventErrorHandler;
3334
import org.springframework.transaction.event.TransactionalEventListenerFactory;
3435
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
3536
import org.springframework.transaction.interceptor.TransactionAttributeSource;
@@ -93,8 +94,8 @@ public TransactionAttributeSource transactionAttributeSource() {
9394

9495
@Bean(name = TransactionManagementConfigUtils.TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME)
9596
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
96-
public static TransactionalEventListenerFactory transactionalEventListenerFactory() {
97-
return new RestrictedTransactionalEventListenerFactory();
97+
public static TransactionalEventListenerFactory transactionalEventListenerFactory(@Nullable TransactionalEventErrorHandler errorHandler) {
98+
return new RestrictedTransactionalEventListenerFactory(errorHandler);
9899
}
99100

100101
}

spring-tx/src/main/java/org/springframework/transaction/annotation/RestrictedTransactionalEventListenerFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import java.lang.reflect.Method;
2020

21+
import org.jspecify.annotations.Nullable;
2122
import org.springframework.context.ApplicationListener;
2223
import org.springframework.core.annotation.AnnotatedElementUtils;
24+
import org.springframework.transaction.config.TransactionalEventErrorHandler;
2325
import org.springframework.transaction.event.TransactionalEventListenerFactory;
2426

2527
/**
@@ -35,6 +37,10 @@
3537
*/
3638
public class RestrictedTransactionalEventListenerFactory extends TransactionalEventListenerFactory {
3739

40+
public RestrictedTransactionalEventListenerFactory(@Nullable TransactionalEventErrorHandler errorHandler) {
41+
super(errorHandler);
42+
}
43+
3844
@Override
3945
public ApplicationListener<?> createApplicationListener(String beanName, Class<?> type, Method method) {
4046
Transactional txAnn = AnnotatedElementUtils.findMergedAnnotation(method, Transactional.class);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.springframework.transaction.config;
2+
3+
import org.jspecify.annotations.Nullable;
4+
import org.springframework.context.ApplicationEvent;
5+
import org.springframework.transaction.event.TransactionalApplicationListener;
6+
7+
public abstract class TransactionalEventErrorHandler implements TransactionalApplicationListener.SynchronizationCallback {
8+
9+
public abstract void handle(ApplicationEvent event, @Nullable Throwable ex);
10+
11+
@Override
12+
public void postProcessEvent(ApplicationEvent event, @Nullable Throwable ex) {
13+
handle(event, ex);
14+
}
15+
16+
}

spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListenerFactory.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import java.lang.reflect.Method;
2020

21+
import org.jspecify.annotations.Nullable;
2122
import org.springframework.context.ApplicationListener;
2223
import org.springframework.context.event.EventListenerFactory;
2324
import org.springframework.core.Ordered;
2425
import org.springframework.core.annotation.AnnotatedElementUtils;
26+
import org.springframework.transaction.config.TransactionalEventErrorHandler;
2527

2628
/**
2729
* {@link EventListenerFactory} implementation that handles {@link TransactionalEventListener}
@@ -35,6 +37,13 @@ public class TransactionalEventListenerFactory implements EventListenerFactory,
3537

3638
private int order = 50;
3739

40+
private @Nullable TransactionalEventErrorHandler errorHandler;
41+
42+
public TransactionalEventListenerFactory() { }
43+
44+
public TransactionalEventListenerFactory(@Nullable TransactionalEventErrorHandler errorHandler) {
45+
this.errorHandler = errorHandler;
46+
}
3847

3948
public void setOrder(int order) {
4049
this.order = order;
@@ -53,7 +62,14 @@ public boolean supportsMethod(Method method) {
5362

5463
@Override
5564
public ApplicationListener<?> createApplicationListener(String beanName, Class<?> type, Method method) {
56-
return new TransactionalApplicationListenerMethodAdapter(beanName, type, method);
65+
if (errorHandler == null) {
66+
return new TransactionalApplicationListenerMethodAdapter(beanName, type, method);
67+
}
68+
else {
69+
TransactionalApplicationListenerMethodAdapter listener = new TransactionalApplicationListenerMethodAdapter(beanName, type, method);
70+
listener.addCallback(errorHandler);
71+
return listener;
72+
}
5773
}
5874

5975
}

0 commit comments

Comments
 (0)