Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handler for transactional event #34146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.config.TransactionManagementConfigUtils;
import org.springframework.transaction.config.TransactionalEventErrorHandler;
import org.springframework.transaction.event.TransactionalEventListenerFactory;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
Expand Down Expand Up @@ -93,8 +94,8 @@ public TransactionAttributeSource transactionAttributeSource() {

@Bean(name = TransactionManagementConfigUtils.TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public static TransactionalEventListenerFactory transactionalEventListenerFactory() {
return new RestrictedTransactionalEventListenerFactory();
public static TransactionalEventListenerFactory transactionalEventListenerFactory(@Nullable TransactionalEventErrorHandler errorHandler) {
return new RestrictedTransactionalEventListenerFactory(errorHandler);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import java.lang.reflect.Method;

import org.jspecify.annotations.Nullable;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.transaction.config.TransactionalEventErrorHandler;
import org.springframework.transaction.event.TransactionalEventListenerFactory;

/**
Expand All @@ -35,6 +37,10 @@
*/
public class RestrictedTransactionalEventListenerFactory extends TransactionalEventListenerFactory {

public RestrictedTransactionalEventListenerFactory(@Nullable TransactionalEventErrorHandler errorHandler) {
super(errorHandler);
}

@Override
public ApplicationListener<?> createApplicationListener(String beanName, Class<?> type, Method method) {
Transactional txAnn = AnnotatedElementUtils.findMergedAnnotation(method, Transactional.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.springframework.transaction.config;

import org.jspecify.annotations.Nullable;
import org.springframework.context.ApplicationEvent;
import org.springframework.transaction.event.TransactionalApplicationListener;

public abstract class TransactionalEventErrorHandler implements TransactionalApplicationListener.SynchronizationCallback {

public abstract void handle(ApplicationEvent event, @Nullable Throwable ex);

@Override
public void postProcessEvent(ApplicationEvent event, @Nullable Throwable ex) {
handle(event, ex);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

import java.lang.reflect.Method;

import org.jspecify.annotations.Nullable;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListenerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.transaction.config.TransactionalEventErrorHandler;

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

private int order = 50;

private @Nullable TransactionalEventErrorHandler errorHandler;

public TransactionalEventListenerFactory() { }

public TransactionalEventListenerFactory(@Nullable TransactionalEventErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}

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

@Override
public ApplicationListener<?> createApplicationListener(String beanName, Class<?> type, Method method) {
return new TransactionalApplicationListenerMethodAdapter(beanName, type, method);
if (errorHandler == null) {
return new TransactionalApplicationListenerMethodAdapter(beanName, type, method);
}
else {
TransactionalApplicationListenerMethodAdapter listener = new TransactionalApplicationListenerMethodAdapter(beanName, type, method);
listener.addCallback(errorHandler);
return listener;
}
}

}