title: Cancel rest request if response takes too long
body: <p>I would like to cancel a REST request, if the response takes longer than 3 seconds, but I haven't managed to figure out how to do it.</p>
  
  <p>So let's say I have a thread A:</p>
  
  <pre><code>@Override
  public void run() {
      RestTemplate restTemplate = new RestTemplate();
      IsAliveMessage isAliveMessage = new IsAliveMessage(nodeInfo.getHostname(), nodeInfo.getPort());
      IsAliveResponse isAliveResponse = restTemplate.postForObject(
          "http://" + nodeInfo.getHostname() + ":" + nodeInfo.getPort() + "/node/isAlive",
           isAliveMessage,
           IsAliveResponse.class);
  }
  </code></pre>
  
  <p>that makes a request and expects an answer from B:</p>
  
  <pre><code>@RequestMapping( value="/isAlive", method= RequestMethod.POST)
      public IsAliveResponse isAlive() throws ConnectException {
          try {
              Thread.sleep(100000);
              IsAliveResponse response = new IsAliveResponse("here here!" ,true);
              return response;
          } catch (Exception e) {
              Thread.currentThread().interrupt();
      }
  }
  </code></pre>
  
  <p>B "sleeps" and doesn't answer, but A keeps waiting for that answer to come. How can I make A give up the waiting after a certain time span?</p>
  
  <p>Thanks in advance</p>