Skip to content

Optimize SpEL and property placeholder support for @Async qualifiers #28549

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

Closed
wants to merge 3 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 @@ -41,6 +41,8 @@
import org.springframework.lang.Nullable;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.StringValueResolver;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.function.SingletonSupplier;

/**
Expand All @@ -56,6 +58,7 @@
* @author Chris Beams
* @author Juergen Hoeller
* @author Stephane Nicoll
* @author He Bo
* @since 3.1.2
*/
public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware {
Expand All @@ -80,6 +83,8 @@ public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware {
@Nullable
private BeanFactory beanFactory;

@Nullable
private StringValueResolver stringValueResolver;

/**
* Create a new instance with a default {@link AsyncUncaughtExceptionHandler}.
Expand Down Expand Up @@ -150,6 +155,9 @@ public void setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
if (beanFactory instanceof ConfigurableBeanFactory configurableBeanFactory) {
this.stringValueResolver = new EmbeddedValueResolver(configurableBeanFactory);
}
}


Expand All @@ -163,6 +171,9 @@ protected AsyncTaskExecutor determineAsyncExecutor(Method method) {
if (executor == null) {
Executor targetExecutor;
String qualifier = getExecutorQualifier(method);
if (this.stringValueResolver != null && StringUtils.hasLength(qualifier)) {
qualifier = this.stringValueResolver.resolveStringValue(qualifier);
}
if (StringUtils.hasLength(qualifier)) {
targetExecutor = findQualifiedExecutor(this.beanFactory, qualifier);
}
Expand Down Expand Up @@ -206,10 +217,6 @@ protected Executor findQualifiedExecutor(@Nullable BeanFactory beanFactory, Stri
throw new IllegalStateException("BeanFactory must be set on " + getClass().getSimpleName() +
" to access qualified executor '" + qualifier + "'");
}
if (beanFactory instanceof ConfigurableBeanFactory configurableBeanFactory) {
EmbeddedValueResolver embeddedValueResolver = new EmbeddedValueResolver(configurableBeanFactory);
qualifier = embeddedValueResolver.resolveStringValue(qualifier);
}
return BeanFactoryAnnotationUtils.qualifiedBeanOfType(beanFactory, Executor.class, qualifier);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ public void withAsyncBeanWithExecutorQualifiedByExpressionOrPlaceholder() throws

Future<Thread> workerThread2 = asyncBean.myWork2();
assertThat(workerThread2.get().getName()).startsWith("myExecutor2-");

Future<Thread> workerThread3 = asyncBean.defaultExecutor();
assertThat(workerThread3.get().getName()).startsWith("SimpleAsyncTaskExecutor");
}
finally {
System.clearProperty("myExecutor");
Expand Down Expand Up @@ -382,6 +385,11 @@ public Future<Thread> myWork1() {
public Future<Thread> myWork2() {
return new AsyncResult<>(Thread.currentThread());
}

@Async("${my.app.targetExecutor:}")
public Future<Thread> defaultExecutor() {
return new AsyncResult<>(Thread.currentThread());
}
}


Expand Down