View Javadoc

1   /*
2    * ====================================================================
3    *
4    *  Licensed to the Apache Software Foundation (ASF) under one or more
5    *  contributor license agreements.  See the NOTICE file distributed with
6    *  this work for additional information regarding copyright ownership.
7    *  The ASF licenses this file to You under the Apache License, Version 2.0
8    *  (the "License"); you may not use this file except in compliance with
9    *  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, software
14   *  distributed under the License is distributed on an "AS IS" BASIS,
15   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *  See the License for the specific language governing permissions and
17   *  limitations under the License.
18   * ====================================================================
19   *
20   * This software consists of voluntary contributions made by many
21   * individuals on behalf of the Apache Software Foundation.  For more
22   * information on the Apache Software Foundation, please see
23   * <http://www.apache.org/>.
24   *
25   */
26  
27  package org.apache.http.client.fluent;
28  
29  import java.util.concurrent.Future;
30  
31  import org.apache.http.client.ResponseHandler;
32  import org.apache.http.concurrent.BasicFuture;
33  import org.apache.http.concurrent.FutureCallback;
34  
35  public class Async {
36  
37      private Executor executor;
38      private java.util.concurrent.Executor concurrentExec;
39  
40      public static Async newInstance() {
41          return new Async();
42      }
43  
44      Async() {
45          super();
46      }
47  
48      public Async use(final Executor executor) {
49          this.executor = executor;
50          return this;
51      }
52      
53      public Async use(final java.util.concurrent.Executor concurrentExec) {
54          this.concurrentExec = concurrentExec;
55          return this;
56      }
57      
58      static class ExecRunnable<T> implements Runnable {
59          
60          private final BasicFuture<T> future;
61          private final Request request;
62          private final Executor executor;
63          private final ResponseHandler<T> handler;
64          
65          ExecRunnable(
66                  final BasicFuture<T> future, 
67                  final Request request, 
68                  final Executor executor, 
69                  final ResponseHandler<T> handler) {
70              super();
71              this.future = future;
72              this.request = request;
73              this.executor = executor;
74              this.handler = handler;
75          }
76  
77          public void run() {
78              try {
79                  Response response = this.executor.execute(this.request);
80                  T result = response.handleResponse(this.handler);
81                  this.future.completed(result);
82              } catch (Exception ex) {
83                  this.future.failed(ex);
84              }
85          }
86          
87      }
88      
89      public <T> Future<T> execute(
90              final Request request, final ResponseHandler<T> handler, final FutureCallback<T> callback) {
91          BasicFuture<T> future = new BasicFuture<T>(callback);
92          ExecRunnable<T> runnable = new ExecRunnable<T>(
93                  future,
94                  request,
95                  this.executor != null ? this.executor : Executor.newInstance(),
96                  handler);
97          if (this.concurrentExec != null) {
98              this.concurrentExec.execute(runnable);
99          } else {
100             Thread t = new Thread(runnable);
101             t.setDaemon(true);
102             t.start();
103         }
104         return future;
105     }
106     
107     public <T> Future<T> execute(final Request request, final ResponseHandler<T> handler) {
108         return execute(request, handler, null);
109     }
110 
111     public Future<Content> execute(final Request request, final FutureCallback<Content> callback) {
112         return execute(request, new ContentResponseHandler(), callback);
113     }
114     
115     public Future<Content> execute(final Request request) {
116         return execute(request, new ContentResponseHandler(), null);
117     }
118     
119 }