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      public V call() throws Exception {
82          if (!cancelled.get()) {
83              try {
84                  metrics.getActiveConnections().incrementAndGet();
85                  started = System.currentTimeMillis();
86                  try {
87                      metrics.getScheduledConnections().decrementAndGet();
88                      final V result = httpclient.execute(request, responseHandler, context);
89                      ended = System.currentTimeMillis();
90                      metrics.getSuccessfulConnections().increment(started);
91                      if (callback != null) {
92                          callback.completed(result);
93                      }
94                      return result;
95                  } catch (final Exception e) {
96                      metrics.getFailedConnections().increment(started);
97                      ended = System.currentTimeMillis();
98                      if (callback != null) {
99                          callback.failed(e);
100                     }
101                     throw e;
102                 }
103             } finally {
104                 metrics.getRequests().increment(started);
105                 metrics.getTasks().increment(started);
106                 metrics.getActiveConnections().decrementAndGet();
107             }
108         } else {
109             throw new IllegalStateException("call has been cancelled for request " + request.getURI());
110         }
111     }
112 
113     public void cancel() {
114         cancelled.set(true);
115         if (callback != null) {
116             callback.cancelled();
117         }
118     }
119 }