I have a Spring Boot Application
.
My application sends requests to another applications using restTemplate
.
I need to send a request to one hundred different applications(on different servers). I use:
publi class Service {
private RestClient restClient;
private List<String> urls;
private ThreadPoolExecutor executor;
public Service(RestClient restClient, List<String> urls, ThreadPoolExecutor executor){
this.restClient = restClient;
this.urls = urls;
this.executor = executor;
}
public void sendPost(Entity entity){
for (String url: urls){
executor.execute(() -> restClient.create(url, entity);
}
}
}
I am trying to use ThreadPoolExecutor(fixedSizeThreadPool)
but I have some questions.
1.I read up that threadPoolExecutor
is thread-safe. Does It means that I can invoke execute()
at the same time from different threads and It will work properly?
2. If there are no idle threads in threadPoolExecutor
It will slow down the application and I should to choose rational pool size, right?
3. For example, I need to write executed urls in ArrayList
:
public void sendPost(Entity entity){
List<String> executedUrls = new ArrayList<>();
for (String url: urls){
executor.execute(() -> restClient.create(url, entity, executedUrls);
}
}
RestClient
sends a request and if It is successfully executed It will be added in ArrayList
.
I expect that ArrayList
will have list of successfully executed urls If I have an exception in any thread from threadPool
.
Will It work as I expect or I can have something like lost update?