1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 package org.apache.http.impl.client; 28 29 import java.util.concurrent.FutureTask; 30 31 import org.apache.http.client.methods.HttpUriRequest; 32 33 /** 34 * FutureTask implementation that wraps a HttpAsyncClientCallable and exposes various task 35 * specific metrics. 36 * 37 * @param <V> 38 */ 39 public class HttpRequestFutureTask<V> extends FutureTask<V> { 40 41 private final HttpUriRequest request; 42 private final HttpRequestTaskCallable<V> callable; 43 44 public HttpRequestFutureTask( 45 final HttpUriRequest request, 46 final HttpRequestTaskCallable<V> httpCallable) { 47 super(httpCallable); 48 this.request = request; 49 this.callable = httpCallable; 50 } 51 52 /* 53 * (non-Javadoc) 54 * @see java.util.concurrent.FutureTask#cancel(boolean) 55 */ 56 @Override 57 public boolean cancel(final boolean mayInterruptIfRunning) { 58 callable.cancel(); 59 if (mayInterruptIfRunning) { 60 request.abort(); 61 } 62 return super.cancel(mayInterruptIfRunning); 63 } 64 65 /** 66 * @return the time in millis the task was scheduled. 67 */ 68 public long scheduledTime() { 69 return callable.getScheduled(); 70 } 71 72 /** 73 * @return the time in millis the task was started. 74 */ 75 public long startedTime() { 76 return callable.getStarted(); 77 } 78 79 /** 80 * @return the time in millis the task was finished/cancelled. 81 */ 82 public long endedTime() { 83 if (isDone()) { 84 return callable.getEnded(); 85 } 86 throw new IllegalStateException("Task is not done yet"); 87 } 88 89 /** 90 * @return the time in millis it took to make the request (excluding the time it was 91 * scheduled to be executed). 92 */ 93 public long requestDuration() { 94 if (isDone()) { 95 return endedTime() - startedTime(); 96 } 97 throw new IllegalStateException("Task is not done yet"); 98 } 99 100 /** 101 * @return the time in millis it took to execute the task from the moment it was scheduled. 102 */ 103 public long taskDuration() { 104 if (isDone()) { 105 return endedTime() - scheduledTime(); 106 } 107 throw new IllegalStateException("Task is not done yet"); 108 } 109 110 @Override 111 public String toString() { 112 return request.getRequestLine().getUri(); 113 } 114 115 }