我正在使用异步任务处理一些大请求,我的详细信息如下
@Configuration
@EnableAsync
public class AsyncConfiguration
{
@Bean(name = "asyncExecutor")
public Executor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(100);
executor.setThreadNamePrefix("AsynchThread-");
executor.initialize();
return executor;
}
}
@Service
public class AsyncService {
@Async("Executor")
public void processData(User user) throws InterruptedException
{
/* my business logic for each user*/
return ;
}
}
在这里我得到用户列表,它可能包含 50 个用户,所以在发布到异步任务之前我想知道我有足够的队列容量来容纳 50 个线程,如果没有,我必须返回调用者说我的服务器很忙。
@RequestMapping(value = "/users", method = RequestMethod.POST)
public void postAllUsers(@RequestBody List<User> users) throws InterruptedException, ExecutionException
{
for (List<User> user: users)
{
try {
service.processData(user);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
那么如何获得当前的 que 使用情况?