001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027package org.apache.http.impl.client;
028
029import java.util.concurrent.FutureTask;
030
031import org.apache.http.client.methods.HttpUriRequest;
032
033/**
034 * FutureTask implementation that wraps a HttpAsyncClientCallable and exposes various task
035 * specific metrics.
036 *
037 * @param <V>
038 */
039public class HttpRequestFutureTask<V> extends FutureTask<V> {
040
041    private final HttpUriRequest request;
042    private final HttpRequestTaskCallable<V> callable;
043
044    public HttpRequestFutureTask(
045            final HttpUriRequest request,
046            final HttpRequestTaskCallable<V> httpCallable) {
047        super(httpCallable);
048        this.request = request;
049        this.callable = httpCallable;
050    }
051
052    /*
053     * (non-Javadoc)
054     * @see java.util.concurrent.FutureTask#cancel(boolean)
055     */
056    @Override
057    public boolean cancel(final boolean mayInterruptIfRunning) {
058        callable.cancel();
059        if (mayInterruptIfRunning) {
060            request.abort();
061        }
062        return super.cancel(mayInterruptIfRunning);
063    }
064
065    /**
066     * @return the time in millis the task was scheduled.
067     */
068    public long scheduledTime() {
069        return callable.getScheduled();
070    }
071
072    /**
073     * @return the time in millis the task was started.
074     */
075    public long startedTime() {
076        return callable.getStarted();
077    }
078
079    /**
080     * @return the time in millis the task was finished/cancelled.
081     */
082    public long endedTime() {
083        if (isDone()) {
084            return callable.getEnded();
085        } else {
086            throw new IllegalStateException("Task is not done yet");
087        }
088    }
089
090    /**
091     * @return the time in millis it took to make the request (excluding the time it was
092     * scheduled to be executed).
093     */
094    public long requestDuration() {
095        if (isDone()) {
096            return endedTime() - startedTime();
097        } else {
098            throw new IllegalStateException("Task is not done yet");
099        }
100    }
101
102    /**
103     * @return the time in millis it took to execute the task from the moment it was scheduled.
104     */
105    public long taskDuration() {
106        if (isDone()) {
107            return endedTime() - scheduledTime();
108        } else {
109            throw new IllegalStateException("Task is not done yet");
110        }
111    }
112
113    @Override
114    public String toString() {
115        return request.getRequestLine().getUri();
116    }
117
118}