示例#1
0
  /** {@inheritDoc} */
  @Override
  public R get(long timeout, TimeUnit unit) throws GridException {
    A.ensure(timeout >= 0, "timeout cannot be negative: " + timeout);
    A.notNull(unit, "unit");

    checkValid();

    try {
      long now = System.currentTimeMillis();

      long end = timeout == 0 ? Long.MAX_VALUE : now + MILLISECONDS.convert(timeout, unit);

      // Account for overflow.
      if (end < 0) end = Long.MAX_VALUE;

      synchronized (mux) {
        while (!done && !cancelled && now < end) {
          mux.wait(end - now);

          if (!done) now = System.currentTimeMillis();
        }

        if (done) {
          if (err != null) throw U.cast(err);

          return res;
        }

        if (cancelled) throw new GridFutureCancelledException("Future was cancelled: " + this);

        throw new GridFutureTimeoutException(
            "Timeout was reached before computation completed [duration="
                + duration()
                + "ms, timeout="
                + unit.toMillis(timeout)
                + "ms]");
      }
    } catch (InterruptedException e) {
      throw new GridInterruptedException(
          "Got interrupted while waiting for future to complete [duration="
              + duration()
              + "ms, timeout="
              + unit.toMillis(timeout)
              + "ms]",
          e);
    }
  }
示例#2
0
 /**
  * Sets the read timeout for the underlying {@link HttpURLConnection}
  * 
  * @param duration duration of the timeout
  * 
  * @param unit unit of time (milliseconds, seconds, etc)
  */
 public void setReadTimeout(int duration, TimeUnit unit)
 {
   this.readTimeout = unit.toMillis(duration);
 }
示例#3
0
 /**
  * Sets the connect timeout for the underlying {@link HttpURLConnection}
  * 
  * @param duration duration of the timeout
  * 
  * @param unit unit of time (milliseconds, seconds, etc)
  */
 public void setConnectTimeout(int duration, TimeUnit unit)
 {
   this.connectTimeout = unit.toMillis(duration);
 }
示例#4
0
 /** {@inheritDoc} */
 @Override
 public long getDelay(TimeUnit unit) {
   return unit.convert(nextFlushTime() - U.currentTimeMillis(), TimeUnit.MILLISECONDS);
 }