Esempio n. 1
0
        /// <summary>Schedule a request for execution.</summary>
        /// <remarks>Schedule a request for execution.</remarks>
        /// <?></?>
        /// <param name="request">request to execute</param>
        /// <param name="context">optional context; use null if not needed.</param>
        /// <param name="responseHandler">handler that will process the response.</param>
        /// <param name="callback">
        /// callback handler that will be called when the request is scheduled,
        /// started, completed, failed, or cancelled.
        /// </param>
        /// <returns>HttpAsyncClientFutureTask for the scheduled request.</returns>
        /// <exception cref="System.Exception">System.Exception</exception>
        public virtual HttpRequestFutureTask <T> Execute <T>(IHttpUriRequest request, HttpContext
                                                             context, ResponseHandler <T> responseHandler, FutureCallback <T> callback)
        {
            if (closed.Get())
            {
                throw new InvalidOperationException("Close has been called on this httpclient instance."
                                                    );
            }
            metrics.GetScheduledConnections().IncrementAndGet();
            HttpRequestTaskCallable <T> callable = new HttpRequestTaskCallable <T>(httpclient,
                                                                                   request, context, responseHandler, callback, metrics);
            HttpRequestFutureTask <T> httpRequestFutureTask = new HttpRequestFutureTask <T>(request
                                                                                            , callable);

            executorService.Execute(httpRequestFutureTask);
            return(httpRequestFutureTask);
        }
 /// <exception cref="System.Exception"></exception>
 public virtual V Call()
 {
     if (!cancelled.Get())
     {
         try
         {
             metrics.GetActiveConnections().IncrementAndGet();
             started = Runtime.CurrentTimeMillis();
             try
             {
                 metrics.GetScheduledConnections().DecrementAndGet();
                 V result = httpclient.Execute(request, responseHandler, context);
                 ended = Runtime.CurrentTimeMillis();
                 metrics.GetSuccessfulConnections().Increment(started);
                 if (callback != null)
                 {
                     callback.Completed(result);
                 }
                 return(result);
             }
             catch (Exception e)
             {
                 metrics.GetFailedConnections().Increment(started);
                 ended = Runtime.CurrentTimeMillis();
                 if (callback != null)
                 {
                     callback.Failed(e);
                 }
                 throw;
             }
         }
         finally
         {
             metrics.GetRequests().Increment(started);
             metrics.GetTasks().Increment(started);
             metrics.GetActiveConnections().DecrementAndGet();
         }
     }
     else
     {
         throw new InvalidOperationException("call has been cancelled for request " + request
                                             .GetURI());
     }
 }