【发布时间】:2014-07-02 20:14:36
【问题描述】:
我正在使用 Oracle Jersey Client,并试图取消长时间运行的 get 或 put 操作。
Client 构造为:
JacksonJsonProvider provider = new JacksonJsonProvider(new ObjectMapper());
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getSingletons().add(provider);
Client client = Client.create(clientConfig);
以下代码在工作线程上执行:
File bigZipFile = new File("/home/me/everything.zip");
WebResource resource = client.resource("https://putfileshere.com");
Builder builder = resource.getRequestBuilder();
builder.type("application/zip").put(bigZipFile); //This will take a while!
我想取消这个长期运行的put。当我尝试中断工作线程时,put 操作继续运行。据我所知,泽西客户端没有尝试检查Thread.interrupted()。
在使用 AsyncWebResource 而不是 WebResource 以及在 Builder.put(..) 调用中使用 Future.cancel(true) 时,我看到了相同的行为。
到目前为止,我想出的唯一解决方案是在 ContainerListener 中抛出 RuntimeException:
client.addFilter(new ConnectionListenerFilter(
new OnStartConnectionListener(){
public ContainerListener onStart(ClientRequest cr) {
return new ContainerListener(){
public void onSent(long delta, long bytes) {
//If the thread has been interrupted, stop the operation
if (Thread.interrupted()) {
throw new RuntimeException("Upload or Download canceled");
}
//Report progress otherwise
}
}...
我想知道是否有更好的解决方案(可能在创建 Client 时)可以正确处理可中断 I/O 而无需使用 RuntimeException。
【问题讨论】:
-
是上传数据需要这么长时间还是在服务器上处理?
-
@isnot2bad 是上传数据。假设一个 20MB 的文件在连接速度较慢的情况下可能需要相当长的时间才能上传。
-
同样的问题,在 2015 年,我一直想知道为什么人们创建的库没有明确的取消机制。我想我会使用 Apache Http 客户端。
标签: java multithreading jersey httpconnection cancellation