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