She*_*tin 5 java multithreading jersey httpconnection cancellation
我正在使用 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);
Run Code Online (Sandbox Code Playgroud)
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!
Run Code Online (Sandbox Code Playgroud)
我想取消这个长期运行的put. 当我尝试中断工作线程时,put操作继续运行。据我所见,泽西岛客户端没有尝试检查Thread.interrupted().
AsyncWebResource我在使用 an代替和在通话中WebResource使用时看到相同的行为。Future.cancel(true)Builder.put(..)
到目前为止,我想出的唯一解决方案是在ContainerListenerRuntimeException中抛出 a :
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
}
}...
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好的解决方案(也许在创建 时Client)可以在不使用RuntimeException.
我想知道是否有更好的解决方案(也许在创建客户端时)可以在不使用 RuntimeException 的情况下正确处理可中断 I/O。
是的,只有当代码正在监视中断或调用监视中断的其他方法(例如Thread.sleep(...))时,中断线程才会起作用。
从侦听器中抛出异常听起来并不是一个坏主意。我当然会创建你自己的RuntimeException类,例如TimeoutRuntimeException或其他东西,这样你就可以专门捕获和处理它。
另一件要做的事情是关闭正在写入的底层 IO 流,这会导致IOException但我不熟悉 Jersey,所以我不确定您是否可以访问该连接。
啊,这是一个想法。与其放置 ,不如在从 读取但也有超时的Filea 上放置某种扩展怎么样?因此,Jersey 将从缓冲区中读取数据,并且在某个时刻,如果超时到期,它会抛出异常。BufferedInputStreamFileIOException
| 归档时间: |
|
| 查看次数: |
1457 次 |
| 最近记录: |