I am using the Oracle Jersey Client
, and am trying to cancel a long running get
or put
operation.
The Client
is constructed as:
JacksonJsonProvider provider = new JacksonJsonProvider(new ObjectMapper());
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getSingletons().add(provider);
Client client = Client.create(clientConfig);
The following code is executed on a worker thread:
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!
I want to cancel this long-running put
. When I try to interrupt the worker thread, the put
operation continues to run. From what I can see, the Jersey Client makes no attempt to check for Thread.interrupted()
.
I see the same behavior when using an AsyncWebResource
instead of WebResource
and using Future.cancel(true)
on the Builder.put(..)
call.
So far, the only solution I have come up with to interrupt this is throwing a RuntimeException
in a ContainerListener:
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
}
}...
I am wondering if there is a better solution (perhaps when creating the Client
) that correctly handles interruptible I/O without using a RuntimeException
.