java - 如何通过调用 executorService.shutdownNow() 来停止 webclient get() exchange()?
问题描述
请告诉我如何解决这样的事情?虽然我试图中断通过 Spring WebFlux webClient 从 url 获取图像 - 它并没有停止。
我有下一个代码
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;
}
在另一个线程中,我尝试通过
executor.shutdownNow()
如果我删除 webClient #block() - 一切正常。所有线程成功中断,进程停止。
但是如果 webClient 有 block() 方法,则执行者不能停止执行。
请帮忙,我该如何解决这个问题?
解决方案
我找到了解决方案。问题是当我捕捉到 InterruptedException 标志“中断”重置。另一个问题是当块抛出异常时,它会抛出包装的 InterruptedException
如果我尝试捕获并再次设置标志 InterruptedException 它没有进入条件,例如
if (e instanceof InterruptedException){
Thread.currentThread().interrupt(); << this is never called
}
解决方案是:
if (Exceptions.unwrap(e) instanceof InterruptedException){
Thread.currentThread().interrupt(); << this is never called
}
推荐阅读
- java - 如何验证所有自己抛出的运行时异常都包含在 Javadoc 中?
- spring - LocalDate 反序列化和序列化错误 -jackson.databind.exc.InvalidDefinitionException:无法构造 java.time.LocalDate 的实例
- javascript - asp Textbox.text 属性不随 javascript 改变
- html - 2个有角度的材料卡并排?
- react-native - React Native 应用程序卡在模拟器中的“加载依赖图完成”
- google-apps-script - 谷歌表格自定义功能 - 简化调用,方便的功能
- javascript - 如何遍历作为 JSON 中键值的数组
- exception - 流中抛出的错误作为 InternalNodeException 返回给 rpc 客户端,我们如何改进错误报告?
- python - Python 3.7 BeautifullSoup soup.find 错误 - find() 没有关键字参数
- go - 通道阻塞顺序