我正在使用异步任务来处理一些大请求,我的详细信息在下面
@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();
}
}
}
那么如何获得当前的使用率?