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

@Async aspect should clear cached executors when different BeanFactory set #28201

Closed
wants to merge 5 commits into from
Closed
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 @@ -18,6 +18,7 @@

import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand All @@ -31,6 +32,7 @@

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
Expand Down Expand Up @@ -61,7 +63,7 @@
* @author Stephane Nicoll
* @since 3.1.2
*/
public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware {
public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware, DisposableBean {

/**
* The default name of the {@link TaskExecutor} bean to pick up: "taskExecutor".
Expand Down Expand Up @@ -144,6 +146,12 @@ public void setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)
this.exceptionHandler = SingletonSupplier.of(exceptionHandler);
}

@Override
public void destroy() {
this.beanFactory = null;
this.executors.clear();
}

/**
* Set the {@link BeanFactory} to be used when looking up executors by qualifier
* or when relying on the default executor lookup algorithm.
Expand All @@ -152,7 +160,10 @@ public void setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)
*/
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
if (!Objects.equals(this.beanFactory, beanFactory)) {
this.destroy();
this.beanFactory = beanFactory;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,32 @@ public void qualifiedAsyncMethodsAreRoutedToCorrectExecutor() throws Interrupted
assertThat(e1OtherThread.get().getName()).startsWith("e1-");
}

@Test
public void testChangeBeanFactory() throws ExecutionException, InterruptedException {
DefaultListableBeanFactory beanFactory1 = new DefaultListableBeanFactory();
ThreadPoolTaskExecutor executor1 = new ThreadPoolTaskExecutor();
executor1.setThreadFactory(r -> new Thread(r, "bf1"));
executor1.initialize();
beanFactory1.registerSingleton("e1", executor1);
AnnotationAsyncExecutionAspect.aspectOf().setBeanFactory(beanFactory1);

ClassWithQualifiedAsyncMethods obj = new ClassWithQualifiedAsyncMethods();

ListenableFuture<Thread> e1Thread = obj.e1Work();
assertThat(e1Thread.get().getName()).isEqualTo("bf1");


DefaultListableBeanFactory beanFactory2 = new DefaultListableBeanFactory();
ThreadPoolTaskExecutor executor2 = new ThreadPoolTaskExecutor();
executor2.setThreadFactory(r -> new Thread(r, "bf2"));
executor2.initialize();
beanFactory2.registerSingleton("e1", executor2);
AnnotationAsyncExecutionAspect.aspectOf().setBeanFactory(beanFactory2);

ListenableFuture<Thread> e2Thread = obj.e1Work();
assertThat(e2Thread.get().getName()).startsWith("bf2");
}

@Test
public void exceptionHandlerCalled() {
Method m = ReflectionUtils.findMethod(ClassWithException.class, "failWithVoid");
Expand Down