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