/** * <p>Mark this request as cancelled and frees any resources held by the request if possible. * All subsequent calls to {@link #isCancelled()} on this Request return <em>true</em>.</p> * * @see #isCancelled() */ public void cancel() { if (cancel) return; if (timeoutManager != null && timeout != null) timeoutManager.unscheduleTimeout(this); cancel = true; }
/** * <p>Sets the allocated time that this Request is allowed to exist before the corresponding call to {@link * ResponseHandler#handleResponse(Response)} must have been made. If no timeout value is assigned to a Request, * there will be no timeout.</p> * * <p>Once the allocated time has expired, unless the {@link ResponseHandler} has already been called, the {@link * RequestHandler#handleTimeout(Request, ResponseHandler)} method is invoked.</p> * * <p>Calls to {@link #isCancelled()} return <em>true</em> if timeout has been exceeded.</p> * * @param timeout The allocated amount of time. * @param unit The time unit of the <em>timeout</em> argument. * @see #getTimeout(TimeUnit) * @see #timeRemaining(TimeUnit) */ public void setTimeout(long timeout, TimeUnit unit) { this.timeout = unit.toMillis(timeout); if (timeoutManager != null) { timeoutManager.scheduleTimeout(this); } }
/** * <p>Sets a {@link TimeoutManager} to be called when {@link #setTimeout(long, TimeUnit)} is invoked. If a timeout * has already been set for this Request, the TimeoutManager is called before returning. This method will throw an * IllegalStateException if it has already been called.</p> * * <p><b>NOTE:</b> This is used by the default timeout management implementation, so unless you are replacing that * mechanism you should avoid calling this method. If you <em>do</em> want to replace that mechanism, you need to * call this method prior to calling the target {@link RequestHandler} (since that injects the default manager).</p> * * @param timeoutManager The manager to set. * @throws NullPointerException If the TimeoutManager is null. * @throws IllegalStateException If another TimeoutManager has already been set. * @see #getTimeoutManager() * @see #setTimeout(long, TimeUnit) */ public void setTimeoutManager(TimeoutManager timeoutManager) { Objects.requireNonNull(timeoutManager, "timeoutManager"); if (this.timeoutManager != null) { throw new IllegalStateException("Timeout manager already set."); } this.timeoutManager = timeoutManager; if (timeout != null) { timeoutManager.scheduleTimeout(this); } }