问题
I am using ThreadPoolExecutor
as my custom executor with @ASync
annotation.
In google, I have found that the task below needs to be configured in xml
but I'm not sure how the myExecutor
is mapped to my custom executor.
<task:annotation-driven executor="myExecutor" />
Even found that in bean properties, its path is not given.
How is it called then?
回答1:
Four options:
- Declare a single bean of type
TaskExecutor
- Declare a single bean with the name
AsyncExecutionAspectSupport.DEFAULT_TASK_EXECUTOR_BEAN_NAME
("taskExecutor"
) - Implement AsyncConfigurer#getAsyncExecutor
- For individual classes/methods, provide a qualifier of an executor bean in the @Async#value.
回答2:
I am not sure I understand your question but your configuration snippet is correct provided that you have defined an Executor
bean with myExecutor
as id.
The javadoc of @EnableAsync
has a good coverage of how this works. For instance, to create a ThreadPoolTaskExecutor
with 5 core threads and 10 max threads:
<task:annotation-driven executor="myExecutor"/>
<task:executor id="myExecutor" pool-size="5-10"/>
来源:https://stackoverflow.com/questions/22367531/how-can-i-use-a-custom-executor-for-an-async-method