Please tell me how I can solve such thing? While I am trying to interrupt getting image from url through Spring WebFlux webClient - it does not stop.
I have next code
method1(){
...
ExecutorService executor = Executors.newFixedThreadPool(3);
for (String url: urlList){
executor.submit(
() -> {
//byte[] byteImage = getImage(url)
//save in file system (byteImage)
//save in DB (byteImage)
//save in redis (byteImage)
}
)}
...
}
public byte[] getImage(String url) {
byte[] result = null;
try {
webClient
.get()
.uri(url)
.header("X-Requested-With", "XMLHttpRequest")
.exchange()
.flatMap(response -> {
if (!response.statusCode().is2xxSuccessful()) {
return Mono.error(new RuntimeException("Internal server error"));
} else {
return response.bodyToMono(ByteArrayResource.class);
}
}).map(ByteArrayResource::getByteArray)
.block();
} catch (Exception e) {
log.warn("can't take screenshot {}", url);
}
return result;
}
in another thread I try to interrupt all executor threads by
executor.shutdownNow()
If I remove webClient #block() - everything ok. All threads successfully interrupted and the process stops.
But if the webClient has block() method the executor can't stop execution.
Help please, how can I solve this issue?