View Javadoc
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.Callable;
30  import java.util.concurrent.atomic.AtomicBoolean;
31  
32  import org.apache.http.client.HttpClient;
33  import org.apache.http.client.ResponseHandler;
34  import org.apache.http.client.methods.HttpUriRequest;
35  import org.apache.http.concurrent.FutureCallback;
36  import org.apache.http.protocol.HttpContext;
37  
38  class HttpRequestTaskCallable<V> implements Callable<V> {
39  
40      private final HttpUriRequest request;
41      private final HttpClient httpclient;
42      private final AtomicBoolean cancelled = new AtomicBoolean(false);
43  
44      private final long scheduled = System.currentTimeMillis();
45      private long started = -1;
46      private long ended = -1;
47  
48      private final HttpContext context;
49      private final ResponseHandler<V> responseHandler;
50      private final FutureCallback<V> callback;
51  
52      private final FutureRequestExecutionMetrics metrics;
53  
54      HttpRequestTaskCallable(
55              final HttpClient httpClient,
56              final HttpUriRequest request,
57              final HttpContext context,
58              final ResponseHandler<V> responseHandler,
59              final FutureCallback<V> callback,
60              final FutureRequestExecutionMetrics metrics) {
61          this.httpclient = httpClient;
62          this.responseHandler = responseHandler;
63          this.request = request;
64          this.context = context;
65          this.callback = callback;
66          this.metrics = metrics;
67      }
68  
69      public long getScheduled() {
70          return scheduled;
71      }
72  
73      public long getStarted() {
74          return started;
75      }
76  
77      public long getEnded() {
78          return ended;
79      }
80  
81      @Override
82      public V call() throws Exception {
83          if (!cancelled.get()) {
84              try {
85                  metrics.getActiveConnections().incrementAndGet();
86                  started = System.currentTimeMillis();
87                  try {
88                      metrics.getScheduledConnections().decrementAndGet();
89                      final V result = httpclient.execute(request, responseHandler, context);
90                      ended = System.currentTimeMillis();
91                      metrics.getSuccessfulConnections().increment(started);
92                      if (callback != null) {
93                          callback.completed(result);
94                      }
95                      return result;
96                  } catch (final Exception e) {
97                      metrics.getFailedConnections().increment(started);
98                      ended = System.currentTimeMillis();
99                      if (callback != null) {
100                         callback.failed(e);
101                     }
102                     throw e;
103                 }
104             } finally {
105                 metrics.getRequests().increment(started);
106                 metrics.getTasks().increment(started);
107                 metrics.getActiveConnections().decrementAndGet();
108             }
109         }
110         throw new IllegalStateException("call has been cancelled for request " + request.getURI());
111     }
112 
113     public void cancel() {
114         cancelled.set(true);
115         if (callback != null) {
116             callback.cancelled();
117         }
118     }
119 }