How can I use a custom executor for an @Async method?

自闭症网瘾萝莉.ら 提交于 2020-03-16 07:41:30

问题


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:

  1. Declare a single bean of type TaskExecutor
  2. Declare a single bean with the name AsyncExecutionAspectSupport.DEFAULT_TASK_EXECUTOR_BEAN_NAME ("taskExecutor")
  3. Implement AsyncConfigurer#getAsyncExecutor
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!